从backgroundworker调用控件停止,没有任何错误
本文关键字:任何 错误 backgroundworker 调用 控件 | 更新日期: 2023-09-27 18:26:08
我正在运行一个BackgroundWorker
,它假设要更新我的UserControl
。我在检查InvokeRequired
属性后尝试调用:
private void bgwHighlightText_DoWork(object sender, DoWorkEventArgs e)
{
tmpRich.SelectedRtf = myRtf;
if (_ucResultRich.InvokeRequired && _ucResultRich.rchResult.InvokeRequired)
_ucResultRich.Invoke(new Action(() => _ucResultRich.rchResult.Rtf = tmpRich.Rtf)); // Debug pointer stops here
//Rest of the code
}
我还尝试直接调用UserControl
中的RichTextBox
:
_ucResultRich.rchResult.Invoke(new Action(() => _ucResultRich.rchResult.Rtf = tmpRich.Rtf));
但当我调试代码时,它只是停止运行其余的代码,没有任何错误。
_ucResultRich.InvokeRequired
和_ucResultRich.rchResult.InvokeRequired
都返回true
。
我是不是做错了什么?
更新
我把Invoke
部分放在try catch
中,现在我可以从异常消息中得到以下错误:
Cross-thread operation not valid: Control '' accessed from a thread
other than the thread it was created on.
是因为它不能确定控制吗?因为它显示它像Control ''
。
使用其他线程更新UI线程上的控件时,需要调用委托而不是Action。
你可以使用我的通用方法来实现这一点:
public delegate void SetControlPropertyDelegateHandler(Control control, string propertyName, object value);
public static void SetControlProperty(Control control, string propertyName, object value)
{
if (control == null)
return;
if (control.InvokeRequired)
{
SetControlPropertyDelegateHandler dlg = new SetControlPropertyDelegateHandler(SetControlProperty);
control.Invoke(dlg, control, propertyName, value);
}
else
{
PropertyInfo property = control.GetType().GetProperty(propertyName);
if (property != null)
property.SetValue(control, value, null);
}
}
你可以这样使用这个方法:
SetControlProperty(_ucResultRich.rchResult, "Rtf", tmpRich.Rtf);
更新
您可以使用此方法在控件上调用无参数方法:
public static void CallMethodUsingInvoke(Control control, Action methodOnControl)
{
if (control == null)
return;
var dlg = new MethodInvoker(methodOnControl);
control.Invoke(dlg, control, methodOnControl);
}
示例:
CallMethodUsingInvoke(richTextBox1, () => _ucResultRich.rchResult.SelectAll());
若要调用更复杂的方法,必须为需要调用的方法创建适当的委托。
更新2
要从其他线程的属性中获取值,可以使用以下方法:
public delegate object GetControlPropertyValueDelegate(Control controlToModify, string propertyName);
public static T GetControlPropertyValue<T>(Control controlToModify, string propertyName)
{
if (controlToModify.InvokeRequired)
{
var dlg = new GetControlPropertyValueDelegate(GetControlPropertyValue);
return (T)controlToModify.Invoke(dlg, controlToModify, propertyName);
}
else
{
var prop = controlToModify.GetType().GetProperty(propertyName);
if (prop != null)
{
return (T)Convert.ChangeType(prop.GetValue(controlToModify, null), typeof(T));
}
}
return default (T);
}
示例:
var textLength = GetControlPropertyValue<int>(_ucResultRich.rchResult, "Length");