Re: c# Thread problem
- From: cty0000@xxxxxxxxx
- Date: Wed, 04 Jul 2007 03:34:13 -0700
On 7 4 , 6 31 , "Jon Skeet [C# MVP]" <s...@xxxxxxxxx> wrote:
On Jul 4, 10:27 am, cty0...@xxxxxxxxx wrote:
I have some question..
This is my first time to use thread..
Following code does not have error but two warring
The warring is
Warning 2 'System.Threading.Thread.Suspend()' is obsolete:
'Thread.Suspend has been deprecated. Please use other classes in
System.Threading, such as Monitor, Mutex, Event, and Semaphore, to
synchronize Threads or protect resources. http://go.microsoft.com/fwlink/?linkid=14202'
The warring are happened thread Resume/Suspend..
I'm not sure what is problem becuase thread programing is first time
for me...
As it says, you shouldn't use Suspend/Resume, basically. If you need a
producer/consumer queue (which it looks like this is) you can use Auto/
ManualResetEvent, or Monitor.Wait/Pulse/PulseAll. See the second half
ofhttp://pobox.com/~skeet/csharp/threads/deadlocks.shtmlfor an example.
You also shouldn't manually be calling Monitor.Enter/Exit - use lock
instead.
Finally, making a UI thread block at all is a bad idea - you won't be
able to do things like move the window while it's blocked. Instead,
you should either work asynchronously or have another thread waiting
for messages and calling Control.BeginInvoke or Control.Invoke to use
that message on the UI thread.
Jon
I changed code as your recommand...
The warring was disappear but some time it happen deadlock... -_-;;
deadlock position is lock (queueLock) at addNewMsg function...
only one difference between my code and the code which is linked at
http://pobox.com/~skeet/csharp/threads/deadlocks.shtml
is that my code need to loop forever until application finish..
So I add while (true) at threadJob... with sleep 0.1 secs in every
loop..
Could you check my code..
And can I ask one more?
My application run with one Main Form first, and button click on the
Main form then start receive form which is include thread job..
When I close Main Form, then the applicaion is not finished..
As I debug.. the Application is wait Monitor.Wait(queueLock) at
threadJob function forever
Do you know the reason...
Thread guiUpdateThread;
guiUpdateThread = new Thread(new ThreadStart(threadJob));
guiUpdateThread.Start();
readonly object queueLock = new object();
public void addNewMsg(GUIMsg msg)
{
lock (queueLock)
{
GuiMsgQueue.Enqueue(msg);
Monitor.Pulse(queueLock);
}
}
public void threadJob()
{
GUIMsg gMsg;
while (true)
{
Thread.Sleep(100);
lock (queueLock)
{
while (GuiMsgQueue.Count == 0)
Monitor.Wait(queueLock);
gMsg = GuiMsgQueue.Dequeue();
update(gMsg);
}
}
}
.
- Follow-Ups:
- Re: c# Thread problem
- From: Jon Skeet [C# MVP]
- Re: c# Thread problem
- From: Christof Nordiek
- Re: c# Thread problem
- From: Tom Spink
- Re: c# Thread problem
- From: Göran Andersson
- Re: c# Thread problem
- References:
- c# Thread problem
- From: cty0000
- Re: c# Thread problem
- From: Jon Skeet [C# MVP]
- c# Thread problem
- Prev by Date: Re: Accessing form controls
- Next by Date: Re: Trouble connecting to Oracle database from C#
- Previous by thread: Re: c# Thread problem
- Next by thread: Re: c# Thread problem
- Index(es):