BeginInvoke、EndInvoke 的多线程问题?
我有一个显示实时值的客户端应用程序。这些值通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要在 DdeClientAdviseCallback 中调用
EndInvoke
吗?don't you have to call
EndInvoke
inside DdeClientAdviseCallback?看来@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.