I am learning to program in C# and have most of the basics down already. I am having trouble using the background worker and using it with multiple classes. This is a backup pro
As your question requests, this shows you how to use the background worker component. This should suffice, however, when you get into more complex thread usage, it'd be better to write something like aaronls has provided.
Your Size class should hold the backgroundWorker thread and BackupSize.GetSize("PathHere")
should make an asynchronous call to start the background worker thread.
in your do work method you can report progress of the work by calling backgroundWorker.ReportProgress(i);
Inside of your ReportProgress delegate you can create an event notification that can be hooked in to by your main gui
class main
{
static void Main(string[] args)
{
size BackupSize = new size();
BackupSize.GetSize("path");
BackupSize.ProgressEvent += new ProgressEventHandler(BackupSize_ProgressEvent);
// BackupSize.BackupSize will not be accurate until the thread is finished.
// You may want to take that into consideration
int SizeofBackup = (int)(((BackupSize.BackupSize) / 1024f) / 1024f) / 1024;
Console.ReadLine();
}
static void BackupSize_ProgressEvent(object source, int progress)
{
Console.WriteLine(String.Format("Progress: {0}", progress));
}
}
// This is the delegate that acts as the event handler for your progress events
public delegate void ProgressEventHandler(object source, int progress);
public class size
{
private readonly BackgroundWorker backgroundWorker;
public event ProgressEventHandler ProgressEvent;
public size()
{
backgroundWorker = new BackgroundWorker { WorkerReportsProgress = true };
backgroundWorker.DoWork += backgroundWorker_DoWork;
backgroundWorker.ProgressChanged += backgroundWorker_ProgressChanged;
}
public void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// do something with progress
ProgressEvent.Invoke(sender, e.ProgressPercentage);
}
void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
String Path = e.Argument as String;
// Do something with Path;
// Simulate work
for (int i = 1; i <= 100; i++)
{
// Get the size of the Here
// Report the Progress
backgroundWorker.ReportProgress(i);
Thread.Sleep(10);
}
}
public void GetSize(String path)
{
backgroundWorker.RunWorkerAsync(path);
}
}