Form created on BackGroundThread

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

    Form created on BackGroundThread

    Hi,

    I have some generic module that handles exceptions. simply you call it like
    this: "LogManager.Err orMessage(ex)".

    Worked great, until we now discovered that it isn't Thread-safe. What
    actially happens is this: A Form is made, labels and buttons are made etc
    with the exception-details, but when trying to show the Form
    (MyExceptionFor m.ShowDialog) we get an System.Security .SecurityExcept ion
    (see udnerneath for more info).

    Seems completely normal to me, because we are accessing the UI from another
    Thread. But how to find a solution?


    Thanks a lot in advance,


    Pieter


    PS 1 : I see 2 possiblities:
    1. Call InvokeRequired with Invoke like we usally do when accessing controls
    from another thread
    (http://msdn.microsoft.com/en-us/libr...8(VS.80).aspx). BUT:
    MyExceptionForm .InvokeRequired returns false... I guess this is because it
    has been constructed in a background Thread?
    2. To get around this problem: Figure out when we are on a background thread
    (via Threading.Threa d.CurrentThread .IsBackground: is this true??) and than
    get via an event an an Invoke or something like that on the Main-htread and
    call the form from there?
    3. ...


    PS 2: The exception when doing a MyExceptionForm .ShowDialog:
    System.Security .SecurityExcept ion was caught
    Message="Reques t for the permission of type
    'System.Securit y.Permissions.U IPermission, mscorlib, Version=2.0.0.0 ,
    Culture=neutral , PublicKeyToken= b77a5c561934e08 9' failed."
    Source="mscorli b"
    StackTrace:
    at System.Security .CodeAccessSecu rityEngine.Chec k(Object demand,
    StackCrawlMark& stackMark, Boolean isPermSet)
    at System.Security .CodeAccessPerm ission.Demand()
    at System.Windows. Forms.NativeWin dow.CreateHandl e(CreateParams cp)
    at System.Windows. Forms.Control.C reateHandle()
    at System.Windows. Forms.Form.Crea teHandle()
    at System.Windows. Forms.Control.g et_Handle()
    at
    System.Windows. Forms.Applicati on.ThreadContex t.RunMessageLoo pInner(Int32
    reason, ApplicationCont ext context)
    at
    System.Windows. Forms.Applicati on.ThreadContex t.RunMessageLoo p(Int32 reason,
    ApplicationCont ext context)
    at System.Windows. Forms.Form.Show Dialog(IWin32Wi ndow owner)
    at System.Windows. Forms.Form.Show Dialog()
    at LoggingManager. LogManager.Exce ptionToUI(LogEx ception MyException)




  • Patrice

    #2
    Re: Form created on BackGroundThrea d

    And you call it using ? Do you want your application to continue in case of
    an unexpected exception ?

    You have also an event that is part of the application framework
    (Application_Un handledExceptio n from the top of my head) and that could
    well work even for background thread.

    --
    Patrice

    "Pieter" <pieterNOSPAMco ucke@hotmail.co ma écrit dans le message de groupe
    de discussion : uyi3LUvyIHA.489 6@TK2MSFTNGP03. phx.gbl...
    Hi,
    >
    I have some generic module that handles exceptions. simply you call it
    like this: "LogManager.Err orMessage(ex)".
    >
    Worked great, until we now discovered that it isn't Thread-safe. What
    actially happens is this: A Form is made, labels and buttons are made etc
    with the exception-details, but when trying to show the Form
    (MyExceptionFor m.ShowDialog) we get an System.Security .SecurityExcept ion
    (see udnerneath for more info).
    >
    Seems completely normal to me, because we are accessing the UI from
    another Thread. But how to find a solution?
    >
    >
    Thanks a lot in advance,
    >
    >
    Pieter
    >
    >
    PS 1 : I see 2 possiblities:
    1. Call InvokeRequired with Invoke like we usally do when accessing
    controls from another thread
    (http://msdn.microsoft.com/en-us/libr...8(VS.80).aspx). BUT:
    MyExceptionForm .InvokeRequired returns false... I guess this is because it
    has been constructed in a background Thread?
    2. To get around this problem: Figure out when we are on a background
    thread (via Threading.Threa d.CurrentThread .IsBackground: is this true??)
    and than get via an event an an Invoke or something like that on the
    Main-htread and call the form from there?
    3. ...
    >
    >
    PS 2: The exception when doing a MyExceptionForm .ShowDialog:
    System.Security .SecurityExcept ion was caught
    Message="Reques t for the permission of type
    'System.Securit y.Permissions.U IPermission, mscorlib, Version=2.0.0.0 ,
    Culture=neutral , PublicKeyToken= b77a5c561934e08 9' failed."
    Source="mscorli b"
    StackTrace:
    at System.Security .CodeAccessSecu rityEngine.Chec k(Object demand,
    StackCrawlMark& stackMark, Boolean isPermSet)
    at System.Security .CodeAccessPerm ission.Demand()
    at System.Windows. Forms.NativeWin dow.CreateHandl e(CreateParams cp)
    at System.Windows. Forms.Control.C reateHandle()
    at System.Windows. Forms.Form.Crea teHandle()
    at System.Windows. Forms.Control.g et_Handle()
    at
    System.Windows. Forms.Applicati on.ThreadContex t.RunMessageLoo pInner(Int32
    reason, ApplicationCont ext context)
    at
    System.Windows. Forms.Applicati on.ThreadContex t.RunMessageLoo p(Int32
    reason, ApplicationCont ext context)
    at System.Windows. Forms.Form.Show Dialog(IWin32Wi ndow owner)
    at System.Windows. Forms.Form.Show Dialog()
    at LoggingManager. LogManager.Exce ptionToUI(LogEx ception MyException)
    >
    >
    >
    >

    Comment

    • Pieter

      #3
      Re: Form created on BackGroundThrea d

      I put this line "LogManager.Err orMessage(ex)" in every Try-Catch-block (in
      the catch offcourse :-) ).
      So what it actually does is: it writes the error in a database, sends it via
      an email, and shows it on the screen. But showing on the screen only works
      when the exception handled on the GUI-thread. so I want it be shown always,
      whiwhever thread was used...

      "Patrice" <http://www.chez.com/scribe/wrote in message
      news:87343D0D-E79C-4276-8254-F9D82CBDDFE8@mi crosoft.com...
      And you call it using ? Do you want your application to continue in case
      of an unexpected exception ?
      >
      You have also an event that is part of the application framework
      (Application_Un handledExceptio n from the top of my head) and that could
      well work even for background thread.
      >
      --
      Patrice

      Comment

      • Patrice

        #4
        Re: Form created on BackGroundThrea d

        And do you still continue ?

        My personal preference instead of catching unexpected errors would be to use
        this (global) event that could well handle nicely threading. You can keep a
        try block without a catch clause if you want to clean up resources before
        your handler runs...

        This way you shouldn"t have to care about threading issues and you don't
        have to explicitely try/catch everywhere unless you really need...

        If you keep this design, it seems a bit weird as the message usually clearly
        state this is a threading issue. Here it would make me think rather to a
        click once application that doesn't have some UI permissions (don't remember
        the details but by default you have some security restrictions applied to
        clickonce applications). Is this a clickonce deployed application ?

        What if you try to create fresh a small repro. Do you have the same behavior
        ?

        --
        Patrice

        "Pieter" <pieterNOSPAMco ucke@hotmail.co ma écrit dans le message de groupe
        de discussion : #Xvhc8vyIHA.206 8@TK2MSFTNGP05. phx.gbl...
        I put this line "LogManager.Err orMessage(ex)" in every Try-Catch-block (in
        the catch offcourse :-) ).
        So what it actually does is: it writes the error in a database, sends it
        via an email, and shows it on the screen. But showing on the screen only
        works when the exception handled on the GUI-thread. so I want it be shown
        always, whiwhever thread was used...
        >
        "Patrice" <http://www.chez.com/scribe/wrote in message
        news:87343D0D-E79C-4276-8254-F9D82CBDDFE8@mi crosoft.com...
        >And you call it using ? Do you want your application to continue in case
        >of an unexpected exception ?
        >>
        >You have also an event that is part of the application framework
        >(Application_U nhandledExcepti on from the top of my head) and that could
        >well work even for background thread.
        >>
        >--
        >Patrice
        >
        >

        Comment

        • Patrice

          #5
          Re: Form created on BackGroundThrea d

          The strange thing is that the message looks like a UI restricted ClickOnce
          operation
          (http://msdn.microsoft.com/en-us/libr...rmission.aspx).
          Is this a ClickOnce applciation ? Does it have fulltrust ?

          When this is a cross thread operation the exception is an
          InvalidOperatio nException and is much more explicit (translated from french
          "Invalid Cross Thread operation : the control "Blah" is being access by
          another thread than the one who created it"). You are using .NET 2.0 ?

          My first move would be likely to create a new application as simple as
          possible that throwns in a background thread to see if I have the same
          behavior.

          --
          Patrice


          "Pieter" <pieterNOSPAMco ucke@hotmail.co ma écrit dans le message de groupe
          de discussion : #Xvhc8vyIHA.206 8@TK2MSFTNGP05. phx.gbl...
          I put this line "LogManager.Err orMessage(ex)" in every Try-Catch-block (in
          the catch offcourse :-) ).
          So what it actually does is: it writes the error in a database, sends it
          via an email, and shows it on the screen. But showing on the screen only
          works when the exception handled on the GUI-thread. so I want it be shown
          always, whiwhever thread was used...
          >
          "Patrice" <http://www.chez.com/scribe/wrote in message
          news:87343D0D-E79C-4276-8254-F9D82CBDDFE8@mi crosoft.com...
          >And you call it using ? Do you want your application to continue in case
          >of an unexpected exception ?
          >>
          >You have also an event that is part of the application framework
          >(Application_U nhandledExcepti on from the top of my head) and that could
          >well work even for background thread.
          >>
          >--
          >Patrice
          >
          >

          Comment

          • Pieter

            #6
            Re: Form created on BackGroundThrea d

            Hi Patrice,

            Thansk a lot for your efforts!

            It's a VB.NET 2.0 Windows Forms application.
            We have this problem whenever we want to show the form, when the exception
            happened on a Thread other than the one of the GUI.
            I'm quit sure it doesn't has any to do with permissions, because of these 2
            reasons:
            - If we run the same code on the GUI thread and generate an exception there,
            it shows it nicely.
            - If even happens when trying to do a Cursor.Current =
            System.Windows. Forms.Cursors.W aitCursor
            Although, I didn't test it yet with some sampel application if it really has
            only something to do with a different Thread. We noticed it because of an
            error generated by the ReportViewer (which uses another Thread). And because
            the exception-handlign screw up whenever it accessed the GUI, I presumed it
            was because of this... I'll try to isolate the problem...

            Handlign the exceptions in another way won't be possible unfortunately.


            Pieter


            "Patrice" <http://www.chez.com/scribe/wrote in message
            news:4E157960-BA02-4957-AAB8-929CEE8A979B@mi crosoft.com...
            The strange thing is that the message looks like a UI restricted ClickOnce
            operation
            (http://msdn.microsoft.com/en-us/libr...rmission.aspx).
            Is this a ClickOnce applciation ? Does it have fulltrust ?
            >
            When this is a cross thread operation the exception is an
            InvalidOperatio nException and is much more explicit (translated from
            french "Invalid Cross Thread operation : the control "Blah" is being
            access by another thread than the one who created it"). You are using .NET
            2.0 ?
            >
            My first move would be likely to create a new application as simple as
            possible that throwns in a background thread to see if I have the same
            behavior.
            >
            --
            Patrice
            >
            >
            "Pieter" <pieterNOSPAMco ucke@hotmail.co ma écrit dans le message de
            groupe de discussion : #Xvhc8vyIHA.206 8@TK2MSFTNGP05. phx.gbl...
            >I put this line "LogManager.Err orMessage(ex)" in every Try-Catch-block
            >(in the catch offcourse :-) ).
            >So what it actually does is: it writes the error in a database, sends it
            >via an email, and shows it on the screen. But showing on the screen only
            >works when the exception handled on the GUI-thread. so I want it be shown
            >always, whiwhever thread was used...
            >>
            >"Patrice" <http://www.chez.com/scribe/wrote in message
            >news:87343D0 D-E79C-4276-8254-F9D82CBDDFE8@mi crosoft.com...
            >>And you call it using ? Do you want your application to continue in case
            >>of an unexpected exception ?
            >>>
            >>You have also an event that is part of the application framework
            >>(Application_ UnhandledExcept ion from the top of my head) and that could
            >>well work even for background thread.
            >>>
            >>--
            >>Patrice
            >>
            >>
            >

            Comment

            • Pieter

              #7
              Re: Form created on BackGroundThrea d

              Hi,

              Apparently it isn't because of a different Threa,d but specific to the
              Reportviewer. We found the needed information o nthis site:

              and solved the problem by addign simply this line of code before rendering
              the Report:
              Me.rpvwReport.L ocalReport.Exec uteReportInCurr entAppDomain(Ap pDomain.Current Domain.Evidence )



              Thanks for the help! :-)


              "Patrice" <http://www.chez.com/scribe/wrote in message
              news:4E157960-BA02-4957-AAB8-929CEE8A979B@mi crosoft.com...
              The strange thing is that the message looks like a UI restricted ClickOnce
              operation
              (http://msdn.microsoft.com/en-us/libr...rmission.aspx).
              Is this a ClickOnce applciation ? Does it have fulltrust ?
              >
              When this is a cross thread operation the exception is an
              InvalidOperatio nException and is much more explicit (translated from
              french "Invalid Cross Thread operation : the control "Blah" is being
              access by another thread than the one who created it"). You are using .NET
              2.0 ?
              >
              My first move would be likely to create a new application as simple as
              possible that throwns in a background thread to see if I have the same
              behavior.
              >
              --
              Patrice
              >
              >
              "Pieter" <pieterNOSPAMco ucke@hotmail.co ma écrit dans le message de
              groupe de discussion : #Xvhc8vyIHA.206 8@TK2MSFTNGP05. phx.gbl...
              >I put this line "LogManager.Err orMessage(ex)" in every Try-Catch-block
              >(in the catch offcourse :-) ).
              >So what it actually does is: it writes the error in a database, sends it
              >via an email, and shows it on the screen. But showing on the screen only
              >works when the exception handled on the GUI-thread. so I want it be shown
              >always, whiwhever thread was used...
              >>
              >"Patrice" <http://www.chez.com/scribe/wrote in message
              >news:87343D0 D-E79C-4276-8254-F9D82CBDDFE8@mi crosoft.com...
              >>And you call it using ? Do you want your application to continue in case
              >>of an unexpected exception ?
              >>>
              >>You have also an event that is part of the application framework
              >>(Application_ UnhandledExcept ion from the top of my head) and that could
              >>well work even for background thread.
              >>>
              >>--
              >>Patrice
              >>
              >>
              >

              Comment

              • Patrice

                #8
                Re: Form created on BackGroundThrea d

                Great you solved it and thanks for letting us know what caused the
                problem...

                --
                Patrice

                "Pieter" <pieterNOSPAMco ucke@hotmail.co ma écrit dans le message de groupe
                de discussion : e7bf8g6yIHA.238 4@TK2MSFTNGP02. phx.gbl...
                Hi,
                >
                Apparently it isn't because of a different Threa,d but specific to the
                Reportviewer. We found the needed information o nthis site:

                and solved the problem by addign simply this line of code before rendering
                the Report:
                Me.rpvwReport.L ocalReport.Exec uteReportInCurr entAppDomain(Ap pDomain.Current Domain.Evidence )
                >
                >
                >
                Thanks for the help! :-)
                >
                >
                "Patrice" <http://www.chez.com/scribe/wrote in message
                news:4E157960-BA02-4957-AAB8-929CEE8A979B@mi crosoft.com...
                >The strange thing is that the message looks like a UI restricted
                >ClickOnce operation
                >(http://msdn.microsoft.com/en-us/libr...rmission.aspx).
                >Is this a ClickOnce applciation ? Does it have fulltrust ?
                >>
                >When this is a cross thread operation the exception is an
                >InvalidOperati onException and is much more explicit (translated from
                >french "Invalid Cross Thread operation : the control "Blah" is being
                >access by another thread than the one who created it"). You are using
                >.NET 2.0 ?
                >>
                >My first move would be likely to create a new application as simple as
                >possible that throwns in a background thread to see if I have the same
                >behavior.
                >>
                >--
                >Patrice
                >>
                >>
                >"Pieter" <pieterNOSPAMco ucke@hotmail.co ma écrit dans le message de
                >groupe de discussion : #Xvhc8vyIHA.206 8@TK2MSFTNGP05. phx.gbl...
                >>I put this line "LogManager.Err orMessage(ex)" in every Try-Catch-block
                >>(in the catch offcourse :-) ).
                >>So what it actually does is: it writes the error in a database, sends it
                >>via an email, and shows it on the screen. But showing on the screen only
                >>works when the exception handled on the GUI-thread. so I want it be
                >>shown always, whiwhever thread was used...
                >>>
                >>"Patrice" <http://www.chez.com/scribe/wrote in message
                >>news:87343D 0D-E79C-4276-8254-F9D82CBDDFE8@mi crosoft.com...
                >>>And you call it using ? Do you want your application to continue in
                >>>case of an unexpected exception ?
                >>>>
                >>>You have also an event that is part of the application framework
                >>>(Application _UnhandledExcep tion from the top of my head) and that
                >>>could well work even for background thread.
                >>>>
                >>>--
                >>>Patrice
                >>>
                >>>
                >>
                >
                >

                Comment

                Working...