CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2001
    Posts
    17

    Starting a thread

    I want to wait for 30 seconds and then display a msgbox, but I don't want the main programme to be idle for those 30 seconds. I want to start a thread that counts 30 seconds and then displays the message.

    How do I start a simple thread in Visual Basic?

    Thanks,
    Yaron.


  2. #2
    Join Date
    Jun 2001
    Location
    Sri Lanka
    Posts
    272

    Re: Starting a thread

    If u want only what u have asked for and if u have a form in ur project, u can use a timer.
    Put a timer on to the form, that u work with. Set the interval as 30000 (milliseconds). In the "Timer Event" of the Timer put whatever u want to happen in every 30 Sec. time period.
    U will get what u want!!!

    Srinika


    If u don't know how to Rate an answer, then Rate my answer to learn, If u know, then practice it

  3. #3
    Join Date
    Mar 2001
    Posts
    17

    Re: Starting a thread

    That won't do.. I need it to work only once.. not every 30 secs..

    Yaron.


  4. #4
    Join Date
    Jun 2001
    Location
    Sri Lanka
    Posts
    272

    Re: Starting a thread

    U can disable the timer after the process.

    sub Timer1_Timer()
    Msgbox "xxxxxxxxxxxx"
    ' any other code
    Timer1.Enabled = false
    end sub




    If u want to happen the operation inside the timer for a different no. of times in every 30 Sec time, u can use a static variable

    Srinika

    If u don't know how to Rate an answer, then Rate my answer to learn, If u know, then practice it

  5. #5
    Join Date
    Jul 2001
    Location
    Mumbai,India
    Posts
    382

    Re: Starting a thread

    I think you did not understand the question.
    What he wants is to start a thread which will do
    the arduous task of counting 30 seconds and then display a message box.
    But, in the meantime he doesn't want the processor to be idle.

    So, it is something like this :

    1.Start the thread.

    2.Do some other processing.

    However, the steps won't be serial as shown here.
    Rather they will be processed in parallel.
    This can only be achieved with MultiThreading,
    as follows.

    Module1 :
    'Declarations :
    Public thrdId as long 'Thread ID
    Public thrdHnd as long 'Thread Handle

    Public Declare Function CreateThread Lib "kernel32" (ByVal _
    lpSecurityAttributes As Long, ByVal dwStackSize As Long, _
    ByVal lpStartAddress As Long, ByVal lpParameter As Long, _
    ByVal dwCreationFlags As Long, _
    lpThreadId As Long) _
    As Long

    Declare Function CloseHandle Lib "kernel32" _
    (ByVal hObject As Long) As Long

    Public Declare Function MessageBoxA Lib "user32" _
    (ByVal hwnd As Long, ByVal lpText As String, _
    ByVal lpCaption As String, ByVal wType As Long) As Long



    Public Sub DispMsgBox()

    MessageBoxA 0, "This is a message"," ",0

    End Sub

    Note : The API MessageBoxA is used because VB's function MsgBox is not thread-safe.

    In Command1_click :

    thrdHnd = CreateThread(0,1000,AddressOf DispMsgBox,0,0,thrdId)

    CloseHandle thrdHnd

    ...Do some other processing..
    ...
    ...


    I Hope this helps you ,Yaron.
    Regards,
    The Beret.













  6. #6
    Join Date
    Jul 2001
    Location
    Mumbai,India
    Posts
    382

    Re: Starting a thread

    I'm sorry, you will also need to add the following call before MessageBoxA..

    Sleep(30 seconds)..

    Regards,
    The Beret.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured