Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 51945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:49:47+00:00 2026-05-10T16:49:47+00:00

I have a method running in a seperate thread. The thread is created and

  • 0

I have a method running in a seperate thread. The thread is created and started from a form in a windows application. If an exception is thrown from inside the thread, what is the best way to pass it back to the main application. Right now, I’m passing a reference to the main form into the thread, then invoking the method from the thread, and causing the method to be called by the main application thread. Is there a best practice way to do this because I’m not comfortable with how I’m doing it now.

Example of my form:

public class frmMyForm : System.Windows.Forms.Form {     /// <summary>     /// Create a thread     /// </summary>     /// <param name='sender'></param>     /// <param name='e'></param>     private void btnTest_Click(object sender, EventArgs e)     {         try         {             //Create and start the thread            ThreadExample pThreadExample = new ThreadExample(this);            pThreadExample.Start();         }         catch (Exception ex)         {             MessageBox.Show(ex.Message, Application.ProductName);         }     }      /// <summary>     /// Called from inside the thread      /// </summary>     /// <param name='ex'></param>     public void HandleError(Exception ex)     {         //Invoke a method in the GUI's main thread         this.Invoke(new ThreadExample.delThreadSafeTriggerScript(HandleError), new Object[] { ex });     }      private void __HandleError(Exception ex)     {         MessageBox.Show(ex.Message);     } } 

Example of my thread class:

public class ThreadExample {     public delegate void delThreadSafeHandleException(System.Exception ex);      private Thread thExample_m;      frmMyForm pForm_m;     private frmMyForm Form     {         get         {             return pForm_m;         }     }      public ThreadExample(frmMyForm pForm)     {         pForm_m = pForm;          thExample_m = new Thread(new ThreadStart(Main));         thExample_m.Name = 'Example Thread';     }      public void Start()     {         thExample_m.Start();     }      private void Main()     {         try         {             throw new Exception('Test');         }         catch (Exception ex)         {             Form.HandleException(ex);         }     } } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T16:49:48+00:00Added an answer on May 10, 2026 at 4:49 pm

    So you’re using Invoke to marshall back to the UI thread, by the looks of it – which is exactly what you need to do. I’d personally use an Action<Exception> for simplicity’s sake, and possibly BeginInvoke instead of Invoke, but basically you’re doing the right thing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 194k
  • Answers 194k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You are using the Windows Media Player ActiveX control, which… May 12, 2026 at 6:43 pm
  • Editorial Team
    Editorial Team added an answer This will do it: input[type=text] Though it might not work… May 12, 2026 at 6:43 pm
  • Editorial Team
    Editorial Team added an answer There is an ultimate solution for this problem - run… May 12, 2026 at 6:43 pm

Related Questions

I have a Windows service written in Delphi. One of the third-party resources it
I have read in many places that network connection in a j2me app should
I have an application that needs to work with a vendor-supplied API to do
I am experiencing a weirdest thing for the last couple of days. I found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.