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

Re: Beginner: Thread not starting before UI Thread finishes? by Roy

Roy
Thu Nov 06 07:09:18 CST 2003

I think the problem is that in the GUI app (not the console) you make
your threaded job show statuses using the GUI. It is always
recommended that only the GUI thread talk to any controls on the form.
If a different thread would like to update the form, it should use
"Control.Invoke" to make those updates using the GUI thread.
This will explain a little more:
if you wanted to update a label on the form , just make the thread
call this method:

private delegate void StdDelegateString(string text);

private void ShowSearchStatus(string status)
{
if(this.InvokeRequired)
{
this.Invoke(new StdDelegateString(ShowSearchStatus),new
object[]{status});
return ;
}

lblStatus.Text= status;
}


On 6 Nov 2003 02:35:05 -0800, eric_mamet_test@yahoo.co.uk (Eric Mamet)
wrote:

>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

---
Regards,

Roy Osherove
www.iserializable.com

Re: Beginner: Thread not starting before UI Thread finishes? by eric_mamet_test

eric_mamet_test
Mon Nov 10 06:40:26 CST 2003

I had suspected something like it and I tried to raiseevents from my
object but still without much success.

Following your advice, I have removed any UI access of any sort and it
works like a treat.

Many Thanks


Eric