After recieving the cross thread exception error, I looked it up on the MSDN.
I tried implementing some of the code there but can't get the Callback to work.
addItemCallback d = new addItemCallback(addItem);
this is located in the addItem() method below.
I am trying to have a list of items add themselves to a listbox, eventually updating the form everytime one value is found, rather than all of them being added once the backgrounder has finished work.
private void startWork()
{
progressBar1.Value = 0;
progressBar1.Maximum = 901242;
backgroundWorker1.RunWorkerAsync();
}
private void getList()
{
if (pathFound)
{
for (int i = 0; i < numberOfPaths; i++)
{
Microsoft.Win32.RegistryKey mainPath = secondaryPath.OpenSubKey("application " + Convert.ToString(i));
if (mainPath != null)
{
this.addItem((string)mainPath.GetValue("Name"));
}
backgroundWorker1.ReportProgress(i);
}
}
pathListBox.Sorted = true;
}
private void addItem(string item)
{
if (this.pathListBox.InvokeRequired)
{
//addItemCallback d = new addItemCallback(addItem);
//not sure what this callBack is, can't get it to work, Callback isnt found.
this.Invoke(d, new object[] { item });
}
else
{
this.pathListBox.Items.Add(item);
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
getList();
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.progressBar1.Visible = false;
}
WORK SO FAR
Code 1. When I use the background_doWork and call a method from it, the progressbar hangs in random spots and stops responding, on closing the form i get an object exception as I've closed the form while it is still trying to do work.
Code 2. When I place all the code in the background_doWork rather than call a method from it, the progress bar will work sometimes, every second or every 3rd attempt at running the program it finishes.
What would be causing this?
-----CODE 1-----------
public Form1()
{
InitializeComponent();
start();
}
int number = 900000;
public void start()
{
progressBar1.Value = 0;
progressBar1.Maximum = number;
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
getList();
}
private void getList()
{
Microsoft.Win32.RegistryKey mainPath = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node");
for (int i = 0; i < number; i++)
{
Microsoft.Win32.RegistryKey mainPath = secondaryPath.OpenSubKey("application " + Convert.ToString(i));
if (mainPath != null)
{
this.addItem((string)mainPath.GetValue("Name"));
}
backgroundWorker1.ReportProgress(i);
}
}
private void addItem(string item)
{
try
{
if (this.listBox1.InvokeRequired)
{
this.Invoke(new Action<string>(addItem), item);
}
else
{
this.listBox1.Items.Add(item);
}
}
catch
{
MessageBox.Show("Error - Closed Object before it finished working.");
}
//this.steamGamesListBox.Sorted = true;
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.progressBar1.Visible = false;
}
------CODE 2 --------
public Form1()
{
InitializeComponent();
start();
}
int number = 900000;
public void start()
{
progressBar1.Value = 0;
progressBar1.Maximum = number;
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Microsoft.Win32.RegistryKey steamApps64 = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall");
for (int i = 0; i < number; i++)
{
Microsoft.Win32.RegistryKey steamApps = steamApps64.OpenSubKey("Steam App " + Convert.ToString(i));
if (steamApps != null)
{
this.addItem((string)steamApps.GetValue("DisplayName"));
}
backgroundWorker1.ReportProgress(i);
}
}
private void addItem(string item)
{
try
{
if (this.listBox1.InvokeRequired)
{
this.Invoke(new Action<string>(addItem), item);
}
else
{
this.listBox1.Items.Add(item);
}
}
catch
{
MessageBox.Show("Error - Closed Object before it finished working.");
}
//this.steamGamesListBox.Sorted = true;
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.progressBar1.Visible = false;
}