Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

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

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,

Posted

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:

 

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 :)

Never trouble another for what you can do for yourself.
  • 1 year later...
Posted

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

Posted

What is the code that is not working?

 

You might try something like

progressFormWithOKButton.Show();

at the end of the method that does the database work.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...