Catch Exception in background thread

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jesper Nilsson

    Catch Exception in background thread

    I'm using a background thread in my application with this example:


    Do you guys know any easy way to catch exceptions being thrown in the
    background thread and show error on the mainForm?
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Catch Exception in background thread

    Jesper,

    What I would do is create a delegate that takes an exception as a
    parameter. Then, define a method with the same signature on your form.
    From the thread, when the exception is caught, call the Invoke method on the
    control, passing the delegate with the method, and the exception. Then, the
    method will be called on the main UI thread, at which point, you can show
    the details of the exception.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Jesper Nilsson" <JesperNilsson@ discussions.mic rosoft.com> wrote in message
    news:53BE5908-25E1-4941-9E1C-4D1170B3825D@mi crosoft.com...[color=blue]
    > I'm using a background thread in my application with this example:
    > http://www.codeproject.com/csharp/winformthreading.asp
    >
    > Do you guys know any easy way to catch exceptions being thrown in the
    > background thread and show error on the mainForm?[/color]


    Comment

    • Justin Rogers

      #3
      Re: Catch Exception in background thread

      AppDomain has an UnhandledExcept ion
      Application has a ThreadException


      --
      Justin Rogers
      DigiTec Web Consultants, LLC.
      Blog: http://weblogs.asp.net/justin_rogers

      "Jesper Nilsson" <JesperNilsson@ discussions.mic rosoft.com> wrote in message
      news:53BE5908-25E1-4941-9E1C-4D1170B3825D@mi crosoft.com...[color=blue]
      > I'm using a background thread in my application with this example:
      > http://www.codeproject.com/csharp/winformthreading.asp
      >
      > Do you guys know any easy way to catch exceptions being thrown in the
      > background thread and show error on the mainForm?[/color]


      Comment

      • Jesper Nilsson

        #4
        Re: Catch Exception in background thread

        Thank you very much, it helped alot.


        "Nicholas Paldino [.NET/C# MVP]" wrote:
        [color=blue]
        > Jesper,
        >
        > What I would do is create a delegate that takes an exception as a
        > parameter. Then, define a method with the same signature on your form.
        > From the thread, when the exception is caught, call the Invoke method on the
        > control, passing the delegate with the method, and the exception. Then, the
        > method will be called on the main UI thread, at which point, you can show
        > the details of the exception.
        >
        > Hope this helps.
        >
        >
        > --
        > - Nicholas Paldino [.NET/C# MVP]
        > - mvp@spam.guard. caspershouse.co m
        >
        > "Jesper Nilsson" <JesperNilsson@ discussions.mic rosoft.com> wrote in message
        > news:53BE5908-25E1-4941-9E1C-4D1170B3825D@mi crosoft.com...[color=green]
        > > I'm using a background thread in my application with this example:
        > > http://www.codeproject.com/csharp/winformthreading.asp
        > >
        > > Do you guys know any easy way to catch exceptions being thrown in the
        > > background thread and show error on the mainForm?[/color]
        >
        >
        >[/color]

        Comment

        Working...