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