BeginInvoke、EndInvoke 的多线程问题?

发布于 2024-11-24 06:44:26 字数 1229 浏览 4 评论 0原文

我有一个显示实时值的客户端应用程序。这些值通过 DDE-Advise 提供。这些实时值是数控机床的移动轴。因此,每分钟大约有 100 个建议通过此 DdeClientAdvise 方法传入。
当应用程序获得许多 DDE-Advises 时,似乎突然所有 adivses 都丢失了。
我将问题简化为以下内容:

public class NcddeZugriff
{
  private DdeClient _ddeClient; //see http://ndde.codeplex.com/

  public NcDdeZugriff()
  {
    _ddeClient = new DdeClient("ncdde", "machineswitch");
    _ddeClient.Connect();
    _ddeClient.Advise += DdeClientAdvise;
  }

  private delegate void CallbackDelegate(object sender, DdeAdviseEventArgs e);    

  private void DdeClientAdvise(object sender, DdeAdviseEventArgs e)
  {
    CallbackDelegate callbackDelegate = DdeClientAdviseCallback;
    _logging.InfoFormat("Advise-Callback for {0}", e.Item);
    //LINE A : return;

    callbackDelegate.BeginInvoke(sender, e, callbackDelegate.EndInvoke, null);
  }

  private void DdeClientAdviseCallback(object sender, DdeAdviseEventArgs e)
  {
    _logging.InfoFormat("Asynchron for {0}", e.Item);
    //do some work with e.Text...
  }
}

如果我删除注释 LINE A,则一切正常,不会丢失任何建议。所有建议均已记录。
如果我启用 BeginInvoke,一段时间后,DdeClientAdvise 方法将不再被调用,也不再有日志条目。

我对 BeginInvoke、EndInvoke 做错了什么?

编辑:添加一些有关班级的更多信息。

I have a client application, that displays realtime values. The values are provided through a DDE-Advise. These realtime values are moving axis of a cnc-machine. So there are about 100 advises per minute comming in through this DdeClientAdvise-Method.
When the application is getting many DDE-Advises it seems that suddenly all adivses are lost.
I reduced the problem to the following:

public class NcddeZugriff
{
  private DdeClient _ddeClient; //see http://ndde.codeplex.com/

  public NcDdeZugriff()
  {
    _ddeClient = new DdeClient("ncdde", "machineswitch");
    _ddeClient.Connect();
    _ddeClient.Advise += DdeClientAdvise;
  }

  private delegate void CallbackDelegate(object sender, DdeAdviseEventArgs e);    

  private void DdeClientAdvise(object sender, DdeAdviseEventArgs e)
  {
    CallbackDelegate callbackDelegate = DdeClientAdviseCallback;
    _logging.InfoFormat("Advise-Callback for {0}", e.Item);
    //LINE A : return;

    callbackDelegate.BeginInvoke(sender, e, callbackDelegate.EndInvoke, null);
  }

  private void DdeClientAdviseCallback(object sender, DdeAdviseEventArgs e)
  {
    _logging.InfoFormat("Asynchron for {0}", e.Item);
    //do some work with e.Text...
  }
}

If I remove comment LINE A, everything works fine, no advise got lost. All the advises are being logged.
If I enable the BeginInvoke, after awhile the DdeClientAdvise-Method is not being called anymore, no more log-entries.

What am I doing wrong with BeginInvoke, EndInvoke?

Edit: Add some more Information about the class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

澉约 2024-12-01 06:44:26

您不需要在 DdeClientAdviseCallback 中调用 EndInvoke 吗?

don't you have to call EndInvoke inside DdeClientAdviseCallback?

雾里花 2024-12-01 06:44:26

看来@Hans Passant 是对的:代表正在收集垃圾。将委托存储在字段中似乎可以解决问题。
虽然我改变了整个项目的设计。所以我不能肯定地说这解决了问题。

It seems like @Hans Passant was right: the delegate was getting garbage collected. Storing the delegate in a field seems to solve the issue.
Although I changed the design of the whole project. So I can't say for sure, that this solved the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文