0

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();
    }

}
Community
  • 1
  • 1
Hao
  • 8,047
  • 18
  • 63
  • 92
  • Can you be a bit more specific about what yo are trying to do? – ChrisF Jun 24 '09 at 09:09
  • The program should make the fileopendialog's box disappear, it's a bit amateurish-looking if the filedialog box is still on screen while the progress bar is updating itself – Hao Jun 24 '09 at 09:15

2 Answers2

4

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
  • 3,450
  • 1
  • 21
  • 26
1

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

Aamir
  • 14,882
  • 6
  • 45
  • 69
  • 3
    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 Jun 24 '09 at 17:58