跨线程边界传输数据 [英] Transfer data across thread borders

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

问题描述

您好,专家,

这是用于半自动硬件设备测试.用户将要测试的设备(被测试者")放置在设备中.
然后用户点击一个按钮.这是此上下文中的唯一按钮.它不在键盘上,而是属于设备.正在处理按钮命中,并最终将其作为事件到达应用程序.

应用程序核心是一系列任务.最简单的方法是使用诸如

Hi experts,

this is for a semi-automatic hardware device test. User places the device to be tested ("the testee") within the apparatus.
Then User hits a button. It is the only button within this context. It''s not on the keyboard but belongs to the apparatus. The button hit is being processed and finally reaches the application as an event.

The application core is a sequence of tasks. The easiest way to accomplish that would be to have some statements like

Task0();
WaitForUserButton();
Task1("Foo", "Bar");
Task2(24);
WaitForUserButton();
Task3();


有趣的部分是WaitForUserButton().
在执行下一条语句之前,用户必须与被测试者打交道,然后按其按钮声明该序列可以继续.

为了管理序列的连续,我使用了 System.Threading.AutoResetEvent [


The interesting part is WaitForUserButton().
Prior to executing the next statement(s), User has to deal with the testee and then press his button to announce that the sequence may continue.

To manage the block-and-continue of the sequence, I used System.Threading.AutoResetEvent[^] this way:

using System.Threading;
private AutoResetEvent _autoResetEvent = new AutoResetEvent(false);

void MyBase_ExternalButtonPressed()
{
    _autoResetEvent.Set();
}

private void WaitForUserButton()
{
    MyBase.ExternalButtonPressed += MyBase_ExternalButtonPressed;

    _autoResetEvent.WaitOne(TimeSpan.FromMilliseconds(100));

    MyBase.ExternalButtonPressed -= MyBase_ExternalButtonPressed;
}



现在,我需要对具有此类事件处理程序签名的另一个事件做出反应:



Now I need to react on another event with such an event handler signature:

public delegate void ParameterPackedDelegate(int foo);

我只是不明白将该foo值转换为WaitForSomeEvent().

如何将数据附加到AutoResetEvent.Set()?

I just don''t see how to get that foo value into WaitForSomeEvent().

How can I attach data to AutoResetEvent.Set()?

推荐答案

您不能将任何数据附加到EventWaitHandle.我认为您甚至不知道等待句柄的目的.您的代码是事件线程同步原语的奇怪混合物,并且似乎一点也不合理.在事件实例的调用列表上使用-="运算符通常是一个坏主意.您应该记住的一件事是.NET事件和事件等待句柄几乎没有任何共同点,它们是完全不同且无关的概念.

由于您并未真正解释此活动的目标,因此很难为您提供帮助.

也许您会从我过去的解决方案中得到一些想法,以回答其他问题.

事件等待句柄:
ManualResetEvent和Thread中的AutoResetEvent [暂停正在运行的线程 [用于线程通信和线程间调用的简单阻止队列 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

—SA
You cannot attach any data to EventWaitHandle. I think you have no idea about the purpose of even wait handles. You code is a strange mixture of events thread synchronization primitives and and does not seem reasonable at all. Using "-=" operators on the invocation list of an event instance is usually a bad idea. One thing you should keep in mind is that .NET events and event wait handles have nearly nothing in common, they are completely different and unrelated concepts.

It''s hard to help you as you did not really explain the goal of this activity.

Perhaps you will get some idea from my past solutions in response to other questions.

On event wait handles:
ManualResetEvent and AutoResetEvent in Thread[^],
pause running thread[^].

Using them for thread communication and synchronization with data — my Tips & Trick article where I introduce the blocking queue, with full source code and sample of the usage: Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

See also more references on threading, my collections of links to my past solutions:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于跨线程边界传输数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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