views:

274

answers:

2

How can I make the FileOpenDialog disappear?

private void ofdAttachment_FileOk(object sender, CancelEventArgs e)
{            

    string fullFilename = ofdAttachment.FileName;
    string filename = Path.GetFileName(fullFilename);
    string dirName = Path.GetDirectoryName(fullFilename);


    this.Parent.Refresh();
    this.Refresh();


    var drv = bdsAttachments.AddNew() as DataRowView;


    var fze = new FastZipEvents();
    fze.ProgressInterval = new TimeSpan(0, 0, 0, 0, 250);
    fze.Progress = new ICSharpCode.SharpZipLib.Core.ProgressHandler(
    (object o, ICSharpCode.SharpZipLib.Core.ProgressEventArgs ex) =>
    {                    
        drv["filename"] = "Compressing: " 
            + ex.PercentComplete.ToString() + "%";
        grdAttachments.Refresh();
        this.Refresh(); // this doesn't work either                    
        Application.DoEvents(); // re: Aamir's answer, neither work
        this.Refresh();

    }
    );



    var ba = new FastZip(fze).CreateZipToArray(dirName, false, filename, null);

    drv["filename"] = filename;
    drv["file_zip_image"] = ba;

    grdAttachments.Refresh();

}

[EDIT: Solved]

using the fire-and-forget approach:

private void ofdAttachment_FileOk(object sender, CancelEventArgs e)
{            
    System.Threading.ThreadPool.QueueUserWorkItem((o) => Attach());
}

void Attach()
{

    if (this.InvokeRequired)
    {
     this.Invoke(new Action(Attach));    
    }
    else
    {

     this.Parent.Refresh();
     this.Refresh();

     string fullFilename = ofdAttachment.FileName;
     string filename = Path.GetFileName(fullFilename);
     string dirName = Path.GetDirectoryName(fullFilename);



     var drv = bdsAttachments.AddNew() as DataRowView;


     var fze = new FastZipEvents();
     fze.ProgressInterval = new TimeSpan(0, 0, 0, 0, 250);
     fze.Progress = new ICSharpCode.SharpZipLib.Core.ProgressHandler(
      (object o, ICSharpCode.SharpZipLib.Core.ProgressEventArgs ex) =>
      {
      drv["filename"] = "Compressing: " 
         + ex.PercentComplete.ToString() + "%";
      grdAttachments.Refresh();                        
      }
     );



     var ba = new FastZip(fze).CreateZipToArray(dirName, false, 
                    filename, null);

     drv["filename"] = filename;
     drv["file_zip_image"] = ba;

     grdAttachments.Refresh();
    }

}
+4  A: 

It looks like your file compression is a long running process which prevents the form from repainting until the compression has finished. If you use a BackgroundWorker object and do the compression routine on a background thread, the UI thread will be available for the form to use for painting.

BackgroundWorker info: http://msdn.microsoft.com/en-us/library/8xs8549b.aspx

AndrewS
There are ways to post the progress state from the worker thread too
ChrisF
+2  A: 

You can call Application.DoEvents() to achieve this.

Aamir
No. No no no no no NO. This is one of those solutions that can come back and bite you in the ass in strange ways later on. Just don't do it. It will definitely fire off Paint, and also any other event that it can. That causes strangeness.
aehiilrs