[英]Need help with weird Begin/EndInvoke issue which Causes “Thread Leak”
我得到了以〜1000ms速率执行的以下代码。 放任不管,它会导致奇怪的“线程泄漏” 。 使用WindDBG / SoS,我可以看到很多ThreadPool Worker线程(其中有些被标记为已死),最终我将获得AccessVioalation Exception 。 谁能告诉我我的BeginInvoke / EndInvoke使用是否错误,是否有不必要的锁定...任何提示都将对我有帮助...好吧,目前为止毫无提示
RichTextBox tmpBox = txtIncomingData;
lock (m_TextUpdateSynch) {
try {
result = Utilities.SafeBeginInvoke(this, delegate() {
try {
if (tmpBox.Text.Length > BufferSize) {
tmpBox.Text = rawData;
}
else {
tmpBox.AppendText(rawData);
}
pageBottom(txtIncomingData);
}
catch (...) {}
});
this.EndInvoke(result);
}
public static IAsyncResult Utilities.SafeBeginInvoke(System.ComponentModel.ISynchronizeInvoke control,
ControlUpdate action, AsyncCallback callback,
params object[] args) {
IAsyncResult result = null;
Control uiControl = control as Control;
try {
result = control.BeginInvoke(action, args);
}
catch (...) { }
return result;
}
看来您的代码正在使用WPF,据我了解,在WPF中,BeginInvoke将编组您的调用;正在对UI(调度程序)线程进行调用; 因此据我所知,它不应创建任何额外的线程。
http://msdn.microsoft.com/en-us/library/ms591206.aspx
我也注意到在BeginInvoke调用周围有try / catch对。 对我来说,这表明您一直在抛出异常,并且我建议,扎根于它们的根源可能比抑制它们更好。 例如,您似乎正在引用三个变量-BufferSize,rawData和txtIncomingData,这些变量在锁之外定义-如果任何其他代码引用了它们并在不同的(非UI)线程上对其进行了修改,则可能会导致您的问题。
最后,我认为您列出的SafeBeginInvoke的重载不是代码所调用的重载-列出的一个包含4个参数(尽管一个是params),您调用的一个带有2个参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.