C# 非法的跨线程操作异常,无法处理
我正在使用visual C#express 2008。当我试图运行我的项目时,我有时会在invoke方法中得到非法的跨线程操作。它在许多地方使用相同的invoke方法,但只有在另一个线程的开始时我才得到错误。我检查InvokeRequired属性,在“else”条件下调用相同的方法,创建一个临时变量,并将其分配给控件的文本。但是在那一行,在'else'语句中,在调用方法的内部,我有时会得到异常。原因可能是什么?如何摆脱它?它并不经常发生,但仍然是一个bug 代码:C# 非法的跨线程操作异常,无法处理,c#,multithreading,C#,Multithreading,我正在使用visual C#express 2008。当我试图运行我的项目时,我有时会在invoke方法中得到非法的跨线程操作。它在许多地方使用相同的invoke方法,但只有在另一个线程的开始时我才得到错误。我检查InvokeRequired属性,在“else”条件下调用相同的方法,创建一个临时变量,并将其分配给控件的文本。但是在那一行,在'else'语句中,在调用方法的内部,我有时会得到异常。原因可能是什么?如何摆脱它?它并不经常发生,但仍然是一个bug 代码: TY问题似乎是您试图从非UI线
TY问题似乎是您试图从非UI线程访问Windows控件属性。相反,请尝试使用Invoke从控件获取文本长度。问题似乎是您试图从非UI线程访问Windows控件属性。相反,尝试使用Invoke从控件获取文本长度。类似于但不一定是的副本:类似于但不一定是的副本:
// Delegate:
private delegate void ChangeTextDelegate(string text);
// Method:
public static void ChangeText(string text)
{
if (richtextbox1.InvokeRequired)
{
richtextbox1.Invoke(new ChangeTextDelegate(ChangeText), new object[] { text });
}
else
{
int startIndex;
startIndex = richtextbox1.TextLength; // <- Exception points here.
// ...
}
}
System.InvalidOperationException was unhandled
Message="Cross-thread operation not valid: Control 'SomeClass' accessed from a thread other than the thread it was created on."
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.get_InternalHandle()
at System.Windows.Forms.Control.WmWindowPosChanged(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
at System.Windows.Forms.RichTextBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmCreate(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
at System.Windows.Forms.RichTextBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.IntCreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
at System.Windows.Forms.UnsafeNativeMethods.CreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
at System.Windows.Forms.Control.CreateHandle()
at System.Windows.Forms.TextBoxBase.CreateHandle()
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.RichTextBox.get_TextLength()
at SomeNamespace.SomeClass.Changetext(String text, Color color) in C:\...\SomeClass.cs:line 827
at SomeNamespace.SomeClass.SomeThreadFun() in C:\...\SomeClass.cs:line 112
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: