简体   繁体   English

C# - 阻塞主线程直到从线程执行 ManualResetEvent.WaitOne()

[英]C# - Block master-thread until slave-thread executed ManualResetEvent.WaitOne()

I've a question regarding thread handling in C#:我有一个关于 C# 中的线程处理的问题:

I have a master thread who creates a slave thread with我有一个主线程创建一个从线程

pollthread = new Thread(doPollThread);
pollthread.Start(this);

I have a我有一个

ManualResetEvent resetevent

the slave thread executes an endless loop like从线程执行一个无限循环,如

private static void doPollThread(object thisObject){

    SpeedCounterDevice sc = (SpeedCounterDevice) thisObject;
    while (true) {

        sc.resetevent.WaitOne();

        //do some work ....

        Thread.Sleep(400);

    }
}

The purpose of the slave thread is to poll an external device through a network socket (how are you .. how are you ... get measurement result ... how are you ....) but this socket is created in functions of the master thread And I want to connect the socket through an UI-Button-Press, disconnect through the UI, connect again, .... in the master thread So control of the socket should belong to the master thread, only the actual data transfer over the network should be done by the slave从线程的目的是通过网络套接字轮询外部设备(你好吗……你好吗……获取测量结果……你好吗……)但是这个套接字是在以下函数中创建的主线程我想通过UI-Button-Press连接套接字,通过UI断开连接,再次连接,....在主线程中所以套接字的控制应该属于主线程,只有实际数据通过网络传输应该由从机完成

Now I want to suspend the slave thread in a function running in the master thread like现在我想在主线程中运行的函数中暂停从线程,例如

resetevent.Reset();

to disconnect the network connection later on when the slave loop is finished稍后在从属循环完成时断开网络连接

But then I want to wait in the master thread (block it) until the slave thread executed WaitOne(), but without any possibility to come into a deadlock or something else problematic ... (I want to finish the loop without interrupt it)但是然后我想在主线程中等待(阻塞它),直到从线程执行 WaitOne(),但没有任何可能陷入死锁或其他问题......(我想完成循环而不中断它)

Yes, you can create another ResetEvent, block the master thread, but if the slave is getting the resetEvent right between signalling the master thread and executing WaitOne(), he will sleep but will not wake up the master thread ...是的,您可以创建另一个 ResetEvent,阻塞主线程,但如果从属设备在向主线程发出信号和执行 WaitOne() 之间正确获取了 resetEvent,他将休眠但不会唤醒主线程......

Is there any safe nice way to solve this problem?有什么安全的好方法来解决这个问题吗?

Thank you!谢谢!

Regards, Hans Juergen问候, 汉斯于尔根

等待从机完成使用pollthread.join()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ManualResetEvent.WaitOne卡住了GUI - ManualResetEvent.WaitOne stuck the GUI ManualResetEvent.WaitOne()导致AccessViolation - ManualResetEvent.WaitOne() causes AccessViolation ManualResetEvent WaitOne阻止我的CollectionView的所有者Thread - ManualResetEvent WaitOne blocks the owner Thread of my CollectionView 使用BackgroundWorker的ManualResetEvent:WaitOne()时当前线程忙吗? - ManualResetEvent with a BackgroundWorker : Current thread is busy while WaitOne()? ManualResetEvent.WaitOne()返回false而不超时? - ManualResetEvent.WaitOne() returning false without timeout? C#使用ManualResetEvent启动停止线程 - C# Start Stop Thread with ManualResetEvent 如何继续前进之前等待ManualResetEvent.WaitOne()到达? - How to wait for ManualResetEvent.WaitOne() to be reached before moving on? ManualResetEvent.WaitOne始终挂在ASP.NET MVC中 - ManualResetEvent.WaitOne is always hanging in ASP.NET MVC ManualResetEvent - WaitOne()似乎在某些时候没有释放线程 - ManualResetEvent - WaitOne() does not seem to release thread at some point ManualResetEvent.WaitOne()抛出NullReferenceException:对象引用未设置为对象的实例 - ManualResetEvent.WaitOne() throws NullReferenceException: Object reference not set to an instance of an object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM