E
Eric Mamet
I am trying to write a Server Component that will spawn a database
task in a thread and wait a certain amount of time for completion.
If, at the end of this time, the job is not complete, I want the
component to report this to the calling code and leave the Thread
finishing the SQL Job.
I tried to emulate this scenario with "Thread.Sleep()" in VB and it
does what I want in a Console Application but not in a Windows
Application.
Could there be something special about Windows Application?
My Console app code is as follows (feel free to use it in your
projects!)
Imports System.Threading
Module Module1
Private _MyThread As Thread
Const MainLoop As Integer = 3
Const ThreadLoop As Integer = 5
Sub Main()
Dim Idx As Integer
_MyThread = New Thread(AddressOf ThreadJob)
Console.WriteLine("Before Launching Thread")
_MyThread.Start()
Console.WriteLine("After Launching Thread")
For Idx = MainLoop To 1 Step -1
Thread.Sleep(1000) ' wait 1 sec
Console.WriteLine("Main Loop...")
Next
If _MyThread.ThreadState = ThreadState.Running Then
Console.WriteLine("Thread is running")
Else
Console.WriteLine("Thread is NOT running")
End If
End Sub
Private Sub ThreadJob()
Dim Idx As Integer
Console.WriteLine("Thread Started")
For Idx = ThreadLoop To 1 Step -1
Thread.Sleep(1000)
Console.WriteLine("Thread Loop")
Next
Console.WriteLine("Thread Finished")
End Sub
End Module
In the Windows app, I also start the Thread before my "Main" Loop but
the thread code only starts after the "Main" Loop finishes.
I was hoping that both threads would run somewhat in parallel.
Thanks
Eric Mamet
task in a thread and wait a certain amount of time for completion.
If, at the end of this time, the job is not complete, I want the
component to report this to the calling code and leave the Thread
finishing the SQL Job.
I tried to emulate this scenario with "Thread.Sleep()" in VB and it
does what I want in a Console Application but not in a Windows
Application.
Could there be something special about Windows Application?
My Console app code is as follows (feel free to use it in your
projects!)
Imports System.Threading
Module Module1
Private _MyThread As Thread
Const MainLoop As Integer = 3
Const ThreadLoop As Integer = 5
Sub Main()
Dim Idx As Integer
_MyThread = New Thread(AddressOf ThreadJob)
Console.WriteLine("Before Launching Thread")
_MyThread.Start()
Console.WriteLine("After Launching Thread")
For Idx = MainLoop To 1 Step -1
Thread.Sleep(1000) ' wait 1 sec
Console.WriteLine("Main Loop...")
Next
If _MyThread.ThreadState = ThreadState.Running Then
Console.WriteLine("Thread is running")
Else
Console.WriteLine("Thread is NOT running")
End If
End Sub
Private Sub ThreadJob()
Dim Idx As Integer
Console.WriteLine("Thread Started")
For Idx = ThreadLoop To 1 Step -1
Thread.Sleep(1000)
Console.WriteLine("Thread Loop")
Next
Console.WriteLine("Thread Finished")
End Sub
End Module
In the Windows app, I also start the Thread before my "Main" Loop but
the thread code only starts after the "Main" Loop finishes.
I was hoping that both threads would run somewhat in parallel.
Thanks
Eric Mamet