Thread.Suspend()和Thread.Resume()的替代方法; [英] Alternate way for Thread.Suspend() and Thread.Resume();

查看:559
本文介绍了Thread.Suspend()和Thread.Resume()的替代方法;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
在我的工作中,我使用线程从串行端口读取然后插入数据库.有时,我需要暂停此操作,然后继续执行操作……我以前使用Thread.Suspend();Thread.Resume();来暂停和恢复线程完成的工作.

但是现在在编译时它说Thread.Suspend();Thread.Resume();已过时且已弃用.

如何不使用Thread.Suspend();Thread.Resume();
来完成我的工作
有什么办法可以暂停和继续工作..

Hello,
In my work I use a thread to read from a serial port and then insert into database. Sometimes i need to pause this operation and after that continue it... I previously used Thread.Suspend(); And Thread.Resume(); to suspend and resume the work done by the thread.

But Now while compiling it says Thread.Suspend(); And Thread.Resume(); are obsolete and deprecated.

How can i do my work without using Thread.Suspend(); Or Thread.Resume();

Is there any way to suspend and resume work..??

推荐答案

你好

看这个:

http://msdn.microsoft.com/en-us/library/system.threading. eventwaithandle.aspx [ ^ ]

并注意EventWaitHandle.WaitOne

http://msdn.microsoft.com/en-us/library/system. threading.eventwaithandle.waitone.aspx [
Hello

Look at this:

http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle.aspx[^]

And pay attention to EventWaitHandle.WaitOne

http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle.waitone.aspx[^]


我想补充一下Shahin的正确答案.如果没有线程挂起的问题,请使用WaitOne.如果未设置事件等待句柄的实例,则调用线程将以特殊的等待状态等待. OS会关闭调用线程,并且直到由于某种原因唤醒"线程才将其调度回执行.在这种状态下,等待线程浪费零CPU时间.

等待线程可以在不同条件下唤醒.主要条件是在其他一些线程中设置相同的偶数句柄的实例.这是此线程同步API的主要目的.但是还有其他条件:过期超时(如果已应用)或从其他某个线程调用等待线程的Thread.Abort.这样,等待线程就不会阻止整个进程终止,也不会发生任何其他不良事件.

—SA
I would like to add to the correct answer by Shahin. Using WaitOne if free from the problems of thread suspensions. If an instance of an event wait handle is not set, the calling thread waits in a special wait state. OS switches the calling thread off and do not schedule it back to execution until it is "awaken" by some reason. In this state, the waiting thread wastes zero CPU time.

The waiting thread can be awaken on different conditions. The primary condition is setting the instance of the same even handle in some other thread; which is the main purpose of this thread synchronization API. But there are other conditions: expired timeout (if applied) or calling Thread.Abort for the waiting thread, from some other thread. This way, there is no danger that the waiting thread blocks the whole process from termination and any other bad thing.

—SA


不推荐使用这些方法是有原因的.它可能会使用Thread.Suspend对您的AppDomain造成破坏.请在此处阅读有关信息: http://msdn.microsoft.com/en-us/library/tttdef8x. aspx [ ^ ].我不知道您到底想达到什么目标,所以也许您最好先解释一下自己的目标.使用问题下方的改进问题"链接进行编辑并添加必要的详细信息.

问候,

曼弗雷德(Manfred)
These methods are deprecated for a reason. It can wreak havoc on your AppDomain using Thread.Suspend. Please read about it here: http://msdn.microsoft.com/en-us/library/tttdef8x.aspx[^]. I don''t know what exactly you are trying to achieve, so maybe you''d better start by explaining what you are driving at. Use the link "Improve question" beneath your question to edit it and add the nescessary details.

Regards,

Manfred


这篇关于Thread.Suspend()和Thread.Resume()的替代方法;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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