C#多个来源,不同的线程,一个事件处理程序 [英] C# multiple sources, different threads, one event handler

查看:111
本文介绍了C#多个来源,不同的线程,一个事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在线程和事件提高高技能的人。

I need somebody with high skills in threading and event raising.

我有一个抽象类 A 和两个具体的类 C1 C2 (如插件)。

I have an abstract class A and two concrete classes C1, C2 (e.g plugins).

因为我需要他们彼此之间的沟通,像插件的应用,插件,插件沟通,我有一个方法 ExecuteCommand 中的抽象类,应该做到这一点。这个函数抛出一个事件应用程序以处理某个命令,并返回结果(例如,如果一个插件,从它调用的应用程序需要的数据 ExecuteCommand 从基地,并等待其自带的应用程序处理的事件处理程序)的结果。

Since I need them to communicate between each other, like "plugin-application" "plugin-plugin" communication, I have a method ExecuteCommand in the abstract class which should accomplish this. This function raises an event to the application in order to process a certain command and return the result (e.g if one plugin needs data from the app it calls ExecuteCommand from the base and waits for the result which comes with the event handler processed on the application).

protected object ExecuteCommand(SvcCmdType cmdType, params object[] inputParams)
{
  // this code has been simplified
  SvcCommandEventArgs eventArgs = new SvcCommandEventArgs(cmdType, inputParams);

  // generate processing command event (it requires to fill in the result)
  OnProcessingAppCommand(this, eventArgs);

  return eventArgs.OutputParamsList; 
}



问题是:

The problem is:

如果 C1中的每一个 C2 有不同的线程的背后,并调用同时 ExecuteCommand 从自己的线程内,则肯定我的设计将被打破,并返回的结果将无法预料。

If each one of C1 and C2 have different threads behind and call simultaneously ExecuteCommand from inside their own threads then for sure my design will be broken and the result returned will be unexpected.

什么是这种情况的最佳设计?我想在里面 ExecuteCommand 异步调用使用像使用 AsyncOperation ...但它是正确的方式?

What is the best design for this scenario? I was thinking to use inside ExecuteCommand asynchronous calls like using AsyncOperation... but is it the right way?

编辑:
我想我在寻找:是我的方案的同步或异步的方式更好?或者,我应该有一个插件的线程中处理或在我的主线程某处同步的应用程序事件处理程序?

edited: I guess I am looking for: is the synchronous or asynchronous way better for my scenario? Or, shall I have the app event handler processed inside a plugin's thread or synchronized somewhere in my main thread?

我真的很感激一些很好的解释为您推荐

I would really appreciate some good explanations for your recommendations

感谢您。

推荐答案

在做线程同步的常用的简单方法公共资源或代码块是使用互斥(或者,在这种情况下,临界区)。使用lock语句:

The usual simple method of doing thread synchronization on a common resource or code block is to use a mutex (or, in this case, a critical section). Use the lock statement:

http://msdn.microsoft.com/en-us/library/c5kehkcz(VS.71)的.aspx

这文章说的这个指针做锁定,但可能是危险的,因为外部用户也可以获取同样的锁,这可能会破坏你的程序。 。做你的私人类变量锁定

This article says to do locking on the "this" pointer, but that can be dangerous, as outside callers could also acquire the same lock, which could break your program. Do your locking on a private class variable.

下面是你的示例代码合并锁定/关键部分做一些修改:

Here is some modification of your example code to incorporate locking/a critical section:

class SomeClass : ISomeInterface
{
  protected object ExecuteCommand(SvcCmdType cmdType, params object[] inputParams)
  {
    lock(executeCommandLock)
    {
      SvcCommandEventArgs eventArgs = new SvcCommandEventArgs(cmdType, inputParams);
      OnProcessingAppCommand(this, eventArgs);
      return eventArgs.OutputParamsList; 
    }
  }

  private Object executeCommandLock = new Object();
}



编辑:

(从评论意译)。你提到,你可能要处理在单个线程的所有调用ExecuteCommand,异步。您可以与调度类来实现这一点:

(paraphrasing from comments). You mentioned that you might want to process all calls to ExecuteCommand on a single thread, asynchronously. You may be able to accomplish this with the Dispatcher class:

http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.aspx

在一个线程获取到调度员参考。传递参照其他线程。当这些线程想调用ExecuteCommand,他们使用dispatcher.BeginInvoke。由于他们使用的BeginInvoke,以ExecuteCommand所有来电然后将异步操作,并在该线程不会阻塞。然而,ExecuteCommand的每个版本将排队,并依次运行调度程序线程。

Get a reference to the dispatcher on one thread. Pass that reference to the other threads. When those threads want to call ExecuteCommand, they use dispatcher.BeginInvoke. Since they use BeginInvoke, all calls to ExecuteCommand will then operate asynchronously, and not block on that thread. However, each version of ExecuteCommand will be queued up, and run sequentially dispatcher thread.

这篇关于C#多个来源,不同的线程,一个事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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