I wrote an application that synchronizes two folders together. The problem with the program is that it stops responding when copying files. A quick search told me that I needed to use something called a background worker. I read several pages on the net about this, but it is very difficult for me to understand, since I am quite new to programming. How can I just put all the File.Copy commands (....) into my own desktop (if that even works)? The following is the event code of the button that executes the helper procedure and the helper procedure that I want to use as a background worker for all lines of File.Copy.
Any help would be greatly appreciated, since after that the program will be largely completed: D
EDIT
Using the Veer recommendations below, I changed my code, however, I continue to receive the following errors:
Error CS1061: enter Gtk.ProgressBar' does not contain a definition forInvokeRequired 'and the Gtk.ProgressBar' extension method will not be used InvokeRequired' of type(do you miss the using directive or assembly references?) (CS1061) (Sync-GUI v2)
Error CS1061: Type Gtk.ProgressBar' does not contain a definition forBeginInvoke 'and the extension method BeginInvoke' of typeGtk.ProgressBar' cannot be found (do you miss the using directive or assembly references?) (CS1061) (Sync-GUI v2)
Below is my code.
Click the Event button:
protected virtual void OnBtnSyncClicked (object sender, System.EventArgs e)
{
prgProgressBar.Fraction = 0;
dblCurrentStatus = 0;
dblFolderSize = 0;
if (fchFolder1.CurrentFolder == fchFolder2.CurrentFolder)
{
MessageDialog msdSame = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Close, "You cannot sync two folders that are the same");
msdSame.Title="Error";
ResponseType response = (ResponseType) msdSame.Run();
if (response == ResponseType.Close || response == ResponseType.DeleteEvent) {
msdSame.Destroy();
}
return;
}
if (fchFolder1.CurrentFolder.StartsWith(fchFolder2.CurrentFolder) || fchFolder2.CurrentFolder.StartsWith(fchFolder1.CurrentFolder))
{
MessageDialog msdContains = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Close, "You cannot sync a folder with one of its parent folders");
msdContains.Title="Error";
ResponseType response = (ResponseType) msdContains.Run();
if (response == ResponseType.Close || response == ResponseType.DeleteEvent)
{
msdContains.Destroy();
}
return;
}
BackgroundWorker bwBackgroundWorker = new BackgroundWorker();
bwBackgroundWorker.DoWork += new DoWorkEventHandler(bwBackgroundWorkerDoWorkFolder1);
bwBackgroundWorker.RunWorkerAsync();
prgProgressBar.Text = "Finished";
}
Background worker:
private void bwBackgroundWorkerDoWorkFolder1 (object sender, DoWorkEventArgs e)
{
TotalFileSizeFolder1(fchFolder1.CurrentFolder);
SyncFolder1(fchFolder1.CurrentFolder, fchFolder2.CurrentFolder);
}
TotalFileSizeFolder1 Subroutine:
protected void TotalFileSizeFolder1 (string strCurrentDirectory)
{
if (prgProgressBar.InvokeRequired)
{
prgProgressBar.BeginInvoke(new MethodInvoker(delegate {prgProgressBar.Text="Getting total size of " + strCurrentDirectory;}));
}
string[] staAllDirectories = Directory.GetDirectories(strCurrentDirectory);
string[] staAllFiles = Directory.GetFiles(strCurrentDirectory);
foreach (string strFile in staAllFiles)
{
FileInfo FileSize = new FileInfo(strFile);
dblFolderSize = dblFolderSize + FileSize.Length;
if (prgProgressBar.InvokeRequired)
{
prgProgressBar.BeginInvoke(new MethodInvoker(delegate {prgProgressBar.Pulse();}));
}
}
foreach (string strDirectory in staAllDirectories)
{
TotalFileSize(strDirectory);
}
if (prgProgressBar.InvokeRequired)
{
prgProgressBar.BeginInvoke(new MethodInvoker(delegate {prgProgressBar.Text="";}));
}
}
SyncFolder1 Subroutine:
protected void SyncFolder1 (string strFolder1, string strFolder2)
{
string[] staAllDirectories = Directory.GetDirectories(strFolder1);
string[] staAllFiles = Directory.GetFiles(strFolder1);
foreach (string strFile in staAllFiles)
{
string strFileName = System.IO.Path.GetFileName(strFile);
string strDirectoryInsideFolder1 = System.IO.Path.GetDirectoryName(strFile).Substring(strFolder1.Length);
if (prgProgressBar.InvokeRequired)
{
prgProgressBar.BeginInvoke(new MethodInvoker(delegate {prgProgressBar.Text="Syncing " + strFile;}));
}
if (!File.Exists(fchFolder2.CurrentFolder + "/" + strDirectoryInsideFolder1 + "/" + strFileName))
{
File.Copy (strFile, strFolder2 + "/" + strDirectoryInsideFolder1 + "/" + strFileName, true);
}
if (File.Exists(strFolder2 + "/" + strDirectoryInsideFolder1 + "/" + strFileName))
{
long lngFolder1FileDate = File.GetLastWriteTime(strFile).ToFileTime();
long lngFolder2FileDate = File.GetLastWriteTime(strFolder2 + "/" + strDirectoryInsideFolder1 + "/" + strFileName).ToFileTime();
if (lngFolder1FileDate > lngFolder2FileDate)
{
File.Copy (strFile, strFolder2 + "/" + strDirectoryInsideFolder1 + "/" + strFileName, true);
}
}
FileInfo FileSize = new FileInfo(strFile);
dblCurrentStatus = dblCurrentStatus + FileSize.Length;
double dblPercentage = dblCurrentStatus/dblFolderSize;
if (prgProgressBar.InvokeRequired)
{
prgProgressBar.BeginInvoke(new MethodInvoker(delegate {prgProgressBar.Fraction = dblPercentage;}));
}
}
foreach (string strDirectory in staAllDirectories)
{
string strDirectoryInsideFolder1 = strDirectory.Substring(strFolder1.Length);
if (!Directory.Exists(strFolder2 + "/" + strDirectoryInsideFolder1))
{
Directory.CreateDirectory(strFolder2 + "/" + strDirectoryInsideFolder1);
}
SyncFolders(strDirectory, strFolder2);
}
}