Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Multithreading Question

Multithreading Question

Scheduled Pinned Locked Moved C#
questiondesignperformancehelpannouncement
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    akyriako78
    wrote on last edited by
    #1

    Hi all.I want to update my UI via a background thread. I have concluded in two options : 1.) Checking InvokeRequired private delegate void UpdateUIHandler(); ... protected virtual void UpdateUI1() { if(!InvokeRequired) { ... } else { this.Invoke(new UpdateUIHandler(UpdateUI1)); } } 2.) Using an anonymous method and MethodInvoker: ... public void SomeMethodRunningInDiffirentThread() { ... this.Invoke(new MethodInvoker( delegate() { UpdateUI2(); } )); ... } public void UpdateUI2(){...} So the question is the following.Which approach is more suitable?Are there any deficiencies or side-effects in any of these two methods (e.g performance,stupidity etc.) or any conceptual error in the approaches. Thanx in advance.

    S 1 Reply Last reply
    0
    • A akyriako78

      Hi all.I want to update my UI via a background thread. I have concluded in two options : 1.) Checking InvokeRequired private delegate void UpdateUIHandler(); ... protected virtual void UpdateUI1() { if(!InvokeRequired) { ... } else { this.Invoke(new UpdateUIHandler(UpdateUI1)); } } 2.) Using an anonymous method and MethodInvoker: ... public void SomeMethodRunningInDiffirentThread() { ... this.Invoke(new MethodInvoker( delegate() { UpdateUI2(); } )); ... } public void UpdateUI2(){...} So the question is the following.Which approach is more suitable?Are there any deficiencies or side-effects in any of these two methods (e.g performance,stupidity etc.) or any conceptual error in the approaches. Thanx in advance.

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      Well, the second way is more verbose, as you need two methods to do the job. You also left out the check for InvokeRequired in the second method. It's not a mistake, as Invoke internally checks if it's on the UI thread before doing the actual job. I'd say both are equivalent, but you'd want to prefer the first. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      A 1 Reply Last reply
      0
      • S S Senthil Kumar

        Well, the second way is more verbose, as you need two methods to do the job. You also left out the check for InvokeRequired in the second method. It's not a mistake, as Invoke internally checks if it's on the UI thread before doing the actual job. I'd say both are equivalent, but you'd want to prefer the first. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        A Offline
        A Offline
        akyriako78
        wrote on last edited by
        #3

        Thanx for answering.In the second method i have intentionally left out InvokeRequired check.

        J 1 Reply Last reply
        0
        • A akyriako78

          Thanx for answering.In the second method i have intentionally left out InvokeRequired check.

          J Offline
          J Offline
          Jared Parsons
          wrote on last edited by
          #4

          You can also shorten up the second one a bit. It avoids the overhead of an extra object creation and delegate call. 2.) Using an anonymous method and MethodInvoker: ... public void SomeMethodRunningInDiffirentThread() { ... this.Invoke((MethodInvoker)delegate() { UpdateUI2(); } )); ... } Jared Parsons jaredp@beanseed.org http://spaces.msn.com/members/jaredp/

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups