我正在编写一个dll来捕获vc ++中的端口口袋(如tcp sniffer)。此应用程序使用一个线程连续检索口袋,并有一个事件将数据发送到c#
应用程序。
此c#
个应用程序接收该口袋数据,并每隔10秒将该数据存储到richtextbox中的.txt文件中,然后清除文本框。
它正在运行,但有时会引发空引用异常。
错误:
at System.Windows.Forms.RichTextBox.EditStreamProc(IntPtr dwCookie, IntPtr buf, Int32 cb, Int32& transferred)
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.WndProc(Message& m)
at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
at System.Windows.Forms.RichTextBox.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
答案 0 :(得分:0)
将更新例程更改为:
debugText.SuspendLayout();
debugText.Focus();
debugText.SelectionStart = 0;
debugText.SelectionLength = 0;
debugText.SelectedText += message;
debugText.SelectedText += Environment.NewLine;
debugText.ResumeLayout();
每次获得焦点是解决崩溃的原因。
答案 1 :(得分:0)
我认为这可能是因为您正在从不是在其上创建的线程访问控件,而您却不安全地进行了操作。这样做:
form.Invoke(new ThreadStart(delegate {
//update your RichTextBox here.
}));