Thread exception

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dan Cimpoiesu

    #1

    Thread exception

    How can I make to route the exceptions from a thread created by my
    Application, different that the Applications thread to the application.
    I want that when a exception occures in that thread, the application to be
    aware of it and to display that default dialog for exceptions.

    Regards
    Dan Cimpoiesu


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Thread exception

    Dan,

    What you would have to do is wrap your thread code in an exception
    handler. Once you do that, you will have to send a message to the main
    thread (a Windows message) with the exception information. You can do this
    by using the GCHandle structure. With it you can allocate a handle to an
    object (the exception) and then pass it through a call to SendMessage
    (through the P/Invoke layer). Then, in your main form, you can process the
    message (assuming you send using a custom message type, usuall WM_USER +
    some number) getting the object back through the static methods on the
    GCHandle structure.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - nicholas.paldin o@exisconsultin g.com

    "Dan Cimpoiesu" <dancimpoiesu@g mx.net> wrote in message
    news:uI4n%23w8a DHA.4020@tk2msf tngp13.phx.gbl. ..[color=blue]
    > How can I make to route the exceptions from a thread created by my
    > Application, different that the Applications thread to the application.
    > I want that when a exception occures in that thread, the application to be
    > aware of it and to display that default dialog for exceptions.
    >
    > Regards
    > Dan Cimpoiesu
    >
    >[/color]


    Comment

    • Dan Cimpoiesu

      #3
      Re: Thread exception

      Thanks
      The part with sending a message I knew it.

      I assume that I have to put all my thread code in a big try catch, and when
      I catch something to send it to the main app.
      Is this correct?

      Regards
      Dan Cimpoiesu

      "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com> wrote
      in message news:e3eZR68aDH A.1832@TK2MSFTN GP10.phx.gbl...[color=blue]
      > Dan,
      >
      > What you would have to do is wrap your thread code in an exception
      > handler. Once you do that, you will have to send a message to the main
      > thread (a Windows message) with the exception information. You can do[/color]
      this[color=blue]
      > by using the GCHandle structure. With it you can allocate a handle to an
      > object (the exception) and then pass it through a call to SendMessage
      > (through the P/Invoke layer). Then, in your main form, you can process[/color]
      the[color=blue]
      > message (assuming you send using a custom message type, usuall WM_USER +
      > some number) getting the object back through the static methods on the
      > GCHandle structure.
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - nicholas.paldin o@exisconsultin g.com
      >
      > "Dan Cimpoiesu" <dancimpoiesu@g mx.net> wrote in message
      > news:uI4n%23w8a DHA.4020@tk2msf tngp13.phx.gbl. ..[color=green]
      > > How can I make to route the exceptions from a thread created by my
      > > Application, different that the Applications thread to the application.
      > > I want that when a exception occures in that thread, the application to[/color][/color]
      be[color=blue][color=green]
      > > aware of it and to display that default dialog for exceptions.
      > >
      > > Regards
      > > Dan Cimpoiesu
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Thread exception

        Dan,

        Bingo! =)

        --
        - Nicholas Paldino [.NET/C# MVP]
        - nicholas.paldin o@exisconsultin g.com

        "Dan Cimpoiesu" <dancimpoiesu@g mx.net> wrote in message
        news:%23D3Fv%23 8aDHA.2296@TK2M SFTNGP09.phx.gb l...[color=blue]
        > Thanks
        > The part with sending a message I knew it.
        >
        > I assume that I have to put all my thread code in a big try catch, and[/color]
        when[color=blue]
        > I catch something to send it to the main app.
        > Is this correct?
        >
        > Regards
        > Dan Cimpoiesu
        >
        > "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com>[/color]
        wrote[color=blue]
        > in message news:e3eZR68aDH A.1832@TK2MSFTN GP10.phx.gbl...[color=green]
        > > Dan,
        > >
        > > What you would have to do is wrap your thread code in an exception
        > > handler. Once you do that, you will have to send a message to the main
        > > thread (a Windows message) with the exception information. You can do[/color]
        > this[color=green]
        > > by using the GCHandle structure. With it you can allocate a handle to[/color][/color]
        an[color=blue][color=green]
        > > object (the exception) and then pass it through a call to SendMessage
        > > (through the P/Invoke layer). Then, in your main form, you can process[/color]
        > the[color=green]
        > > message (assuming you send using a custom message type, usuall WM_USER +
        > > some number) getting the object back through the static methods on the
        > > GCHandle structure.
        > >
        > > Hope this helps.
        > >
        > >
        > > --
        > > - Nicholas Paldino [.NET/C# MVP]
        > > - nicholas.paldin o@exisconsultin g.com
        > >
        > > "Dan Cimpoiesu" <dancimpoiesu@g mx.net> wrote in message
        > > news:uI4n%23w8a DHA.4020@tk2msf tngp13.phx.gbl. ..[color=darkred]
        > > > How can I make to route the exceptions from a thread created by my
        > > > Application, different that the Applications thread to the[/color][/color][/color]
        application.[color=blue][color=green][color=darkred]
        > > > I want that when a exception occures in that thread, the application[/color][/color][/color]
        to[color=blue]
        > be[color=green][color=darkred]
        > > > aware of it and to display that default dialog for exceptions.
        > > >
        > > > Regards
        > > > Dan Cimpoiesu
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Dan Cimpoiesu

          #5
          Re: Thread exception

          Thanks
          :-)

          Dan

          "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com> wrote
          in message news:uTNDiD9aDH A.2588@TK2MSFTN GP09.phx.gbl...[color=blue]
          > Dan,
          >
          > Bingo! =)
          >
          > --
          > - Nicholas Paldino [.NET/C# MVP]
          > - nicholas.paldin o@exisconsultin g.com
          >
          > "Dan Cimpoiesu" <dancimpoiesu@g mx.net> wrote in message
          > news:%23D3Fv%23 8aDHA.2296@TK2M SFTNGP09.phx.gb l...[color=green]
          > > Thanks
          > > The part with sending a message I knew it.
          > >
          > > I assume that I have to put all my thread code in a big try catch, and[/color]
          > when[color=green]
          > > I catch something to send it to the main app.
          > > Is this correct?
          > >
          > > Regards
          > > Dan Cimpoiesu
          > >
          > > "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com>[/color]
          > wrote[color=green]
          > > in message news:e3eZR68aDH A.1832@TK2MSFTN GP10.phx.gbl...[color=darkred]
          > > > Dan,
          > > >
          > > > What you would have to do is wrap your thread code in an exception
          > > > handler. Once you do that, you will have to send a message to the[/color][/color][/color]
          main[color=blue][color=green][color=darkred]
          > > > thread (a Windows message) with the exception information. You can do[/color]
          > > this[color=darkred]
          > > > by using the GCHandle structure. With it you can allocate a handle to[/color][/color]
          > an[color=green][color=darkred]
          > > > object (the exception) and then pass it through a call to SendMessage
          > > > (through the P/Invoke layer). Then, in your main form, you can[/color][/color][/color]
          process[color=blue][color=green]
          > > the[color=darkred]
          > > > message (assuming you send using a custom message type, usuall WM_USER[/color][/color][/color]
          +[color=blue][color=green][color=darkred]
          > > > some number) getting the object back through the static methods on the
          > > > GCHandle structure.
          > > >
          > > > Hope this helps.
          > > >
          > > >
          > > > --
          > > > - Nicholas Paldino [.NET/C# MVP]
          > > > - nicholas.paldin o@exisconsultin g.com
          > > >
          > > > "Dan Cimpoiesu" <dancimpoiesu@g mx.net> wrote in message
          > > > news:uI4n%23w8a DHA.4020@tk2msf tngp13.phx.gbl. ..
          > > > > How can I make to route the exceptions from a thread created by my
          > > > > Application, different that the Applications thread to the[/color][/color]
          > application.[color=green][color=darkred]
          > > > > I want that when a exception occures in that thread, the application[/color][/color]
          > to[color=green]
          > > be[color=darkred]
          > > > > aware of it and to display that default dialog for exceptions.
          > > > >
          > > > > Regards
          > > > > Dan Cimpoiesu
          > > > >
          > > > >
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Val Savvateev

            #6
            Re: Thread exception

            If you're developing a Windows app check out Application.OnT hreadException

            "Dan Cimpoiesu" <dancimpoiesu@g mx.net> wrote in message
            news:uI4n%23w8a DHA.4020@tk2msf tngp13.phx.gbl. ..[color=blue]
            > How can I make to route the exceptions from a thread created by my
            > Application, different that the Applications thread to the application.
            > I want that when a exception occures in that thread, the application to be
            > aware of it and to display that default dialog for exceptions.
            >
            > Regards
            > Dan Cimpoiesu
            >
            >[/color]


            Comment

            Working...