Re: c# Thread problem
- From: Tom Spink <tspink@xxxxxxxxx>
- Date: Wed, 04 Jul 2007 12:14:05 +0100
cty0000@xxxxxxxxx wrote:
On 7 4 , 6 31 , "Jon Skeet [C# MVP]" <s...@xxxxxxxxx> wrote:<snippedy>
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..
Hi,
The reason you're getting a deadlock is because you're waiting on a lock
that you already hold. It looks like you need to implement the
Producer/Consumer pattern here, as has been mentioned. Take a look here
for an example:
http://www.betasafe.com/code?view=prcon
--
Tom Spink
University of Edinburgh
.
- Follow-Ups:
- Re: c# Thread problem
- From: Jon Skeet [C# MVP]
- Re: c# Thread problem
- References:
- c# Thread problem
- From: cty0000
- Re: c# Thread problem
- From: Jon Skeet [C# MVP]
- Re: c# Thread problem
- From: cty0000
- c# Thread problem
- Prev by Date: Re: whats faster, initialize component, or form load?
- Next by Date: Re: Problems with DllImport and a Delphi native DLL.
- Previous by thread: Re: c# Thread problem
- Next by thread: Re: c# Thread problem
- Index(es):