Accessesing GUI from different thread

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

    #1

    Accessesing GUI from different thread

    Whats the best way to access .net GUI classes from a spawned thread? Is
    this even advisable, or is it a sign that the design is wrong?

    Any input, links, etc., would be helpful. Thanks!

    Steve
  • Tomas Restrepo \(MVP\)

    #2
    Re: Accessesing GUI from different thread

    Steve,
    [color=blue]
    > Whats the best way to access .net GUI classes from a spawned thread? Is
    > this even advisable, or is it a sign that the design is wrong?[/color]

    It is certainly possible, and certainly necessary at times. It's not
    necessarily a wrong thing, though in some cases there might be alternatives.

    That said, you do have to take a few things into account, and make sure you
    marshal the GUI access back to the main thread (so that you never touch the
    gui controls from secondary threads), but, fortunately, the .NET framework
    provides a mechanism for this.

    Here's a good article on the topic (it's in C#, but the principles are the
    same):
    http://www.code-magazine.com/article...0403071&page=1


    --
    Tomas Restrepo
    tomasr@mvps.org



    Comment

    • Arnaud Debaene

      #3
      Re: Accessesing GUI from different thread

      Steve N. wrote:[color=blue]
      > Whats the best way to access .net GUI classes from a spawned thread?
      > Is this even advisable, or is it a sign that the design is wrong?[/color]

      The design is not wrong, but you jmust be careful to make your GUI API
      thread-safe : each public function of your GUI objects that may be called on
      different threads should begin by something like this (in C# for ease of
      syntax, but you've got the idea) :

      delegate void DoSomethingDele gate(params...) ///DoSomethingDele gate matches
      DoSomething'sig nature
      void DoSomething(par ams....)
      {
      if (InvokeRequired )
      {
      BeginInvoke (new DoSomethingDele gate(DoSomethin g, new object[]
      {params...}));
      return;
      }

      //normal GUI stuff
      }

      Arnaud
      MVP - VC


      Comment

      • Steve N.

        #4
        Re: Accessesing GUI from different thread

        Tomas Restrepo (MVP) wrote:[color=blue]
        > That said, you do have to take a few things into account, and make sure you
        > marshal the GUI access back to the main thread (so that you never touch the
        > gui controls from secondary threads), but, fortunately, the .NET framework
        > provides a mechanism for this.
        >
        > Here's a good article on the topic (it's in C#, but the principles are the
        > same):
        > http://www.code-magazine.com/article...0403071&page=1[/color]

        Yes, I got it working, thank you.

        Comment

        Working...