PC Review


Reply
Thread Tools Rate Thread

Beginner: Thread not starting before UI Thread finishes?

 
 
Eric Mamet
Guest
Posts: n/a
 
      6th Nov 2003
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
 
Reply With Quote
 
 
 
 
Roy Osherove
Guest
Posts: n/a
 
      6th Nov 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, (E-Mail Removed) (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
 
Reply With Quote
 
Eric Mamet
Guest
Posts: n/a
 
      10th Nov 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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ShowDialog - Cross-thread operation not valid: Control'CheckAccountInfo' accessed from a thread other than the thread it was createdon. Tom C Microsoft C# .NET 9 20th Feb 2008 08:15 PM
Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on Joe Microsoft C# .NET 4 12th Mar 2007 09:59 AM
Thread A calls a delegate on Thread B but Thread A executes it!?!? Paul Tomlinson Microsoft C# .NET 4 3rd Feb 2005 10:09 PM
when a thread finishes, it fires any event? Wrathchild Microsoft C# .NET 3 17th Apr 2004 09:22 PM
Threading - Make a thread sleep until a worker finishes Eric Marvets Microsoft Dot NET 0 11th Aug 2003 07:30 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:06 AM.