Click to See Complete Forum and Search --> : Starting a thread


yaronb
September 6th, 2001, 03:24 AM
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.

srinika
September 6th, 2001, 05:22 AM
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

yaronb
September 6th, 2001, 06:01 AM
That won't do.. I need it to work only once.. not every 30 secs..

Yaron.

srinika
September 6th, 2001, 06:13 AM
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

Green_Beret
October 11th, 2001, 07:29 AM
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.

Green_Beret
October 11th, 2001, 07:32 AM
I'm sorry, you will also need to add the following call before MessageBoxA..

Sleep(30 seconds)..

Regards,
The Beret.