private void saveFile()
 {
     if (saving)
     {
         if (new_save_scheduled) return;
         new_save_scheduled = true;
         new System.Threading.Timer(new System.Threading.TimerCallback(delegate { saveFile(); }), null, 1000, System.Threading.Timeout.Infinite);
         return;
     }
     saving = true;
     new_save_scheduled = false;
     DocumentOpener.op_mutex.WaitOne();
     DocumentOpener.op_win.Invoke((MethodInvoker)delegate
     {
         DocumentOpener.op_win.text.Text = "Saving file " + filename;
         DocumentOpener.op_win.progressBar.Value = 0;
         DocumentOpener.op_win.progressBar.Visible = true;
         DocumentOpener.op_win.TopLevel = true;
         DocumentOpener.op_win.Show();
     });
     string tmp_path = DocumentOpener.app_path + "/" + this.storage_id + "/tmp_save."+(save_count++);
     System.IO.FileInfo tmp_info;
     long time;
     try
     {
         time = System.IO.File.GetLastWriteTime(file_path).Ticks;
         for (int i = 0; i < 10; i++)
         {
             try { copyCool(file_path, tmp_path); break; }
             catch (Exception e)
             {
                 if (i == 9) throw e;
                 Thread.Sleep(100);
             }
         }
         tmp_info = new System.IO.FileInfo(tmp_path);
     }
     catch (Exception e)
     {
         DocumentOpener.op_win.Invoke((MethodInvoker)delegate
         {
             DocumentOpener.op_win.Hide();
         });
         DocumentOpener.op_mutex.ReleaseMutex();
         MessageBox.Show("Error saving file: unable to read content of the file (" + e.Message + ")", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
         saving = false;
         return;
     }
     DocumentOpener.op_win.Invoke((MethodInvoker)delegate
     {
         DocumentOpener.op_win.progressBar.Maximum = (int)tmp_info.Length;
     });
     last_change = time;
     DocumentUpload up = new DocumentUpload(this, tmp_path, tmp_info);
     try
     {
         System.IO.File.Delete(tmp_path);
     }
     catch (Exception) { }
     DocumentOpener.op_win.Invoke((MethodInvoker)delegate
     {
         DocumentOpener.op_win.Hide();
     });
     DocumentOpener.op_mutex.ReleaseMutex();
     saving = false;
     if (up.error != null)
         MessageBox.Show(up.error, "PN Document Opener - Error while saving to server", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
     else
     {
         if (icon == null)
         {
             icon = new NotifyIcon();
             DocumentOpener.op_win.Invoke((MethodInvoker)delegate
             {
                 icon.Icon = System.Drawing.SystemIcons.Information;
                 icon.Visible = true;
                 icon.ShowBalloonTip(3000, "PN Documents", "File " + filename + " successfully saved to the server.", ToolTipIcon.Info);
                 icon.BalloonTipClosed += delegate
                 {
                     NotifyIcon i = icon; icon = null;
                     if (i != null)
                     {
                         i.Visible = false;
                         i.Dispose();
                     }
                 };
                 new System.Threading.Timer(delegate {
                     NotifyIcon i = icon; icon = null;
                     if (i != null)
                     {
                         i.Visible = false;
                         i.Dispose();
                     }
                 }, null, 3000, Timeout.Infinite);
             });
         }
     }
 }
        private void saveFile()
        {
            if (saving)
            {
                if (new_save_scheduled)
                {
                    return;
                }
                new_save_scheduled = true;
                new System.Threading.Timer(new System.Threading.TimerCallback(delegate { saveFile(); }), null, 1000, System.Threading.Timeout.Infinite);
                return;
            }
            saving             = true;
            new_save_scheduled = false;
            DocumentOpener.op_mutex.WaitOne();
            DocumentOpener.op_win.Invoke((MethodInvoker) delegate
            {
                DocumentOpener.op_win.text.Text           = "Saving file " + filename;
                DocumentOpener.op_win.progressBar.Value   = 0;
                DocumentOpener.op_win.progressBar.Visible = true;
                DocumentOpener.op_win.TopLevel            = true;
                DocumentOpener.op_win.Show();
            });
            string tmp_path = DocumentOpener.app_path + "/" + this.storage_id + "/tmp_save." + (save_count++);

            System.IO.FileInfo tmp_info;
            long time;

            try
            {
                time = System.IO.File.GetLastWriteTime(file_path).Ticks;
                for (int i = 0; i < 10; i++)
                {
                    try { copyCool(file_path, tmp_path); break; }
                    catch (Exception e)
                    {
                        if (i == 9)
                        {
                            throw e;
                        }
                        Thread.Sleep(100);
                    }
                }
                tmp_info = new System.IO.FileInfo(tmp_path);
            }
            catch (Exception e)
            {
                DocumentOpener.op_win.Invoke((MethodInvoker) delegate
                {
                    DocumentOpener.op_win.Hide();
                });
                DocumentOpener.op_mutex.ReleaseMutex();
                MessageBox.Show("Error saving file: unable to read content of the file (" + e.Message + ")", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                saving = false;
                return;
            }
            DocumentOpener.op_win.Invoke((MethodInvoker) delegate
            {
                DocumentOpener.op_win.progressBar.Maximum = (int)tmp_info.Length;
            });
            last_change = time;
            DocumentUpload up = new DocumentUpload(this, tmp_path, tmp_info);

            try
            {
                System.IO.File.Delete(tmp_path);
            }
            catch (Exception) { }
            DocumentOpener.op_win.Invoke((MethodInvoker) delegate
            {
                DocumentOpener.op_win.Hide();
            });
            DocumentOpener.op_mutex.ReleaseMutex();
            saving = false;
            if (up.error != null)
            {
                MessageBox.Show(up.error, "PN Document Opener - Error while saving to server", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
            }
            else
            {
                if (icon == null)
                {
                    icon = new NotifyIcon();
                    DocumentOpener.op_win.Invoke((MethodInvoker) delegate
                    {
                        icon.Icon    = System.Drawing.SystemIcons.Information;
                        icon.Visible = true;
                        icon.ShowBalloonTip(3000, "PN Documents", "File " + filename + " successfully saved to the server.", ToolTipIcon.Info);
                        icon.BalloonTipClosed += delegate
                        {
                            NotifyIcon i = icon; icon = null;
                            if (i != null)
                            {
                                i.Visible = false;
                                i.Dispose();
                            }
                        };
                        new System.Threading.Timer(delegate {
                            NotifyIcon i = icon; icon = null;
                            if (i != null)
                            {
                                i.Visible = false;
                                i.Dispose();
                            }
                        }, null, 3000, Timeout.Infinite);
                    });
                }
            }
        }