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. Updating console textbox from multiple threads

Updating console textbox from multiple threads

Scheduled Pinned Locked Moved C#
questiondata-structuresannouncement
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.
  • S Offline
    S Offline
    shivamkalra
    wrote on last edited by
    #1

    Hello everyone, I've made a multithread data base application, which is collecting data from multiple data bases from different threads and then process it and notifies the result on GUI. I've made a console which is just a big text box. Now, my question is that what is the best way to update a single GUI component from multiple threads. So far, I've made class called class IOConsole which constains a variable called static Queue(String message) and a method public static println(string str) and then I've added a background worker in GUI thread which keep looping and checks if queque is empty or not and if not empty then it pop the string and print it on the GUI. To me its does't look very nice approach, not sure if it might be. But could I get some suggestion about how can I achieve this most efficiently. Thanks, Shivam Kalra :)

    M 1 Reply Last reply
    0
    • S shivamkalra

      Hello everyone, I've made a multithread data base application, which is collecting data from multiple data bases from different threads and then process it and notifies the result on GUI. I've made a console which is just a big text box. Now, my question is that what is the best way to update a single GUI component from multiple threads. So far, I've made class called class IOConsole which constains a variable called static Queue(String message) and a method public static println(string str) and then I've added a background worker in GUI thread which keep looping and checks if queque is empty or not and if not empty then it pop the string and print it on the GUI. To me its does't look very nice approach, not sure if it might be. But could I get some suggestion about how can I achieve this most efficiently. Thanks, Shivam Kalra :)

      M Offline
      M Offline
      manchukuo
      wrote on last edited by
      #2

      why not just use begininvoke or invoke? this is executed in the main thread always, or even using windows messages, or there is even invoke required way since .NET 2 the invoke way can be something like this, using anonymous method

      ///...code code code and more code in your worker thread

      string nextText = "qwe";
      this.Invoke( (MethodInvoker)delegate {
      oneLabel.Text = nextText; // runs on main thread
      }
      );

      ///...and goes more code in your worker thread

      B S 2 Replies Last reply
      0
      • M manchukuo

        why not just use begininvoke or invoke? this is executed in the main thread always, or even using windows messages, or there is even invoke required way since .NET 2 the invoke way can be something like this, using anonymous method

        ///...code code code and more code in your worker thread

        string nextText = "qwe";
        this.Invoke( (MethodInvoker)delegate {
        oneLabel.Text = nextText; // runs on main thread
        }
        );

        ///...and goes more code in your worker thread

        B Offline
        B Offline
        BobJanova
        wrote on last edited by
        #3

        The right idea, but it's generally better to have the updater fire events and not be tightly linked in to the UI class. E.g.

        class DatabaseUpdater {
        event EventHandler<UpdateEventArgs> Update;

        void ThreadMethod() {
        while(true){
        // ... check the db
        if(thereWasAnUpdate && Update != null){
        Update(this, new UpdateEventArgs(updatedTable, key, Value);
        // or whatever you want to be in your UpdateEventArgs
        }
        }
        }
        }

        class MainForm {
        List<DatabaseUpdater> updaters;
        MainForm(){
        // ...
        foreach(DatabaseUpdater updater in updaters)
        updater.Update += DatabaseUpdated;
        }

        void DatabaseUpdated(object sender, UpdateEventArgs e){
        this.Invoke((MethodInvoker) delegate { myTextBox.Text += e.UpdatedTable + "\n"; });
        }
        }

        1 Reply Last reply
        0
        • M manchukuo

          why not just use begininvoke or invoke? this is executed in the main thread always, or even using windows messages, or there is even invoke required way since .NET 2 the invoke way can be something like this, using anonymous method

          ///...code code code and more code in your worker thread

          string nextText = "qwe";
          this.Invoke( (MethodInvoker)delegate {
          oneLabel.Text = nextText; // runs on main thread
          }
          );

          ///...and goes more code in your worker thread

          S Offline
          S Offline
          shivamkalra
          wrote on last edited by
          #4

          Hi, thanks for the reply. Will this code make sure that even if I invoke it through multiple threads..may be 5 threads then will be able to print all 5 of them on the console text box? And if "yes" then could you put more light on how it does that? I thought code should have been more complicated but it looks just one sentence thing?

          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