|
-
Jul 25th, 2007, 02:53 PM
#1
Thread Starter
Hyperactive Member
[1.0/1.1] How to display file transfer progress [C# 2002]
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,
-
Jul 25th, 2007, 03:50 PM
#2
Re: [1.0/1.1] How to display file transfer progress [C# 2002]
Hi there,
I think you may only need to repaint your fFileTransfer form. Try a
Code:
fFileTransfer.Refresh();
in your loop.
Hope that helps.
-
Jul 25th, 2007, 04:47 PM
#3
Sleep mode
Re: [1.0/1.1] How to display file transfer progress [C# 2002]
Did you try using "Application.DoEvents" ?...This helped me in some issues with halted forms !
-
Jul 25th, 2007, 05:06 PM
#4
Re: [1.0/1.1] How to display file transfer progress [C# 2002]
The best way would be to run your code in the DoWork subroutine of a BackgroundWorker component. You'd use the ReportProgress method to communicate with the main thread.
-
Jul 25th, 2007, 08:19 PM
#5
Re: [1.0/1.1] How to display file transfer progress [C# 2002]
.NET 1.0 has no BackgroundWorker. You should perform the file transfer in a worker thread and then use delegation to access the UI.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|