BackgroundWorker and comboBox.Items.Add(f)
-
Hi, I'm new to Threading and WPF and need some help Adding to my Combo Box. I Understand I can't touch the UI from a background thread I just cant seem to figure out the work around. I got This Far. You'll see a Combo1.Items.Add(f); That is throwing. If I can get a good example of how you are suppose to do something like this I'll have a chunk of code I can play with and learn from. Thank you very much For any help
private void button1_Click(object sender, RoutedEventArgs e)
{
string Server;
Server = cmdServers.SelectedItem.ToString();
FillApplicationCombo(Server);
}void FillApplicationCombo( string Server)
{
int count;
string Path;
int Version;
string AppName;Path = @"\\\\" + Server + @"\\" + "DOCFOCUS"; progressBar1.Minimum = 0; progressBar1.Maximum = \_DirectoryFiles.Count; \_Worker = new BackgroundWorker(); \_Worker.WorkerReportsProgress = true; \_Worker.WorkerSupportsCancellation = true; \_Worker.DoWork += (s, args) => { BackgroundWorker worker = s as BackgroundWorker; DirSearch(Path); //Gets All Files in the Path count = 0; foreach (string f in \_DirectoryFiles) { count++; worker.ReportProgress(count); AppName = FileVersionInfo.GetVersionInfo(f).ProductName; if (AppName == null) continue; if (AppName.ToUpper().IndexOf("MYAPPNAME") == -1) continue; Version = FileVersionInfo.GetVersionInfo(f).ProductMajorPart;//FileVersion; if (Version == 6) { Combo1.Items.Add(f);//What should I do about this? } } }; \_Worker.RunWorkerCompleted += (s, args) => { progressBar1.Value = 0; }; \_Worker.ProgressChanged += (s, args) => { progressBar1.Value = args.ProgressPercentage; }; \_Worker.RunWorkerAsync(); }
Ronald Hahn, CN
-
Hi, I'm new to Threading and WPF and need some help Adding to my Combo Box. I Understand I can't touch the UI from a background thread I just cant seem to figure out the work around. I got This Far. You'll see a Combo1.Items.Add(f); That is throwing. If I can get a good example of how you are suppose to do something like this I'll have a chunk of code I can play with and learn from. Thank you very much For any help
private void button1_Click(object sender, RoutedEventArgs e)
{
string Server;
Server = cmdServers.SelectedItem.ToString();
FillApplicationCombo(Server);
}void FillApplicationCombo( string Server)
{
int count;
string Path;
int Version;
string AppName;Path = @"\\\\" + Server + @"\\" + "DOCFOCUS"; progressBar1.Minimum = 0; progressBar1.Maximum = \_DirectoryFiles.Count; \_Worker = new BackgroundWorker(); \_Worker.WorkerReportsProgress = true; \_Worker.WorkerSupportsCancellation = true; \_Worker.DoWork += (s, args) => { BackgroundWorker worker = s as BackgroundWorker; DirSearch(Path); //Gets All Files in the Path count = 0; foreach (string f in \_DirectoryFiles) { count++; worker.ReportProgress(count); AppName = FileVersionInfo.GetVersionInfo(f).ProductName; if (AppName == null) continue; if (AppName.ToUpper().IndexOf("MYAPPNAME") == -1) continue; Version = FileVersionInfo.GetVersionInfo(f).ProductMajorPart;//FileVersion; if (Version == 6) { Combo1.Items.Add(f);//What should I do about this? } } }; \_Worker.RunWorkerCompleted += (s, args) => { progressBar1.Value = 0; }; \_Worker.ProgressChanged += (s, args) => { progressBar1.Value = args.ProgressPercentage; }; \_Worker.RunWorkerAsync(); }
Ronald Hahn, CN
You have to setup a delegate,
private delegate void DelegateUpdateCombo();
create a method that matches it,
private void MyMethod()
{
// do something
}and then use this in your thread event handler:
DelegateUpdateCombo method = new DelegateUpdateCombo(MyMethod);
comboBox.Dispatcher.Invoke(myMethod);.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
You have to setup a delegate,
private delegate void DelegateUpdateCombo();
create a method that matches it,
private void MyMethod()
{
// do something
}and then use this in your thread event handler:
DelegateUpdateCombo method = new DelegateUpdateCombo(MyMethod);
comboBox.Dispatcher.Invoke(myMethod);.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001