GUI Threads
-
Hi! Can somebody send to me code of GUI thread written in C#? How correctly stop bckground thread? For current moment I found only such solution, but it often raise strange Exceptions... I use such code in async progress dialog... but at moment when I say progress form to Finish his work it can raise to me exception, like: ************** Exception Text ************** System.Threading.ThreadAbortException: Thread was being aborted. at System.Windows.Forms.UnsafeNativeMethods.SetFocus(IntPtr hWnd) at System.Windows.Forms.ContainerControl.FocusActiveControlInternal() at System.Windows.Forms.Form.set_Active(Boolean value) at System.Windows.Forms.Form.WmActivate(Message& m) at System.Windows.Forms.Form.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)
/// /// Start background message loop thread and show dialog /// public void AsyncShow() { if( m_guiThread == null ) { ThreadStart start = new ThreadStart( BackgroundGUIThread ); m_guiThread = new Thread( start ); m_guiThread.Start(); } } /// /// Hide dialog and stop background thread /// public void Finish() { try { // hide form this.Hide(); // abort background message loop thread and wait till real thread abort m_guiThread.Abort(); m_guiThread.Join(); } catch( Exception exc ) { Trace.WriteLine( exc.Message + "\r\n" + exc.StackTrace, "PrgDlg Exception" ); } } /// /// Background GUI thread /// private void BackgroundGUIThread() { Thread.CurrentThread.Name = "Background GUI MsgLoop Thread"; try { this.Show(); // Create an object for storing windows message information MSG msg = new MSG(); while( WindowsAPI.GetMessage( ref msg, 0, 0, 0 ) ) { WindowsAPI.TranslateMessage( ref msg ); WindowsAPI.DispatchMessage( ref msg ); } } finally { try { this.Close(); } catch { } Trace.WriteLine( "Quit thread" ); } }
-
Hi! Can somebody send to me code of GUI thread written in C#? How correctly stop bckground thread? For current moment I found only such solution, but it often raise strange Exceptions... I use such code in async progress dialog... but at moment when I say progress form to Finish his work it can raise to me exception, like: ************** Exception Text ************** System.Threading.ThreadAbortException: Thread was being aborted. at System.Windows.Forms.UnsafeNativeMethods.SetFocus(IntPtr hWnd) at System.Windows.Forms.ContainerControl.FocusActiveControlInternal() at System.Windows.Forms.Form.set_Active(Boolean value) at System.Windows.Forms.Form.WmActivate(Message& m) at System.Windows.Forms.Form.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)
/// /// Start background message loop thread and show dialog /// public void AsyncShow() { if( m_guiThread == null ) { ThreadStart start = new ThreadStart( BackgroundGUIThread ); m_guiThread = new Thread( start ); m_guiThread.Start(); } } /// /// Hide dialog and stop background thread /// public void Finish() { try { // hide form this.Hide(); // abort background message loop thread and wait till real thread abort m_guiThread.Abort(); m_guiThread.Join(); } catch( Exception exc ) { Trace.WriteLine( exc.Message + "\r\n" + exc.StackTrace, "PrgDlg Exception" ); } } /// /// Background GUI thread /// private void BackgroundGUIThread() { Thread.CurrentThread.Name = "Background GUI MsgLoop Thread"; try { this.Show(); // Create an object for storing windows message information MSG msg = new MSG(); while( WindowsAPI.GetMessage( ref msg, 0, 0, 0 ) ) { WindowsAPI.TranslateMessage( ref msg ); WindowsAPI.DispatchMessage( ref msg ); } } finally { try { this.Close(); } catch { } Trace.WriteLine( "Quit thread" ); } }
-
Thanks... Not realy what I expect, but it can work without strange exceptions Good Luck Alex Kucherenko