1
我正在使用Visual C#express 2008.我有時在我的調用方法中獲取非法跨線程操作,當我嘗試運行我的項目。它在許多地方使用相同的invoke方法,但它只在另一個線程的開始處出現錯誤。我檢查InvokeRequired屬性,調用相同的方法並在'else'條件中創建一個臨時變量併爲其分配我的控件文本。但在這一行上,在'else'語句中,在調用方法中,我有時會得到異常。可能是什麼原因?如何擺脫它?它不經常發生,但它仍然是一個錯誤。非法跨線程操作異常,無法處理它
代碼:
// 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:
TY
類似但不一定重複:http://stackoverflow.com/questions/1954140/getting-cross-thread-operation-not-valid-even-when-using-invoke-method – 2010-03-31 16:55:33