Using background worker with multiple classes in C#

前端 未结 5 1293
离开以前
离开以前 2021-01-22 05:35

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

5条回答
  •  深忆病人
    2021-01-22 06:28

    See if this could give you a clue on background worker. If this is in the windows form context, you like to use BeginInvoke of the control to update the UI by its owning thread. e.g. txtMessage.Invoke(UpdateMyMsg)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace QXBackup
    {
        class main
        {
            static void Main(string[] args)
            {
                var bgWorker = new BackgroundWorker();
                bgWorker.WorkerReportsProgress = true;
    
                bgWorker.DoWork += (sender, e) =>
                {
                    lacie BackupDrive = new lacie();
                    BackupDrive.findLacie();
    
                    xml xmlFile = new xml();
                    xmlFile.ProcessXML();
    
                    size BackupSize = new size();
                    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);
                };
    
                bgWorker.RunWorkerCompleted += (sender, e) => Console.WriteLine("completed...");
                bgWorker.ProgressChanged += (sender, e) => Console.WriteLine("progressing...");
    
    
                bgWorker.RunWorkerAsync();
            }
        }
    }
    

提交回复
热议问题