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
Without taking into account needing to update a GUI (which would require doing a control.Invoke()), here is a very easy way to run something in the background using the ThreadPool. The great thing about the thread pool is that you don't have to worry about creating, disposing and keeping track of threads.
static void Main(string[] args)
{
lacie BackupDrive = new lacie();
BackupDrive.findLacie();
xml xmlFile = new xml();
xmlFile.ProcessXML();
size BackupSize = new size();
System.Threading.ThreadPool.QueueUserWorkItem(s =>
{
BackupSize.GetSize(xmlFile.Path);
});
int SizeofBackup = (int)(((BackupSize.BackupSize) / 1024f) / 1024f) / 1024;
Console.WriteLine("Drive Letter: " + BackupDrive.Drive);
Console.WriteLine("Volume Name: " + BackupDrive.VolumeLabel);
Console.WriteLine("Free Space: " + Convert.ToString(BackupDrive.AvailableSize) + "G");
Console.WriteLine("Size of Lacie: " + Convert.ToString(BackupDrive.TotalSize) + "G");
Console.WriteLine("Backup Size: " + Convert.ToString(SizeofBackup + "G"));
Console.WriteLine("Backing up " + BackupSize.FileCount + " files found in " + BackupSize.FolderCount + " folders.");
Console.ReadKey(true);
}
You could add something else to the thread pool that just kept writing to the console like this:
System.Threading.ThreadPool.QueueUserWorkItem(s =>
{
while (true) // might want a real condition like while(!backupNotDone)
{
int SizeofBackup = (int) (((BackupSize.BackupSize)/1024f)/1024f)/1024;
Console.WriteLine("Backup Size: " + Convert.ToString(SizeofBackup + "G"));
}
});