Winforms and threads

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

    #1

    Winforms and threads

    I know that the controls on a Winform are not thread safe, and that you want
    to do all of your UI updates on a single thread -- generally the main
    thread.

    Now, 2 questions:
    1. Does the one thread that you can use to update controls have to be the
    thread that you were on when you instanced the form?

    2. Can 2 forms in the same application have affinity for different threads,
    or do you want all of the forms in an application to be created on the main
    thread?

    Why I'm asking:

    I'm working on a module that will place an item in system tray. The code
    associated with the tray item will use a secondary thread to listen for
    system events, and based on some of the events, it might have to launch a
    form. Do I need to work in some logic to make sure that the form launch
    code is always executed by the main thread?


  • 100

    #2
    Re: Winforms and threads

    Hi J.Marsch,
    [color=blue]
    > I know that the controls on a Winform are not thread safe, and that you[/color]
    want[color=blue]
    > to do all of your UI updates on a single thread -- generally the main
    > thread.
    > Now, 2 questions:
    > 1. Does the one thread that you can use to update controls have to be the
    > thread that you were on when you instanced the form?[/color]

    The thread which has created the control (this thread owns the underlying
    windows' control handle) can update the control. This is Windows requirement
    rather than Windows Form's one.
    [color=blue]
    > 2. Can 2 forms in the same application have affinity for different[/color]
    threads,[color=blue]
    > or do you want all of the forms in an application to be created on the[/color]
    main[color=blue]
    > thread?[/color]
    You may have many UI threads any of them may has created bunch of forms and
    controls. The rule for the version of .NET running on windows platform is:
    You have to update a control only from the thread that owns the control's
    HWND (this is thread created the control)
    [color=blue]
    > Why I'm asking:
    >
    > I'm working on a module that will place an item in system tray. The code
    > associated with the tray item will use a secondary thread to listen for
    > system events, and based on some of the events, it might have to launch a
    > form. Do I need to work in some logic to make sure that the form launch
    > code is always executed by the main thread?
    >
    >[/color]
    From any thread you can call Control.Invoke to execute a method in the UI
    thread created the control or form and let this method udate the controls.
    Usually main form's Invoke is used.

    HTH
    B\rgds
    100


    Comment

    • Sunny

      #3
      Re: Winforms and threads

      Hi,

      In article <unHdSltrDHA.22 52@TK2MSFTNGP09 .phx.gbl>,
      jeremy@ctcdevel oper.com says...[color=blue]
      > I know that the controls on a Winform are not thread safe, and that you want
      > to do all of your UI updates on a single thread -- generally the main
      > thread.
      >
      > Now, 2 questions:
      > 1. Does the one thread that you can use to update controls have to be the
      > thread that you were on when you instanced the form?
      >
      > 2. Can 2 forms in the same application have affinity for different threads,
      > or do you want all of the forms in an application to be created on the main
      > thread?
      >
      > Why I'm asking:
      >
      > I'm working on a module that will place an item in system tray. The code
      > associated with the tray item will use a secondary thread to listen for
      > system events, and based on some of the events, it might have to launch a
      > form. Do I need to work in some logic to make sure that the form launch
      > code is always executed by the main thread?
      >
      >
      >[/color]

      Every form has Form.InvokeRequ ired, which you have to inspect to see if
      you should need Invoke.
      Maybe this is not good solution, but in my form (I want to change a
      label field) I use this additions:

      private delegate void ChangeActionTex tDelegate(strin g newtext);

      public void SetActionText(s tring txt)
      {
      if (this.InvokeReq uired)
      this.Invoke(new ChangeActionTex tDelegate(
      this.ChangeActi onText),new object[] {txt});

      else
      this.ChangeActi onText(txt);
      }

      private void ChangeActionTex t(string newtext)
      {
      this.lAction.Te xt = newtext;
      this.lAction.Re fresh();
      }

      So, whenever you want to change the text, doesn't matter from which
      thread, you call the public method SetActionText, and it makes sure that
      the call is forwarded to the form thread if necessary.

      Also, it seems possible the public method to look like:

      public void SetActionText (string txt)
      {
      if (this.InvokeReq uired)
      this.Invoke(new ChangeActionTex tDelegate(
      this.SetActionT ext), new object[] {txt});

      else
      {
      this.lAction.Te xt = newtext;
      this.lAction.Re fresh();
      }
      }

      I.e, you may skip the private method, and to make the form thread to
      invoke the same public method.

      I just don't like this, becausee I'm not so sure that it is safe at all
      (I tried, it worked), and because in the first approach the things are
      more readable.

      So, you can use similar things for every form control, which you have to
      change.

      !!!CAUTION!!!

      I do not know why, but this approach does not work with properties. If
      you put the if statement in a public property, and not in a method, it
      hangs the same way, as if you are trying to change a control directly.

      Hope that helps
      Sunny

      Comment

      • Jeffrey Tan[MSFT]

        #4
        RE: Winforms and threads


        Hi,

        In winform, controls in Windows Forms are bound to a specific thread and
        are not thread safe.
        There are four methods on a control that are safe to call from any thread:
        Invoke, BeginInvoke, EndInvoke and CreateGraphics. For all other method
        calls, you should use one of these invoke methods when calling from a
        different thread.

        The sample below tells you how to manipulate multithreading winform
        controls:
        http://msdn.microsoft.com/library/de...us/cpguide/htm
        l/cpconDeveloping MultithreadedWi ndowsFormsContr ol.asp

        Hope this helps,
        Best regards,
        Jeffrey Tan
        Microsoft Online Partner Support
        Get Secure! - www.microsoft.com/security
        This posting is provided "as is" with no warranties and confers no rights.

        Comment

        • J.Marsch

          #5
          Re: Winforms and threads

          Thank you all for your input, but there's a fine point here that I'm trying
          to close in on. We are on the same page regarding Invoke and that the
          "owning" thread must fire events on the control, and I agree that this is a
          Windows issue, not winforms.

          What I'm lost on:
          What would be best practice for instancing a form? In my example, a
          background thread will possibly need to throw up a form in response to a
          system event. When that happens, from a best-practice point of view, am I
          ok just instancing a form on the spot, or am I better off creating a pattern
          that will cause the form to be created on the application's Main thread?

          ""Jeffrey Tan[MSFT]"" <v-jetan@online.mi crosoft.com> wrote in message
          news:ubTm02wrDH A.3644@cpmsftng xa07.phx.gbl...[color=blue]
          >
          > Hi,
          >
          > In winform, controls in Windows Forms are bound to a specific thread and
          > are not thread safe.
          > There are four methods on a control that are safe to call from any thread:
          > Invoke, BeginInvoke, EndInvoke and CreateGraphics. For all other method
          > calls, you should use one of these invoke methods when calling from a
          > different thread.
          >
          > The sample below tells you how to manipulate multithreading winform
          > controls:
          >[/color]
          http://msdn.microsoft.com/library/de...us/cpguide/htm[color=blue]
          > l/cpconDeveloping MultithreadedWi ndowsFormsContr ol.asp
          >
          > Hope this helps,
          > Best regards,
          > Jeffrey Tan
          > Microsoft Online Partner Support
          > Get Secure! - www.microsoft.com/security
          > This posting is provided "as is" with no warranties and confers no rights.
          >[/color]


          Comment

          • Alvin Bruney

            #6
            Re: Winforms and threads

            Do what works for you. Why Must you follow everybody else when you're
            solution is for you.

            If I told you to shove yer hand in the oven , would you? Or cut on yer
            gonads?

            "J.Marsch" <jeremy@ctcdeve loper.com> wrote in message
            news:#wyr9d6rDH A.2448@TK2MSFTN GP12.phx.gbl...[color=blue]
            > Thank you all for your input, but there's a fine point here that I'm[/color]
            trying[color=blue]
            > to close in on. We are on the same page regarding Invoke and that the
            > "owning" thread must fire events on the control, and I agree that this is[/color]
            a[color=blue]
            > Windows issue, not winforms.
            >
            > What I'm lost on:
            > What would be best practice for instancing a form? In my example, a
            > background thread will possibly need to throw up a form in response to a
            > system event. When that happens, from a best-practice point of view, am I
            > ok just instancing a form on the spot, or am I better off creating a[/color]
            pattern[color=blue]
            > that will cause the form to be created on the application's Main thread?
            >
            > ""Jeffrey Tan[MSFT]"" <v-jetan@online.mi crosoft.com> wrote in message
            > news:ubTm02wrDH A.3644@cpmsftng xa07.phx.gbl...[color=green]
            > >
            > > Hi,
            > >
            > > In winform, controls in Windows Forms are bound to a specific thread and
            > > are not thread safe.
            > > There are four methods on a control that are safe to call from any[/color][/color]
            thread:[color=blue][color=green]
            > > Invoke, BeginInvoke, EndInvoke and CreateGraphics. For all other method
            > > calls, you should use one of these invoke methods when calling from a
            > > different thread.
            > >
            > > The sample below tells you how to manipulate multithreading winform
            > > controls:
            > >[/color]
            >[/color]
            http://msdn.microsoft.com/library/de...us/cpguide/htm[color=blue][color=green]
            > > l/cpconDeveloping MultithreadedWi ndowsFormsContr ol.asp
            > >
            > > Hope this helps,
            > > Best regards,
            > > Jeffrey Tan
            > > Microsoft Online Partner Support
            > > Get Secure! - www.microsoft.com/security
            > > This posting is provided "as is" with no warranties and confers no[/color][/color]
            rights.[color=blue][color=green]
            > >[/color]
            >
            >[/color]


            Comment

            • J.Marsch

              #7
              Re: Winforms and threads

              Well, with all due respect (though you've shown me none). What works for me
              may not work for Windows. If I'm about to make a design decision that will
              affect the architecture of a UI layer that will be used in multiple
              applications, and as the basis for the design of hundreds of forms, don't
              you sort of think that it would be a good idea to make sure that the
              threading model is compatible with the Windows UI layer???

              That's called called due dilligence and common sense, Alvin.

              If you don't have anything useful to add to this conversation, why don't
              you just crawl back under whatever rock you came out from under.

              "Alvin Bruney" <alvin.bruney@t elia.com> wrote in message
              news:eXdqRh6rDH A.2360@TK2MSFTN GP10.phx.gbl...[color=blue]
              > Do what works for you. Why Must you follow everybody else when you're
              > solution is for you.
              >
              > If I told you to shove yer hand in the oven , would you? Or cut on yer
              > gonads?
              >
              > "J.Marsch" <jeremy@ctcdeve loper.com> wrote in message
              > news:#wyr9d6rDH A.2448@TK2MSFTN GP12.phx.gbl...[color=green]
              > > Thank you all for your input, but there's a fine point here that I'm[/color]
              > trying[color=green]
              > > to close in on. We are on the same page regarding Invoke and that the
              > > "owning" thread must fire events on the control, and I agree that this[/color][/color]
              is[color=blue]
              > a[color=green]
              > > Windows issue, not winforms.
              > >
              > > What I'm lost on:
              > > What would be best practice for instancing a form? In my example, a
              > > background thread will possibly need to throw up a form in response to a
              > > system event. When that happens, from a best-practice point of view, am[/color][/color]
              I[color=blue][color=green]
              > > ok just instancing a form on the spot, or am I better off creating a[/color]
              > pattern[color=green]
              > > that will cause the form to be created on the application's Main thread?
              > >
              > > ""Jeffrey Tan[MSFT]"" <v-jetan@online.mi crosoft.com> wrote in message
              > > news:ubTm02wrDH A.3644@cpmsftng xa07.phx.gbl...[color=darkred]
              > > >
              > > > Hi,
              > > >
              > > > In winform, controls in Windows Forms are bound to a specific thread[/color][/color][/color]
              and[color=blue][color=green][color=darkred]
              > > > are not thread safe.
              > > > There are four methods on a control that are safe to call from any[/color][/color]
              > thread:[color=green][color=darkred]
              > > > Invoke, BeginInvoke, EndInvoke and CreateGraphics. For all other[/color][/color][/color]
              method[color=blue][color=green][color=darkred]
              > > > calls, you should use one of these invoke methods when calling from a
              > > > different thread.
              > > >
              > > > The sample below tells you how to manipulate multithreading winform
              > > > controls:
              > > >[/color]
              > >[/color]
              >[/color]
              http://msdn.microsoft.com/library/de...us/cpguide/htm[color=blue][color=green][color=darkred]
              > > > l/cpconDeveloping MultithreadedWi ndowsFormsContr ol.asp
              > > >
              > > > Hope this helps,
              > > > Best regards,
              > > > Jeffrey Tan
              > > > Microsoft Online Partner Support
              > > > Get Secure! - www.microsoft.com/security
              > > > This posting is provided "as is" with no warranties and confers no[/color][/color]
              > rights.[color=green][color=darkred]
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • 100

                #8
                Re: Winforms and threads

                Hi J.Marsch,[color=blue]
                > What I'm lost on:
                > What would be best practice for instancing a form? In my example, a
                > background thread will possibly need to throw up a form in response to a
                > system event. When that happens, from a best-practice point of view, am I
                > ok just instancing a form on the spot, or am I better off creating a[/color]
                pattern[color=blue]
                > that will cause the form to be created on the application's Main thread?[/color]

                You don't have options in this case. if you create the form in the worker
                thread it won't receive any win messages and will stay frozen. If you want
                your form to do something you have to pump messages (Application.Ru n or
                Application.DoE vents) in your worker thread, which is not what you want to
                do in your worker thread I believe.
                So the only option is to let the main UI thread throw up the form. Use
                Control.Invoke to switch to the main UI thread, etc.

                HTH
                B\rgds
                100
                [color=blue]
                >
                > ""Jeffrey Tan[MSFT]"" <v-jetan@online.mi crosoft.com> wrote in message
                > news:ubTm02wrDH A.3644@cpmsftng xa07.phx.gbl...[color=green]
                > >
                > > Hi,
                > >
                > > In winform, controls in Windows Forms are bound to a specific thread and
                > > are not thread safe.
                > > There are four methods on a control that are safe to call from any[/color][/color]
                thread:[color=blue][color=green]
                > > Invoke, BeginInvoke, EndInvoke and CreateGraphics. For all other method
                > > calls, you should use one of these invoke methods when calling from a
                > > different thread.
                > >
                > > The sample below tells you how to manipulate multithreading winform
                > > controls:
                > >[/color]
                >[/color]
                http://msdn.microsoft.com/library/de...us/cpguide/htm[color=blue][color=green]
                > > l/cpconDeveloping MultithreadedWi ndowsFormsContr ol.asp
                > >
                > > Hope this helps,
                > > Best regards,
                > > Jeffrey Tan
                > > Microsoft Online Partner Support
                > > Get Secure! - www.microsoft.com/security
                > > This posting is provided "as is" with no warranties and confers no[/color][/color]
                rights.[color=blue][color=green]
                > >[/color]
                >
                >[/color]


                Comment

                • J.Marsch

                  #9
                  Re: Winforms and threads

                  Gotcha. That's exactly what I needed to know. Thank you very much.

                  Regards.

                  -- Jeremy


                  "100" <100@100.com> wrote in message
                  news:OF5%23ju6r DHA.2360@TK2MSF TNGP10.phx.gbl. ..[color=blue]
                  > Hi J.Marsch,[color=green]
                  > > What I'm lost on:
                  > > What would be best practice for instancing a form? In my example, a
                  > > background thread will possibly need to throw up a form in response to a
                  > > system event. When that happens, from a best-practice point of view, am[/color][/color]
                  I[color=blue][color=green]
                  > > ok just instancing a form on the spot, or am I better off creating a[/color]
                  > pattern[color=green]
                  > > that will cause the form to be created on the application's Main thread?[/color]
                  >
                  > You don't have options in this case. if you create the form in the worker
                  > thread it won't receive any win messages and will stay frozen. If you want
                  > your form to do something you have to pump messages (Application.Ru n or
                  > Application.DoE vents) in your worker thread, which is not what you want to
                  > do in your worker thread I believe.
                  > So the only option is to let the main UI thread throw up the form. Use
                  > Control.Invoke to switch to the main UI thread, etc.
                  >
                  > HTH
                  > B\rgds
                  > 100
                  >[color=green]
                  > >
                  > > ""Jeffrey Tan[MSFT]"" <v-jetan@online.mi crosoft.com> wrote in message
                  > > news:ubTm02wrDH A.3644@cpmsftng xa07.phx.gbl...[color=darkred]
                  > > >
                  > > > Hi,
                  > > >
                  > > > In winform, controls in Windows Forms are bound to a specific thread[/color][/color][/color]
                  and[color=blue][color=green][color=darkred]
                  > > > are not thread safe.
                  > > > There are four methods on a control that are safe to call from any[/color][/color]
                  > thread:[color=green][color=darkred]
                  > > > Invoke, BeginInvoke, EndInvoke and CreateGraphics. For all other[/color][/color][/color]
                  method[color=blue][color=green][color=darkred]
                  > > > calls, you should use one of these invoke methods when calling from a
                  > > > different thread.
                  > > >
                  > > > The sample below tells you how to manipulate multithreading winform
                  > > > controls:
                  > > >[/color]
                  > >[/color]
                  >[/color]
                  http://msdn.microsoft.com/library/de...us/cpguide/htm[color=blue][color=green][color=darkred]
                  > > > l/cpconDeveloping MultithreadedWi ndowsFormsContr ol.asp
                  > > >
                  > > > Hope this helps,
                  > > > Best regards,
                  > > > Jeffrey Tan
                  > > > Microsoft Online Partner Support
                  > > > Get Secure! - www.microsoft.com/security
                  > > > This posting is provided "as is" with no warranties and confers no[/color][/color]
                  > rights.[color=green][color=darkred]
                  > > >[/color]
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  • Alvin Bruney

                    #10
                    Re: Winforms and threads

                    Its called MSDN, go read it.


                    "J.Marsch" <jeremy@ctcdeve loper.com> wrote in message
                    news:OyNxns6rDH A.1512@TK2MSFTN GP10.phx.gbl...[color=blue]
                    > Well, with all due respect (though you've shown me none). What works for[/color]
                    me[color=blue]
                    > may not work for Windows. If I'm about to make a design decision that[/color]
                    will[color=blue]
                    > affect the architecture of a UI layer that will be used in multiple
                    > applications, and as the basis for the design of hundreds of forms, don't
                    > you sort of think that it would be a good idea to make sure that the
                    > threading model is compatible with the Windows UI layer???
                    >
                    > That's called called due dilligence and common sense, Alvin.
                    >
                    > If you don't have anything useful to add to this conversation, why don't
                    > you just crawl back under whatever rock you came out from under.
                    >
                    > "Alvin Bruney" <alvin.bruney@t elia.com> wrote in message
                    > news:eXdqRh6rDH A.2360@TK2MSFTN GP10.phx.gbl...[color=green]
                    > > Do what works for you. Why Must you follow everybody else when you're
                    > > solution is for you.
                    > >
                    > > If I told you to shove yer hand in the oven , would you? Or cut on yer
                    > > gonads?
                    > >
                    > > "J.Marsch" <jeremy@ctcdeve loper.com> wrote in message
                    > > news:#wyr9d6rDH A.2448@TK2MSFTN GP12.phx.gbl...[color=darkred]
                    > > > Thank you all for your input, but there's a fine point here that I'm[/color]
                    > > trying[color=darkred]
                    > > > to close in on. We are on the same page regarding Invoke and that[/color][/color][/color]
                    the[color=blue][color=green][color=darkred]
                    > > > "owning" thread must fire events on the control, and I agree that this[/color][/color]
                    > is[color=green]
                    > > a[color=darkred]
                    > > > Windows issue, not winforms.
                    > > >
                    > > > What I'm lost on:
                    > > > What would be best practice for instancing a form? In my example, a
                    > > > background thread will possibly need to throw up a form in response to[/color][/color][/color]
                    a[color=blue][color=green][color=darkred]
                    > > > system event. When that happens, from a best-practice point of view,[/color][/color][/color]
                    am[color=blue]
                    > I[color=green][color=darkred]
                    > > > ok just instancing a form on the spot, or am I better off creating a[/color]
                    > > pattern[color=darkred]
                    > > > that will cause the form to be created on the application's Main[/color][/color][/color]
                    thread?[color=blue][color=green][color=darkred]
                    > > >
                    > > > ""Jeffrey Tan[MSFT]"" <v-jetan@online.mi crosoft.com> wrote in message
                    > > > news:ubTm02wrDH A.3644@cpmsftng xa07.phx.gbl...
                    > > > >
                    > > > > Hi,
                    > > > >
                    > > > > In winform, controls in Windows Forms are bound to a specific thread[/color][/color]
                    > and[color=green][color=darkred]
                    > > > > are not thread safe.
                    > > > > There are four methods on a control that are safe to call from any[/color]
                    > > thread:[color=darkred]
                    > > > > Invoke, BeginInvoke, EndInvoke and CreateGraphics. For all other[/color][/color]
                    > method[color=green][color=darkred]
                    > > > > calls, you should use one of these invoke methods when calling from[/color][/color][/color]
                    a[color=blue][color=green][color=darkred]
                    > > > > different thread.
                    > > > >
                    > > > > The sample below tells you how to manipulate multithreading winform
                    > > > > controls:
                    > > > >
                    > > >[/color]
                    > >[/color]
                    >[/color]
                    http://msdn.microsoft.com/library/de...us/cpguide/htm[color=blue][color=green][color=darkred]
                    > > > > l/cpconDeveloping MultithreadedWi ndowsFormsContr ol.asp
                    > > > >
                    > > > > Hope this helps,
                    > > > > Best regards,
                    > > > > Jeffrey Tan
                    > > > > Microsoft Online Partner Support
                    > > > > Get Secure! - www.microsoft.com/security
                    > > > > This posting is provided "as is" with no warranties and confers no[/color]
                    > > rights.[color=darkred]
                    > > > >
                    > > >
                    > > >[/color]
                    > >
                    > >[/color]
                    >
                    >[/color]


                    Comment

                    • J.Marsch

                      #11
                      Re: Winforms and threads

                      Hmm.

                      I think I'm finished here, you've clearly hit a point where you cannot
                      logically argue, so you're throwing out a few one-liners.

                      I can't help but to point out that your initial argument was "just do what
                      works for you", and now you are attempting to back that up by suggesting
                      that I go read about what other people are doing.
                      Hmm.

                      But beyond that, I would love to hear your explanation of exactly what you
                      think these newsgroups are for.

                      Personally, I use them as a resource for seeking answers to general
                      questions, so that I can tune my research, and as airing platform so that I
                      can get reality checks on technical and design decisions, and as an
                      opportunity to poll the experience of other professionals when I feel it
                      appropriate.

                      I also try to repay the community by providing constructive, helpful
                      responses when I encounter someone with a problem that I am familiar with.
                      Oh, and since I do pay for MSDN Universal, which includes newsgroup support,
                      I feel completely entitled to post a question when one arises.

                      "Alvin Bruney" <alvin.bruney@t elia.com> wrote in message
                      news:OSordA7rDH A.560@TK2MSFTNG P11.phx.gbl...[color=blue]
                      > Its called MSDN, go read it.
                      >
                      >
                      > "J.Marsch" <jeremy@ctcdeve loper.com> wrote in message
                      > news:OyNxns6rDH A.1512@TK2MSFTN GP10.phx.gbl...[color=green]
                      > > Well, with all due respect (though you've shown me none). What works[/color][/color]
                      for[color=blue]
                      > me[color=green]
                      > > may not work for Windows. If I'm about to make a design decision that[/color]
                      > will[color=green]
                      > > affect the architecture of a UI layer that will be used in multiple
                      > > applications, and as the basis for the design of hundreds of forms,[/color][/color]
                      don't[color=blue][color=green]
                      > > you sort of think that it would be a good idea to make sure that the
                      > > threading model is compatible with the Windows UI layer???
                      > >
                      > > That's called called due dilligence and common sense, Alvin.
                      > >
                      > > If you don't have anything useful to add to this conversation, why[/color][/color]
                      don't[color=blue][color=green]
                      > > you just crawl back under whatever rock you came out from under.
                      > >
                      > > "Alvin Bruney" <alvin.bruney@t elia.com> wrote in message
                      > > news:eXdqRh6rDH A.2360@TK2MSFTN GP10.phx.gbl...[color=darkred]
                      > > > Do what works for you. Why Must you follow everybody else when you're
                      > > > solution is for you.
                      > > >
                      > > > If I told you to shove yer hand in the oven , would you? Or cut on yer
                      > > > gonads?
                      > > >
                      > > > "J.Marsch" <jeremy@ctcdeve loper.com> wrote in message
                      > > > news:#wyr9d6rDH A.2448@TK2MSFTN GP12.phx.gbl...
                      > > > > Thank you all for your input, but there's a fine point here that I'm
                      > > > trying
                      > > > > to close in on. We are on the same page regarding Invoke and that[/color][/color]
                      > the[color=green][color=darkred]
                      > > > > "owning" thread must fire events on the control, and I agree that[/color][/color][/color]
                      this[color=blue][color=green]
                      > > is[color=darkred]
                      > > > a
                      > > > > Windows issue, not winforms.
                      > > > >
                      > > > > What I'm lost on:
                      > > > > What would be best practice for instancing a form? In my example, a
                      > > > > background thread will possibly need to throw up a form in response[/color][/color][/color]
                      to[color=blue]
                      > a[color=green][color=darkred]
                      > > > > system event. When that happens, from a best-practice point of[/color][/color][/color]
                      view,[color=blue]
                      > am[color=green]
                      > > I[color=darkred]
                      > > > > ok just instancing a form on the spot, or am I better off creating a
                      > > > pattern
                      > > > > that will cause the form to be created on the application's Main[/color][/color]
                      > thread?[color=green][color=darkred]
                      > > > >
                      > > > > ""Jeffrey Tan[MSFT]"" <v-jetan@online.mi crosoft.com> wrote in[/color][/color][/color]
                      message[color=blue][color=green][color=darkred]
                      > > > > news:ubTm02wrDH A.3644@cpmsftng xa07.phx.gbl...
                      > > > > >
                      > > > > > Hi,
                      > > > > >
                      > > > > > In winform, controls in Windows Forms are bound to a specific[/color][/color][/color]
                      thread[color=blue][color=green]
                      > > and[color=darkred]
                      > > > > > are not thread safe.
                      > > > > > There are four methods on a control that are safe to call from any
                      > > > thread:
                      > > > > > Invoke, BeginInvoke, EndInvoke and CreateGraphics. For all other[/color]
                      > > method[color=darkred]
                      > > > > > calls, you should use one of these invoke methods when calling[/color][/color][/color]
                      from[color=blue]
                      > a[color=green][color=darkred]
                      > > > > > different thread.
                      > > > > >
                      > > > > > The sample below tells you how to manipulate multithreading[/color][/color][/color]
                      winform[color=blue][color=green][color=darkred]
                      > > > > > controls:
                      > > > > >
                      > > > >
                      > > >[/color]
                      > >[/color]
                      >[/color]
                      http://msdn.microsoft.com/library/de...us/cpguide/htm[color=blue][color=green][color=darkred]
                      > > > > > l/cpconDeveloping MultithreadedWi ndowsFormsContr ol.asp
                      > > > > >
                      > > > > > Hope this helps,
                      > > > > > Best regards,
                      > > > > > Jeffrey Tan
                      > > > > > Microsoft Online Partner Support
                      > > > > > Get Secure! - www.microsoft.com/security
                      > > > > > This posting is provided "as is" with no warranties and confers no
                      > > > rights.
                      > > > > >
                      > > > >
                      > > > >
                      > > >
                      > > >[/color]
                      > >
                      > >[/color]
                      >
                      >[/color]


                      Comment

                      Working...