Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. GUI Threads

GUI Threads

Scheduled Pinned Locked Moved C#
csharpdebuggingquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    Oleksandr Kucherenko
    wrote on last edited by
    #1

    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" ); } }

    S 1 Reply Last reply
    0
    • O Oleksandr Kucherenko

      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" ); } }

      S Offline
      S Offline
      Stephane Rodriguez
      wrote on last edited by
      #2

      This MSDN article[^] can help.

      O 1 Reply Last reply
      0
      • S Stephane Rodriguez

        This MSDN article[^] can help.

        O Offline
        O Offline
        Oleksandr Kucherenko
        wrote on last edited by
        #3

        Thanks... Not realy what I expect, but it can work without strange exceptions Good Luck Alex Kucherenko

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups