c#线程问题 [英] c# Thread problem

查看:56
本文介绍了c#线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一些问题..

这是我第一次使用线程..

以下代码没有错误,但两个交战


战争是


警告2''System.Threading.Thread.Suspend()''已过时:

''Thread.Suspend已被弃用。请使用

System.Threading中的其他类,例如Monitor,Mutex,Event和Semaphore,以便

同步线程或保护资源。 http://go.microsoft.com/fwlink/?linkid=14202''


战争发生在线程简历/暂停......

我' '我不确定是什么问题因为线程编程是第一次

对我来说...

这个类在收到来自

其他类。

1.首次申请开始,线程由threadJob恢复,因为

在QUEUE上没有留言

2.如果新消息到达,addNewMsg将新消息推送到QUEUE

3.恢复线程到procss消息(更新)

4.循环threadJob直到QUEUE为空(在更新期间, QUEUE可以添加新的

msg)


======================== ===


某处定义和允许线程像流动的3行

线程guiUpdateThread;

guiUpdateThread = new Thread(新的ThreadStart (threadJob));

guiUpdateThread.Start();


public void addNewMsg(GUIMsg msg)

{< br $>
Monitor.Enter(GuiMsgQueue);

GuiMsgQueue.Enqueue(msg);

Monitor.Exit(GuiMsgQueue);


if(guiUpdateThread.ThreadState ==

ThreadState.Suspended)

guiUpdateThread.Resume();

}


private void threadJob()

{

GUIMsg gMsg;


while(true )

{

if(GuiMsgQueue.Count 0)

{

Monitor.Enter(GuiMsgQueue);

gMsg = GuiMsgQueue.Dequeue();

Monitor.Exit(GuiMsgQueue);


update(gMsg);

}

其他

{

if(guiUpdateThread.ThreadState ==

ThreadState.Running )

guiUpdateThread.Suspend();

}

}

}

请帮帮我..



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...
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(threadJob));
guiUpdateThread.Start();

public void addNewMsg(GUIMsg msg)
{
Monitor.Enter(GuiMsgQueue);
GuiMsgQueue.Enqueue(msg);
Monitor.Exit(GuiMsgQueue);

if (guiUpdateThread.ThreadState ==
ThreadState.Suspended)
guiUpdateThread.Resume();
}

private void threadJob()
{
GUIMsg gMsg;

while (true)
{
if (GuiMsgQueue.Count 0)
{
Monitor.Enter(GuiMsgQueue);
gMsg = GuiMsgQueue.Dequeue();
Monitor.Exit(GuiMsgQueue);

update(gMsg);
}
else
{
if (guiUpdateThread.ThreadState ==
ThreadState.Running)
guiUpdateThread.Suspend();
}
}
}
Please help me..

推荐答案

7月4日上午10:27,cty0 ... @ gmail.com写道:
On Jul 4, 10:27 am, cty0...@gmail.com wrote:

我有一些问题..

这是m y第一次使用线程..

以下代码没有错误但是两个交战


战争是


警告2''System.Threading.Thread.Suspend()''已过时:

''Thread.Suspend已被弃用。请使用

System.Threading中的其他类,例如Monitor,Mutex,Event和Semaphore,以便

同步线程或保护资源。 http://go.microsoft.com/fwlink/?linkid=14202''


战争发生在线程简历/暂停......

我' '我不确定是什么问题因为线程编程是第一次

对我来说...
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...



正如它所说,你不应该'' t基本上使用Suspend / Resume。如果您需要一个

生产者/消费者队列(看起来像这样),您可以使用Auto /

ManualResetEvent或Monitor.Wait / Pulse / PulseAll。看到下半年


http://pobox.com/~skeet/csharp/threads/deadlocks.shtml 的例子。


你也不应该手动调用Monitor 。输入/退出 - 使用锁定

代替。


最后,制作一个UI线程块是一个坏主意 - 你不会

能够做一些事情,比如在窗口被阻挡时移动它。相反,

您应该异步工作或让另一个线程等待消息并调用Control.BeginInvoke或Control.Invoke使用

UI线程上的消息。


Jon

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
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.BeginInvoke or Control.Invoke to use
that message on the UI thread.

Jon


7月4日,6月31日,Jon Skeet [ C#MVP]" < s ... @ pobox.comwrote:
On 7 4 , 6 31 , "Jon Skeet [C# MVP]" <s...@pobox.comwrote:

7月4日上午10:27,cty0 ... @ gmail.com写道:
On Jul 4, 10:27 am, cty0...@gmail.com 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


警告2'' System.Threading.Thread.Suspend()''已过时:

''Thread.Suspend已被弃用。请使用

System.Threading中的其他类,例如Monitor,Mutex,Event和Semaphore,以便

同步线程或保护资源。 http://go.microsoft.com/fwlink/?linkid=14202''b
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...



如上所述,基本上你不应该使用Suspend / Resume。如果您需要一个

生产者/消费者队列(看起来像这样),您可以使用Auto /

ManualResetEvent或Monitor.Wait / Pulse / PulseAll。看到下半年

ofhttp://pobox.com/~skeet/csharp/threads/deadlocks.shtml为例。


你也不应该'' t手动调用Monitor.Enter / Exit - 使用锁

代替。


最后,制作一个UI线程块是一个坏主意 - 你赢了不能

能够做一些事情,比如在窗户被阻挡时移动窗户。相反,

您应该异步工作或让另一个线程等待消息并调用Control.BeginInvoke或Control.Invoke使用

UI线程上的消息。


Jon


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



我改变了代码作为你的推荐...

战争已经消失,但有时候会发生死锁... -_- ;;

死锁位置是addNewMsg函数的锁定(queueLock)......


我的代码和链接的代码之间只有一个区别是
http://pobox.com/~skeet/csharp/threads/deadlocks.shtml

是我的代码需要永远循环直到应用程序完成..

所以我在threadJob中添加while(true)...在每个

循环中休眠0.1秒...


你能查一下吗?我的代码..


我可以再询问一次吗?

我的应用程序运行一个M ain表格首先,按钮点击

主表格,然后开始接收表格,其中包括线程作业..


当我关闭主表格,然后应用程序没有完成..

当我调试..应用程序是等待Monitor.Wait(queueLock)在

threadJob函数永远

做你知道原因......


线程guiUpdateThread;

guiUpdateThread =新线程(新的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) ;

锁定(queueLock)

{

而(GuiMsgQueue.Count == 0)

Monitor.Wait(queueLock);


gMsg = GuiMsgQueue.Dequeue();

update(gMsg);

}

}

}


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);
}
}
}


ct ***** @ gmail.com 写道:

我改变了代码作为你的推荐...

战争已经消失,但有时候会发生死锁...... -_- ;;

死锁位置是addNewMsg函数的锁定(queueLock)...


我的代码与链接的代码之间只有一个区别
http://pobox.com/~skeet/csharp/threads/deadlocks.shtml

是我的代码需要永远循环直到应用程序完成..

所以我在threadJob中添加while(true)...在每个

循环中休眠0.1秒...


你能查一下我的代码..


还可以再询问一次吗?

我的应用程序首先运行一个主窗体,然后单击

主窗体,然后启动接收窗体,其中包括线程作业..


当我关闭时主窗体,然后应用程序没有完成..

当我调试..应用程序正在等待Monitor.Wait(queueLock)在

threadJob函数永远

你知道原因吗...


线程guiUpdateThread;

guiUpdateThread =新线程(新的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);
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);



哎呀。你在锁内部有一个循环,等待进入队列的某些东西,但是将队列放入队列的唯一代码也是

a lock,所以这将是永远不会发生此线程将在此循环中等待
,在执行此操作时以100%CPU运行。

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.Dequeue();

update(gMsg);

}

}

}
>
gMsg = GuiMsgQueue.Dequeue();
update(gMsg);
}
}
}



-

G?ran Andersson

_____
http://www.guffa.com


这篇关于c#线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆