等待方法完成? [英] Wait for method to complete?

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

问题描述

我只是想知道是否有更好的方法可以解决这个问题。我正在同时发送两个数据包但由于某种原因,只有一个客户端可以在其余客户端拒绝接收数据的同时接收两个数据包。通过添加Thread.Sleep(1)它工作得很好但只是想听听你是否有更好的方法来解决它或我应该保持这样吗?



发送(fs.GetFileCheckList(), 1 ); 
Thread.Sleep( 1 );
发送(FileHashes, 0 );

_buffer = new byte [ 1024 ];
client.BeginReceive(_buffer, 0 ,_ buffer.Length,SocketFlags.None, new AsyncCallback (ReceiveCallback),client);

解决方案

为此,不仅睡觉不是更好的方式;这是完全不可接受的,因为它导致竞争条件 http://en.wikipedia.org/wiki/Race_condition [ ^ ]。



如果它适合你的事件,这只是一个偶然的问题。它不是一种合法的线程同步方式。睡眠有它的用途,但这超出了这个问题。暂时忘掉吧。



如果你真的需要一个线程来等待另一个线程,你可以使用类 System.Threading.ManualResetEvent ,它只是一种具有专门构造函数的其他类, System.Threading.EventWaitHandle ,您也可以使用 System.Threading.EventResetMode.ManualReset

http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent%28v=vs.110%29.aspx [<一个href =http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent%28v=vs.110%29.aspxtarget =_ blanktitle =New Window> ^ ],

http ://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle(v = vs.110).aspx [ ^ ]。



我希望你能弄清楚如何从这个MSDN文档中使用它。当一个线程调用 EventWaitHandle.WaitOne 时,它会被置于一个特殊的等待状态,这会浪费没有CPU时间:调用线程被关闭,从不安排回执行直到它被唤醒,这可以通过中止,超时等条件来完成,更重要的是,来自其他线程的调用,它发出相同的 EventWaitHandle 调用 EventWaitHandle.Set



-SA

Hi, I'm just wondering if there is a better way of fixing this problem that I have. I'm sending two packets at the same time but for some reason only one client can receive both packets just fine at the same time the rest of the clients refuse to receive data. By adding the Thread.Sleep(1) it works just fine but just wanted to hear from you if there is a better way to solve it or should I keep it like this?

Send(fs.GetFileCheckList(), 1);
Thread.Sleep(1);
Send(FileHashes, 0);

_buffer = new byte[1024];
client.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), client);

解决方案

For this purpose, not only sleeping is not a "better way"; it is totally unacceptable because it causes race condition: http://en.wikipedia.org/wiki/Race_condition[^].

Event if it works for you, it's just a matter of chance. It is not a legitimate way of thread synchronization. Sleeping has its uses, but this is beyond this question. Forget it for now.

If you really need one thread to wait for another one, you can use the class System.Threading.ManualResetEvent, which is merely a kind of the other class with specialized constructor, System.Threading.EventWaitHandle, which you also can use with System.Threading.EventResetMode.ManualReset:
http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle(v=vs.110).aspx[^].

I hope you can figure out how to use it from this MSDN documentation. When one thread calls EventWaitHandle.WaitOne, it is put to a special wait state which wastes no CPU time: the calling thread is switched off and never scheduled back to execution until it is waken up, which can be done by such conditions as abort, timeout, and, importantly, the call from other thread, which signals the same instance of EventWaitHandle with the call to EventWaitHandle.Set.

—SA


这篇关于等待方法完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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