none
How to stop the File saving process while it is in progress?

    常规讨论

  • I have a button which user press to save file. Say, saving file is in progress. How do I provide a choice to user to Stop the saving file process? here is my code
    public void  SaveFile (string strFilenm)
    { try { using (var isf = IsolatedStorageFile.GetUserStoreForApplication()) { if (!isf.DirectoryExists("MyDoc")) isf.CreateDirectory("MyDoc"); StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("MyDoc\\" + strFilenm, FileMode.OpenOrCreate, isf)); writeFile.WriteLine(txtBlkExplanation.Text); writeFile.Close(); MessageBox.Show("This Message has been saved."); } } catch (Exception ex) { MessageBox.Show("Please Try Again. Error :" + ex.Message); } }
    
    Thanks
    
    
    
    2011年4月10日 2:39

全部回复

  • Trying to add such an option may be overkill. Unless you've got a huge chunk of text to write the time needed to save the file would be so short that to provide the user with an option to cancel the save would be inpractical.

    If by some chance you happen to have enough data to write such that it took a while then you would have to write it in chunks (not all at once) and after writing each chunk check a flag to see if a requests to stop has been received. Note this also means that you would need to be performing the save on a secondary thread.
    2011年4月10日 7:50
  • Hi,
    I totally agreed with you. I have checked my code and time required to save. The ISO saving process is so fast that I dont have time to react after pressing save button to see any progress indicator on page.
    In My UI, I provide these :
    a) a dialogue Box to user to confirm if he or she wants to save the files.
    b) UI such as :
    During saving process:
    1. TextBlockMsg.Text = " File saving in progress..."
    After saving process:
    2. TextBlockMsg.Text = " File saving completed."

    I want to know, If I have provided these UI (b) as an indicator to user, would this be good enough? I try to provide a progress bar but the ISO saving process is so fast for saving multiple files at once that I dont have a chance to see the progress bar in action.

    Now, suppose I need a button to stop the File Saving Process. How do I do it? If there any file saving stopping function in ISO ??
    btnClose_Click(object sender, RoutedEventArgs e) { // Code to completely stop file saving ?? }
    Thanks
    2011年4月10日 8:52
  • close the filestream and handle the exception ... that's not the gentle way it should be. :D
    2011年4月10日 12:32
  • suppose I need a button to stop the File Saving Process


    You would have to do two things:
    a) move the file saving operation off the UI thread onto a background thread
    b) perform the saving in chunks, as opposed to a single file operation

    Now you can have a button in the UI that signals the background thread to stop the writing.

    Thanks,
    Stefan Wick - Microsoft Silverlight
    2011年4月10日 16:29
  • Another approach is to use FileStream instead of StreamWriter. FileStream has asynchronous capability and you can stop its operations.
    2011年4月10日 18:45
  • suppose I need a button to stop the File Saving Process


    You would have to do two things:
    a) move the file saving operation off the UI thread onto a background thread
    b) perform the saving in chunks, as opposed to a single file operation

    Now you can have a button in the UI that signals the background thread to stop the writing.

    Thanks,
    Stefan Wick - Microsoft Silverlight
    Thank you for the info. This sounds like a huge task to me. Currently what I do is to save small size files using IsolatedStorage filesStream and others. This I can do. With respect to what you descirbe, I think I am very new to it. Would appreciate if you could show some sample and code to learn. Thanks.
    2011年4月11日 0:14