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. Handling Exceptions from another thread

Handling Exceptions from another thread

Scheduled Pinned Locked Moved C#
questionhelpdesign
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.
  • F Offline
    F Offline
    Fayu
    wrote on last edited by
    #1

    I am having some issue handling an exception thrown in another thread in my UI. My question is how can I handle exceptions from another thread without syncronized the threads in the UI. I would prefer to sync the threads in the class where the method is called. Any suggestions? Here is my code: Localized fields

    Test t = new Test();

    Method that throws the exception

    public class Test
    {
    public void DoSomething()
    {
    throw new MyCustomException(this,EventArgs.Empty);
    }
    }

    The exception

    public class MyCustomException : Exception
    {
    ...
    }

    This is in the UI. Not multi-threaded

     void CallException()
     {
          try
          {
               t.DoSomething();
          }
          catch(MyCustomException ex)
          {
               lblMessage.Text = "error";
          }
     }
    

    This is also in the UI. Multi-threaded

     void CallExceptionASync()
     {
          Thread thrd;
          ThreadStart ts = (ThreadStart)delegate
          {
               t.DoSomething();
          };
          thrd = new Thread(ts);
          thrd.Start();
     }
    
    F 1 Reply Last reply
    0
    • F Fayu

      I am having some issue handling an exception thrown in another thread in my UI. My question is how can I handle exceptions from another thread without syncronized the threads in the UI. I would prefer to sync the threads in the class where the method is called. Any suggestions? Here is my code: Localized fields

      Test t = new Test();

      Method that throws the exception

      public class Test
      {
      public void DoSomething()
      {
      throw new MyCustomException(this,EventArgs.Empty);
      }
      }

      The exception

      public class MyCustomException : Exception
      {
      ...
      }

      This is in the UI. Not multi-threaded

       void CallException()
       {
            try
            {
                 t.DoSomething();
            }
            catch(MyCustomException ex)
            {
                 lblMessage.Text = "error";
            }
       }
      

      This is also in the UI. Multi-threaded

       void CallExceptionASync()
       {
            Thread thrd;
            ThreadStart ts = (ThreadStart)delegate
            {
                 t.DoSomething();
            };
            thrd = new Thread(ts);
            thrd.Start();
       }
      
      F Offline
      F Offline
      Fayu
      wrote on last edited by
      #2

      I have created a workaround but I am not satisfied with it. If anyone has a better suggestion please let me know. Here is the workaround:

      void CallExceptionASync()
      {
      Thread thrd;
      ThreadStart ts = (ThreadStart)delegate
      {
      try
      {
      t.DoSomething();
      }
      catch(MyCustomException ex)
      {
      this.lblMessage.Invoke((MethodInvokder)delegate()
      {
      this.lblMessage.Text = ex.Message;
      });
      }
      }
      }

      L 1 Reply Last reply
      0
      • F Fayu

        I have created a workaround but I am not satisfied with it. If anyone has a better suggestion please let me know. Here is the workaround:

        void CallExceptionASync()
        {
        Thread thrd;
        ThreadStart ts = (ThreadStart)delegate
        {
        try
        {
        t.DoSomething();
        }
        catch(MyCustomException ex)
        {
        this.lblMessage.Invoke((MethodInvokder)delegate()
        {
        this.lblMessage.Text = ex.Message;
        });
        }
        }
        }

        L Offline
        L Offline
        Luc 648011
        wrote on last edited by
        #3

        if the thread throws an exception, and the catch is to touch a GUI Control, then you need Control.Invoke, there is no way around that since that thread is not allowed to touch any Controls. BTW: A BackgroundWorker has a Completed event which does the invoking automatically, and its event argument holds the exception that may have occurred. :)

        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