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.Threadi ng.Thread.Suspe nd()' is obsolete:
'Thread.Suspend has been deprecated. Please use other classes in
System.Threadin g, such as Monitor, Mutex, Event, and Semaphore, to
synchronize Threads or protect resources. http://go.microsoft.co m/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...
This class to update message (GuiMsg) after received new message from
other class.
1. First application start, thread is resume by threadJob because
there are no message on QUEUE
2. If new message arrived, addNewMsg push new message to QUEUE
3. Resume thread to procss message(update)
4. Loop threadJob until QUEUE empty (during update, QUEUE can add new
msg)
=============== ============
Somewhere define and allowcate thread like flowing 3 line
Thread guiUpdateThread ;
guiUpdateThread = new Thread(new ThreadStart(thr eadJob));
guiUpdateThread .Start();
public void addNewMsg(GUIMs g msg)
{
Monitor.Enter(G uiMsgQueue);
GuiMsgQueue.Enq ueue(msg);
Monitor.Exit(Gu iMsgQueue);
if (guiUpdateThrea d.ThreadState ==
ThreadState.Sus pended)
guiUpdateThread .Resume();
}
private void threadJob()
{
GUIMsg gMsg;
while (true)
{
if (GuiMsgQueue.Co unt 0)
{
Monitor.Enter(G uiMsgQueue);
gMsg = GuiMsgQueue.Deq ueue();
Monitor.Exit(Gu iMsgQueue);
update(gMsg);
}
else
{
if (guiUpdateThrea d.ThreadState ==
ThreadState.Run ning)
guiUpdateThread .Suspend();
}
}
}
Please help me.. 20 5532
On Jul 4, 10:27 am, cty0...@gmail.c om 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.Threadi ng.Thread.Suspe nd()' is obsolete:
'Thread.Suspend has been deprecated. Please use other classes in
System.Threadin g, such as Monitor, Mutex, Event, and Semaphore, to
synchronize Threads or protect resources. http://go.microsoft.co m/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/
ManualResetEven t, or Monitor.Wait/Pulse/PulseAll. See the second half
of http://pobox.com/~skeet/csharp/threads/deadlocks.shtml for 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.BeginIn voke or Control.Invoke to use
that message on the UI thread.
Jon
On 7 4 , 6 31 , "Jon Skeet [C# MVP]" <s...@pobox.com wrote:
On Jul 4, 10:27 am, cty0...@gmail.c om 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.Threadi ng.Thread.Suspe nd()' is obsolete:
'Thread.Suspend has been deprecated. Please use other classes in
System.Threadin g, such as Monitor, Mutex, Event, and Semaphore, to
synchronize Threads or protect resources. http://go.microsoft.co m/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/
ManualResetEven t, or Monitor.Wait/Pulse/PulseAll. See the second half
ofhttp://pobox.com/~skeet/csharp/threads/deadlocks.shtml for 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.BeginIn voke 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(qu eueLock) at
threadJob function forever
Do you know the reason...
Thread guiUpdateThread ;
guiUpdateThread = new Thread(new ThreadStart(thr eadJob));
guiUpdateThread .Start();
readonly object queueLock = new object();
public void addNewMsg(GUIMs g msg)
{
lock (queueLock)
{
GuiMsgQueue.Enq ueue(msg);
Monitor.Pulse(q ueueLock);
}
}
public void threadJob()
{
GUIMsg gMsg;
while (true)
{
Thread.Sleep(10 0);
lock (queueLock)
{
while (GuiMsgQueue.Co unt == 0)
Monitor.Wait(qu eueLock);
gMsg = GuiMsgQueue.Deq ueue();
update(gMsg);
}
}
} ct*****@gmail.c om wrote:
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(qu eueLock) at
threadJob function forever
Do you know the reason...
Thread guiUpdateThread ;
guiUpdateThread = new Thread(new ThreadStart(thr eadJob));
guiUpdateThread .Start();
readonly object queueLock = new object();
public void addNewMsg(GUIMs g msg)
{
lock (queueLock)
{
GuiMsgQueue.Enq ueue(msg);
Monitor.Pulse(q ueueLock);
}
}
public void threadJob()
{
GUIMsg gMsg;
while (true)
{
Thread.Sleep(10 0);
lock (queueLock)
{
while (GuiMsgQueue.Co unt == 0)
Monitor.Wait(qu eueLock);
Oops. You have a loop inside the lock waiting for something to get into
the queue, but the only code that puts something in the queue is also in
a lock, so that will never happen. This thread will wait in this loop
forever, running at 100% CPU while doing so.
>
gMsg = GuiMsgQueue.Deq ueue();
update(gMsg);
}
}
}
--
Göran Andersson
_____ http://www.guffa.com ct*****@gmail.c om wrote:
On 7 4 , 6 31 , "Jon Skeet [C# MVP]" <s...@pobox.com wrote:
>On Jul 4, 10:27 am, cty0...@gmail.c om 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.Threadi ng.Thread.Suspe nd()' is obsolete:
'Thread.Suspend has been deprecated. Please use other classes in
System.Threadin g, such as Monitor, Mutex, Event, and Semaphore, to
synchronize Threads or protect resources.
http://go.microsoft.co m/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/ ManualResetEve nt, or Monitor.Wait/Pulse/PulseAll. See the second half ofhttp://pobox.com/~skeet/csharp/threads/deadlocks.shtml for 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.BeginIn voke 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..
<snippedy>
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
<ct*****@gmail. comschrieb im Newsbeitrag
news:11******** *************@e 16g2000pri.goog legroups.com...
On 7 4 , 6 31 , "Jon Skeet [C# MVP]" <s...@pobox.com wrote:
>On Jul 4, 10:27 am, cty0...@gmail.c om wrote:
while (true)
{
Thread.Sleep(10 0);
lock (queueLock)
{
while (GuiMsgQueue.Co unt == 0)
Monitor.Wait(qu eueLock);
gMsg = GuiMsgQueue.Deq ueue();
The following line should be outside of the lock block. The lock should only
spawn the deqeueing, not what is done with the deqeued object after.
update(gMsg);
}
}
}
I'm not sure, if this causes the deadlock. Since deadlocks can only occur
with atleast two lock-objects, there must something else go on. Maybe the
call on update blocks, waiting for the Mainthread, while this is trying to
achieve a lock on qeueLock. This would produce a deadlock.
Christof
On Jul 4, 12:14 pm, Tom Spink <tsp...@gmail.c omwrote:
The reason you're getting a deadlock is because you're waiting on a lock
that you already hold.
No, that's the *only* place you can call Wait. If you call
Monitor.Wait on a lock you don't already hold, an exception will be
thrown.
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
Note that that code *also* calls Wait on a lock it already holds.
Jon
On Jul 4, 11:34 am, cty0...@gmail.c om wrote:
<snip>
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 athttp://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..
The code looks mostly okay, but you haven't shown what update() does.
I don't think there's any need for you to hold the queue lock while
you do that. (I'd also put the queue into a different class to keep
the responsibilitie s clear.)
Jon
On Jul 4, 11:57 am, Göran Andersson <g...@guffa.com wrote:
<snip>
while (GuiMsgQueue.Co unt == 0)
Monitor.Wait(qu eueLock);
Oops. You have a loop inside the lock waiting for something to get into
the queue, but the only code that puts something in the queue is also in
a lock, so that will never happen. This thread will wait in this loop
forever, running at 100% CPU while doing so.
No it won't - it will wait for something to pulse the monitor.
Monitor.Wait releases the lock until it's pulsed, at which point it
then waits to reacquire it before returning.
Jon
On Jul 4, 1:15 pm, "Christof Nordiek" <c...@nospam.de wrote:
<snip>
I'm not sure, if this causes the deadlock. Since deadlocks can only occur
with atleast two lock-objects, there must something else go on. Maybe the
call on update blocks, waiting for the Mainthread, while this is trying to
achieve a lock on qeueLock. This would produce a deadlock.
There don't have to be two explicit locks in the code though. Suppose
the update(gMsg) code included a call to Control.Invoke, and at the
time the UI was waiting to enqueue an extra message... that would
cause a deadlock, certainly.
Jon This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Tony Proctor |
last post by:
Hi everyone
We're experiencing some serious anomalies with the scheduling of ASP
threads. I'd be interested to hear if anyone knows what algorithm is used
(e.g. simple round-robin, or something more sophisticated), and what
situations might perturb it. Even a hint as to what would be considered
normal scheduling might help.
The root of our problem is that we observed a normally well-behaved web
application suddenly limit itself to a...
|
by: Ivan |
last post by:
Hi
I have following problem: I'm creating two threads who are performing some
tasks. When one thread finished I would like to restart her again (e.g. new
job). Following example demonstrates that. Problem is that when program is
started many threads are created (see output section), when only two should
be running at any time. Can you please help me to identify me where the
problem is?
Best regards
|
by: Doug Thews |
last post by:
I ran into an interesting re-pain delay after calling the Abort() method on
a thread, but it only happens the very first time I call it. Every time
afterward, there is no delay.
I've got a delegate inside the UI that I call to update the progress meter.
I use the Suspend() and Abort() methods based on button events.
I can watch the progress meter increase just fine when the thread is
running. When I select Start, I enable the Cancel...
|
by: Tomaz Koritnik |
last post by:
I have a class that runs one of it's method in another thread. I use Thread
object to do this and inside ThreadMethod I have an infinite loop:
While (true)
{
// do something
Thread.Sleep(100);
}
The problem is that I don't know how to terminate the thread when my class
|
by: Bob Day |
last post by:
Using vs2003, vb.net
I start a thread, giving it a name before start. Code snippet:
'give each thread a unique name (for later identification)
Trunk_Thread.Name = "Trunk_0_Thread"
' allow only 1 thread per line
Trunk_Thread.ApartmentState = ApartmentState.STA
| |
by: fred |
last post by:
I use a Synclock in a secondary thread and also stop the thread using the
abort method.
If the abort occurs while the thread is in the Synclock will the SyncLock
always be released before the thread stops or do I need to add some extra
code to avoid Synclock staying on after the thread has been stopped.
Thanks
Fred
|
by: Hans |
last post by:
Hi all,
Is there a way that the program that created and started a thread also stops
it.
(My usage is a time-out).
E.g.
thread = threading.Thread(target=Loop.testLoop)
thread.start() # This thread is expected to finish within a second
|
by: joey.powell |
last post by:
I am using VS2005 for a windows forms application. I need to be able to
use a worker thread function to offload some processing from the UI
thread. The worker thread will need access to a datagridview on the
form. I am using the following code to spawn the worker thread...
Thread WorkerThread = new Thread(new ThreadStart(WT_MyFunction));
WorkerThread.IsBackground = true;
WorkerThread.Start();
The problem I am having is...I cannot seem...
|
by: Sin Jeong-hun |
last post by:
Hi. I'm writing a Client/Multi-threaded Server program on Windows
Vista. It worked fine on Windows Vista, but when the server ran on
Windows XP,
I/O operation has been aborted because of either a thread exit or an
application request
exception randomly occurred at the OnReceive method (asynchronous tcp
stream reading). I searched all over the internet and found a post
posted few years ago. He had the same problem as me, and he said it
|
by: Creativ |
last post by:
Why does Thread class not support IDisposable? It's creating quite
some problem. Namely, it can exhaust the resource and you have not
control over it.
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |