473,756 Members | 2,900 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c# Thread problem



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..

Jul 4 '07
20 5541
Jon Skeet [C# MVP] wrote:
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
Whoops.

--
Tom Spink
University of Edinburgh
Jul 4 '07 #11
On Wed, 04 Jul 2007 05:27:52 -0700, Jon Skeet [C# MVP] <sk***@pobox.co m>
wrote:
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.
And in fact, in my experience using Invoke() with a lock is the most
common source of deadlock in .NET code. :)
Jul 4 '07 #12
On 7 5 , 2 40 , "Peter Duniho" <NpOeStPe...@nn owslpianmk.comw rote:
On Wed, 04 Jul 2007 05:27:52 -0700, Jon Skeet [C# MVP] <s...@pobox.com >
wrote:
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.

And in fact, in my experience using Invoke() with a lock is the most
common source of deadlock in .NET code. :)
Actually I'm using invoke() to update control like textBox, Label with
lock...
According to debugger, deadlock may happen by both of invoke to
update label and lock for push a data to QUEUE
I need to update lable and also need to receive message by using
thread..

Is there any solution?
Jul 5 '07 #13
On Jul 5, 10:01 am, cty0...@gmail.c om wrote:
On 7 5 , 2 40 , "Peter Duniho" <NpOeStPe...@nn owslpianmk.comw rote:
On Wed, 04 Jul 2007 05:27:52 -0700, Jon Skeet [C# MVP] <s...@pobox.com >
wrote:
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.
And in fact, in my experience using Invoke() with a lock is the most
common source of deadlock in .NET code. :)

Actually I'm using invoke() to update control like textBox, Label with
lock...
According to debugger, deadlock may happen by both of invoke to
update label and lock for push a data to QUEUE
I need to update lable and also need to receive message by using
thread..

Is there any solution?
Yes:

1) Pull the call to update out of the lock block. (This would also
naturally occur as part of refactoring the queue to its own task-
agnostic class.)
2) Use BeginInvoke instead of Invoke.

Jon

Jul 5 '07 #14
On 7 5 , 6 07 , "Jon Skeet [C# MVP]" <s...@pobox.com wrote:
On Jul 5, 10:01 am, cty0...@gmail.c om wrote:


On 7 5 , 2 40 , "Peter Duniho" <NpOeStPe...@nn owslpianmk.comw rote:
On Wed, 04 Jul 2007 05:27:52 -0700, Jon Skeet [C# MVP] <s...@pobox.com >
wrote:
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.
And in fact, in my experience using Invoke() with a lock is the most
common source of deadlock in .NET code. :)
Actually I'm using invoke() to update control like textBox, Label with
lock...
According to debugger, deadlock may happen by both of invoke to
update label and lock for push a data to QUEUE
I need to update lable and also need to receive message by using
thread..
Is there any solution?

Yes:

1) Pull the call to update out of the lock block. (This would also
naturally occur as part of refactoring the queue to its own task-
agnostic class.)
2) Use BeginInvoke instead of Invoke.

Jon- -

- -
I can not understand all ^^
I need to update function call during exist QUEUE data.. to do this,
updata must be lock block..
get the data until QUEUE empty and update GUI screen.
if update function pull out of lock block.. hmmm.... Actually I do not
know how to implement the code..

If I use begininvoke instead of invoke, I do not need to pull out
upate() from lock block??
I will try to find out about begininvoke..

It's Big problem for me -.-;;

Jul 5 '07 #15
On Jul 5, 10:21 am, cty0...@gmail.c om wrote:
I can not understand all ^^
I need to update function call during exist QUEUE data.. to do this,
updata must be lock block..
No - you *fetch* the item while the queue is locked, but after you've
fetched the update you don't need to hold the lock any more. At least,
if you think you do, please explain why.
get the data until QUEUE empty and update GUI screen.
if update function pull out of lock block.. hmmm.... Actually I do not
know how to implement the code..
Just move the call to "update" to after the brace ending the lock.
If I use begininvoke instead of invoke, I do not need to pull out
upate() from lock block??
I will try to find out about begininvoke..

It's Big problem for me -.-;;
You don't *have* to pull update() out of the lock block, but you
should anyway because conceptually you don't need the lock while
you're updating.

Jon

Jul 5 '07 #16
On 7 5 , 6 27 , "Jon Skeet [C# MVP]" <s...@pobox.com wrote:
On Jul 5, 10:21 am, cty0...@gmail.c om wrote:
I can not understand all ^^
I need to update function call during exist QUEUE data.. to do this,
updata must be lock block..

No - you *fetch* the item while the queue is locked, but after you've
fetched the update you don't need to hold the lock any more. At least,
if you think you do, please explain why.
get the data until QUEUE empty and update GUI screen.
if update function pull out of lock block.. hmmm.... Actually I do not
know how to implement the code..

Just move the call to "update" to after the brace ending the lock.
If I use begininvoke instead of invoke, I do not need to pull out
upate() from lock block??
I will try to find out about begininvoke..
It's Big problem for me -.-;;

You don't *have* to pull update() out of the lock block, but you
should anyway because conceptually you don't need the lock while
you're updating.

Jon
I have changed my code like follow to test. Is this what you means?
The following code...
Launch one thread when new message arrived, then the thread aborted
after update all by using QUEUE message..
Of course more then one messages can be exist in QUEUE when threadJob
is started..
That's whay I use while statement in threadJob..

One msg in QUEUE used to update GUI screen.. (one message = one update
GUI)
So.....
I used update in while loop and lock block....
The following code also makes deadlock... at same position (invoke
control and lock in addNewMsg)
Is it possible to pull update out from lock in my case???

public void addNewMsg(GUIMs g msg)
{

lock (queueLock)
{
GuiMsgQueue.Enq ueue(msg);
}

Thread guiUpdateThread = new Thread(new
ThreadStart(thr eadJob));
guiUpdateThread .Start();
}

private void threadJob()
{
GUIMsg gMsg;

lock (queueLock)
{
while (GuiMsgQueue.Co unt != 0)
{
gMsg = GuiMsgQueue.Deq ueue();
update(gMsg);
}
}
}

Jul 5 '07 #17
On Jul 5, 2:57 pm, cty0...@gmail.c om wrote:
I have changed my code like follow to test. Is this what you means?
No - the call to update(gMsg) is still within the lock, and you've
effectively removed all the producer/consumer stuff. All that was
needed was a one line change, moving the call to update to one line
later.

Jon

Jul 5 '07 #18
<ct*****@gmail. comschrieb im Newsbeitrag
news:11******** **************@ z28g2000prd.goo glegroups.com.. .
What he meant is, change youre original threadJob:

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); //This line is moved.
}
}

After the GuiMsgQueue.Deq ueue() there is no need to hold the lock.

From your code:
>
private void threadJob()
{
GUIMsg gMsg;

lock (queueLock)
{
while (GuiMsgQueue.Co unt != 0)
{
gMsg = GuiMsgQueue.Deq ueue();
update(gMsg);
Here the update ist still inside of the lock. And the lock is even hold over
several dequeues. By this you prevent enqueueing, till the consumer has
emptied the queue.

Christof
}
}
}

Jul 5 '07 #19
On 7 5 , 11 28 , "Christof Nordiek" <c...@nospam.de wrote:
<cty0...@gmail. comschrieb im Newsbeitragnews :11************ **********@z28g 2000prd.googleg roups.com...
What he meant is, change youre original threadJob:

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); //This line is moved.
}
}

After the GuiMsgQueue.Deq ueue() there is no need to hold the lock.

From your code:
private void threadJob()
{
GUIMsg gMsg;
lock (queueLock)
{
while (GuiMsgQueue.Co unt != 0)
{
gMsg = GuiMsgQueue.Deq ueue();
update(gMsg);

Here the update ist still inside of the lock. And the lock is even hold over
several dequeues. By this you prevent enqueueing, till the consumer has
emptied the queue.

Christof
}
}
}- -

- -
Thanks you guys... I was stupid ^^...
I could pull out update like follow, the code work well Thanks.. a
loooooooooooooo oooot

public void addNewMsg(GUIMs g msg)
{

lock (queueLock)
{
GuiMsgQueue.Enq ueue(msg);
}

Thread guiUpdateThread = new Thread(new
ThreadStart(thr eadJob));
guiUpdateThread .Start();
}

private void threadJob()
{
GUIMsg gMsg;
while (GuiMsgQueue.Co unt != 0)
{
lock (queueLock)
{
gMsg = GuiMsgQueue.Deq ueue();
}
update(gMsg);
}

Jul 6 '07 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2327
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...
7
2704
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
20
3032
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...
6
23740
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
13
5095
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
4
2558
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
51
54865
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
14
6890
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...
7
5901
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
34
2808
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.
0
9275
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,...
0
9872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8713
agi2029
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...
1
7248
isladogs
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...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.