threading and UI calls

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

    threading and UI calls

    About a year back I posted a question and eventually implemented the
    following code in an event handler as a result:

    object[] some_args = new object[1];
    some_args[0] = my_event_args.r esults;
    ISynchronizeInv oke lSync = this as ISynchronizeInv oke;
    if(lSync == null)
    {// UI Thread
    UIDelegate.Dyna micInvoke(some_ args);
    }
    else
    {// Worker thread
    this.Invoke(UID elegate, some_args);
    }

    Reading the code now I'm questioning it. Can lSync ever be null if the code
    is in a WinForm derived class. Other examples I am seeing all do a check of
    InvokeRequired before calling Invoke. Since I am in the form code lSync can
    never be null, correct? Therefore, in my current code, I am always calling
    this.Invoke. I have changed the code to check InvokeRequiredb efore calling
    Invoke. If InvokeRequired is false I just make the call directly.

    My new code:
    object[] some_args = new object[1];
    some_args[0] = my_event_args.r esults;
    ISynchronizeInv oke lSync = this as ISynchronizeInv oke;

    if(lSync.Invoke Required)
    {// Worker thread
    lSync.Invoke(UI Delegate, some_args);
    }
    else
    {//gui thread
    MyHandler(my_ar gs.results);
    }

    Is this correct? This just seems to have less overhead than always calling
    invoke.

    Thanks,
    jim


  • Brian Gideon

    #2
    Re: threading and UI calls

    Jim,

    You are right. The first example is wrong, but the second is correct.

    Brian

    Jim H wrote:[color=blue]
    > About a year back I posted a question and eventually implemented the
    > following code in an event handler as a result:
    >
    > object[] some_args = new object[1];
    > some_args[0] = my_event_args.r esults;
    > ISynchronizeInv oke lSync = this as ISynchronizeInv oke;
    > if(lSync == null)
    > {// UI Thread
    > UIDelegate.Dyna micInvoke(some_ args);
    > }
    > else
    > {// Worker thread
    > this.Invoke(UID elegate, some_args);
    > }
    >
    > Reading the code now I'm questioning it. Can lSync ever be null if the code
    > is in a WinForm derived class. Other examples I am seeing all do a check of
    > InvokeRequired before calling Invoke. Since I am in the form code lSync can
    > never be null, correct? Therefore, in my current code, I am always calling
    > this.Invoke. I have changed the code to check InvokeRequiredb efore calling
    > Invoke. If InvokeRequired is false I just make the call directly.
    >
    > My new code:
    > object[] some_args = new object[1];
    > some_args[0] = my_event_args.r esults;
    > ISynchronizeInv oke lSync = this as ISynchronizeInv oke;
    >
    > if(lSync.Invoke Required)
    > {// Worker thread
    > lSync.Invoke(UI Delegate, some_args);
    > }
    > else
    > {//gui thread
    > MyHandler(my_ar gs.results);
    > }
    >
    > Is this correct? This just seems to have less overhead than always calling
    > invoke.
    >
    > Thanks,
    > jim[/color]

    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: threading and UI calls

      Jim,

      Form derives ultimately from Control, which implements
      ISynchronizeInv oke. You don't have to check to see if it implements it.

      As for your code, you don't have to cast to ISynchronizeInv oke, as
      Control/Form implements the interface implicitly (so you can call it right
      off the "this" reference).

      Hope this helps.


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

      "Jim H" <jimh@nospam.no spam> wrote in message
      news:Ot$LJl3mGH A.5056@TK2MSFTN GP04.phx.gbl...[color=blue]
      > About a year back I posted a question and eventually implemented the
      > following code in an event handler as a result:
      >
      > object[] some_args = new object[1];
      > some_args[0] = my_event_args.r esults;
      > ISynchronizeInv oke lSync = this as ISynchronizeInv oke;
      > if(lSync == null)
      > {// UI Thread
      > UIDelegate.Dyna micInvoke(some_ args);
      > }
      > else
      > {// Worker thread
      > this.Invoke(UID elegate, some_args);
      > }
      >
      > Reading the code now I'm questioning it. Can lSync ever be null if the
      > code is in a WinForm derived class. Other examples I am seeing all do a
      > check of InvokeRequired before calling Invoke. Since I am in the form
      > code lSync can never be null, correct? Therefore, in my current code, I
      > am always calling this.Invoke. I have changed the code to check
      > InvokeRequiredb efore calling Invoke. If InvokeRequired is false I just
      > make the call directly.
      >
      > My new code:
      > object[] some_args = new object[1];
      > some_args[0] = my_event_args.r esults;
      > ISynchronizeInv oke lSync = this as ISynchronizeInv oke;
      >
      > if(lSync.Invoke Required)
      > {// Worker thread
      > lSync.Invoke(UI Delegate, some_args);
      > }
      > else
      > {//gui thread
      > MyHandler(my_ar gs.results);
      > }
      >
      > Is this correct? This just seems to have less overhead than always
      > calling invoke.
      >
      > Thanks,
      > jim
      >[/color]


      Comment

      Working...