- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在编写一个 API 来促进与串行端口的通信。我正在进行一些重构和一般清理,想知道是否有办法避免以下问题。
API 中的主类能够不断从端口读取数据,并在读取字节与特定正则表达式匹配时引发包含值的事件。读取和解析的过程发生在另一个线程上。该事件包含作为参数 (string
) 的值,并且因为它是从另一个线程引发的,所以客户端试图直接将值分配给 除非处理程序具有正确的
属性会导致跨线程异常。Invoke
代码,否则控件的 Text
我理解为什么会发生这种情况,当我将正确的调用代码放入测试客户端的事件处理程序时,一切都很好;我的问题是我是否可以在 API 代码本身中做任何事情,以便客户不必担心它。
基本上,我想把这个:
void PortAdapter_ValueChanged(Command command, string value)
{
if (this.InvokeRequired)
{
Invoke(new MethodInvoker(() =>
{
receivedTextBox.Text = value;
}));
}
else
{
receivedTextBox.Text = value;
}
}
简单地说:
void PortAdapter_ValueChanged(Command command, string value)
{
receivedTextBox.Text = value;
}
最佳答案
嗯,在 .Net 框架本身的许多地方使用了一个通用模式。例如 BackgroundWorker
使用此模型。
为此,您将使用 SynchronizationContext
作为 API 的参数,在本例中我假设它是 PortAdapter
。
引发事件时,您可以使用 SynchronizationContext.Post
或 SynchronizationContext.Send
在给定的 SynchronizationContext
中引发事件。前者是异步的,后者是同步的。
因此,当客户端代码创建您的 PortAdapter
实例时,它会将 WindowsFormsSynchronizationContext
实例作为参数传递。这意味着 PortAdapter
将在给定的同步上下文中引发事件,这也意味着您不需要 InvokeRequired
或 Invoke
调用。
public class PortAdapter
{
public event EventHandler SomethingHappened;
private readonly SynchronizationContext context;
public PortAdapter(SynchronizationContext context)
{
this.context = context ?? new SynchronizationContext();//If no context use thread pool
}
private void DoSomethingInteresting()
{
//Do something
EventHandler handler = SomethingHappened;
if (handler != null)
{
//Raise the event in client's context so that client doesn't needs Invoke
context.Post(x => handler(this, EventArgs.Empty), null);
}
}
}
客户端代码:
PortAdapter adpater = new PortAdapter(SynchronizationContext.Current);
...
在 UI 线程中创建 PortAdapter
的实例非常重要,否则 SynchronizationContext.Current
将为 null,因此事件仍将在 ThreadPool 线程中引发。
关于c# - 防止事件处理程序中 InvokeRequired 的必要性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26343132/
InvokeRequired 和 somecontrol.InvokeRequired 有什么区别? 像这样, delegate void valueDelegate(string value); p
这怎么可能?我有 Windows 窗体控件,从 System.Windows.Forms.Form 派生,WebBrowser 控件包含在此窗体中。 Webbrowser 对象实例是在表单的构造函数中
我有一个事件处理程序,我想在创建对象的原始线程中处理它,这样它就不会阻塞。使用表单,很容易使用 InvokeRequired 将其强制到原始线程。但是如果你的类不是表单,你怎么做呢? 谢谢, 下午 最
我知道您必须调用才能进行跨线程更新。但是,如果不需要 Invoke,您能否像需要 Invoke 时那样调用代码? 所以不是这个: if(rtbSearchResults.InvokeRequired)
这个问题在这里已经有了答案: What's wrong with calling Invoke, regardless of InvokeRequired? (6 个答案) 关闭 1 年前。 我的同
当我想在 Windows 窗体工作时使用委托(delegate)类进行调用时,我总是必须使用 InvokeRequired。没关系。但是谁在它工作时更改了 InvokeReuqired 属性。请检查这
UI 线程偶尔会在以下方法中的语句 'if (this.InvokeRequired)' 处挂起。 你能帮我找出问题的原因吗 public void OnModuleInitializationC
我一直在编写一个 API 来促进与串行端口的通信。我正在进行一些重构和一般清理,想知道是否有办法避免以下问题。 API 中的主类能够不断从端口读取数据,并在读取字节与特定正则表达式匹配时引发包含值的事
我想让我的 getter 线程安全。当我这样做时,出现错误: public ApplicationViewModel SelectedApplication { get
我来找你是想看看是否有人知道如何解决我在迁移到 ActiveMQ 时遇到的问题。我在这个项目中使用 ActiveMQ 发送通知(在 C# 中),在完成实现后我发现了一些关于线程问题的错误。 (我知道该
这是我的代码: foreach (var pathCartella in folderList) { try { // some operation i
我一直在寻找这个问题的答案,但似乎找不到满意的答案。也许这里有人可以启发我。 我有一个 BindingList 的后代存储对 SynchronizationContext 的引用对象以便在 UI 线程
我有一个 UserControl,上面有一个名为 mTreeView 的 TreeView 控件。我可以从多个不同的线程获取数据更新,这些会导致 TreeView 被更新。为此,我设计了以下模式:所有
这个问题在这里已经有了答案: Invoke or BeginInvoke cannot be called on a control until the window handle has been
在我的应用程序中,我有一个负责所有数据库操作的类。它从主类调用,并在操作完成后使用委托(delegate)调用方法。因为它是异步的,所以我必须在我的 GUI 上使用 invoke,所以我创建了一个简单
我是一名新手程序员,所以我在这里可能完全错了,但这个问题比它应该的更让我烦恼。 这实际上是 this 的跟进问题。 公认的答案是,您必须调用 InvokeRequired 以避免一些开销,因为您有可能
这个问题在这里已经有了答案: What's wrong with calling Invoke, regardless of InvokeRequired? (6 个答案) 关闭去年。 我知道,当从
我已经痛苦地意识到需要在事件驱动的 GUI 代码中编写以下代码模式的频率,其中 private void DoGUISwitch() { // cruisin for a bruisin' t
从.NET 4.0开始,TPL可以执行异步任务。如果您正在阅读msdn,则所有与窗体/UI交互的异步操作仍将使用InvokeRequire ... Invoke()模式。 我要问的是有原因吗?据我了解
首先,我在 Window.Forms 开发方面经验不足。但是我发现 InvokeRequired 检查,对于控件,在线程应用程序中使用时有点乏味。我创建了一个静态方法,我认为它可以解决我乏味的 Inv
我是一名优秀的程序员,十分优秀!