How to display file transfer progress [C# 2002]

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
This is going to be an age-old question - I have a form that will perform IO File Transfer of certain files (10-20 of them) and this can take up to 10 minutes, during the file transfer period I want to display to the user a form that indicates what file is currently being copied.

So - to that end - I created a new form (frmFileTransfer) that will display the File Name, Source, and Destination to the user while the IO File Copy is occuring - so I did something like this:

Main Form
Code:
this.hide();
frmFileTransfer fFileTransfer = new frmFileTransfer;
fFileTransfer.show();
foreach (string sFile in sFilesToCopy)
{	
	string sSource = ...;
	string sDestination = ...;
	fFileTransfer.SetDisplay(sFile, sSource, sDestination);
	File.Copy(sSource + sFile, sDestination + sFile, true);
}
fFileTransfer.close();
this.show();

File Transfer Form
Code:
public void SetDisplay(string sFile, string sSourcePath, string sDestinationPath)
{
	tbFileName.Text = sFileName;
	tbSource.Text = sSourcePath;
	tbDestination.Text = sDestinationPath;
}
Where tb = TextBox (to display the FileName, Source, and Destination to the User).

Most of you are already reading this going "doh this won't work" as I assume I am violating some thread laws but obviously this method ends up blocking (holding hostage) both forms (the calling one doing the file transfer as well as fFileTransfer) - what can I do to unblock fFileTransfer so that it can show the progress to the user without freezing up as it does now?

I thought forms where seperate threads so I don't see why fFileTransfer is blocked at all when the IO File Copy occurs....I thought of using a delegate I just have no clue how that could be implemented...

Any help would be greatly appreciated.
Thanks,
 
Delegates and the Invoke method


You should perform the file transfers on a separate thread, then use the Control.Invoke method to invoke a delegate which can perform any UI updates:

C#:
private void FileCopyThread()
{
    foreach (string sFile in sFilesToCopy) {	
        string sSource = ...;
        string sDestination = ...;
        
        //Invoke delegate on UI thread
        this.Invoke(new UpdateDisplayCallback(UpdateDisplay), sFile, sSource, sDestination);

        File.Copy(sSource + sFile, sDestination + sFile, true);
    }
}

private delegate void UpdateDisplayCallback(string fileName, string srcPath, string destPath);

private void UpdateDisplay(string fileName, string srcPath, string destPath)
{
    fFileTransfer.SetDisplay(fileName, srcPath, destPath);
}

Hopefully that demonstrates the general idea.

Good luck :)
 
i have a similar situation i'd like to pursue using C# 2005. let's say from the main app a user requests a series of database operations. i'd like to be able to update a separate small modal status/progress window as the operations start and finish. sorta like: Operation 1 has started...done!
Operation2 has started...done! etc.
and when all operations have completed, put the focus on the progress form and have the user be able to click an OK button to then close the status log.

i can simulate most of that functionality, except where i want the process to put the user at the OK button...that form simply closes when my processing is complete.

:confused:

thanks in advance
 
What is the code that is not working?

You might try something like
C#:
progressFormWithOKButton.Show();
at the end of the method that does the database work.
 
Back
Top