Updating console textbox from multiple threads
-
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 calledstatic Queue(String message)
and a methodpublic static println(string str)
and then I've added a background worker in GUI thread which keep looping and checks ifqueque
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 :) -
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 calledstatic Queue(String message)
and a methodpublic static println(string str)
and then I've added a background worker in GUI thread which keep looping and checks ifqueque
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 :)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
-
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
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"; });
}
} -
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
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?