public void run()
 {
     if (System.IO.Directory.Exists(DocumentOpener.app_path + "/" + this.storage_id))
     {
         MessageBox.Show("The file "+this.filename+" is already open.", "File already open", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
         return;
     }
     DocumentOpener.op_mutex.WaitOne();
     DocumentOpener.op_win.Invoke((MethodInvoker)delegate
     {
         DocumentOpener.op_win.text.Text = "";
         DocumentOpener.op_win.progressBar.Visible = false;
         DocumentOpener.op_win.TopLevel = true;
         DocumentOpener.op_win.Show();
     });
     if (!readOnly)
     {
         DocumentOpener.op_win.Invoke((MethodInvoker)delegate
         {
             DocumentOpener.op_win.text.Text = "Asking to edit "+filename;
         });
         doc_lock = new DocumentLock(this);
         if (doc_lock.error != null)
         {
             DocumentOpener.op_win.Invoke((MethodInvoker)delegate
             {
                 DocumentOpener.op_win.Hide();
             });
             DocumentOpener.op_mutex.ReleaseMutex();
             MessageBox.Show(doc_lock.error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
             return;
         }
     }
     DocumentDownload download = new DocumentDownload(this);
     last_change = System.IO.File.GetLastWriteTime(file_path).Ticks;
     DocumentOpener.op_win.Invoke((MethodInvoker)delegate
     {
         DocumentOpener.op_win.Hide();
     });
     DocumentOpener.op_mutex.ReleaseMutex();
     if (download.error != null)
     {
         MessageBox.Show(download.error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
         download = null;
         return;
     }
     download = null;
     if (!readOnly)
     {
         monitor = new System.IO.FileSystemWatcher();
         monitor.Path = DocumentOpener.app_path + "/" + this.storage_id;
         monitor.NotifyFilter = System.IO.NotifyFilters.CreationTime | System.IO.NotifyFilters.LastAccess | System.IO.NotifyFilters.LastWrite | System.IO.NotifyFilters.Size | System.IO.NotifyFilters.FileName;
         //monitor.NotifyFilter = System.IO.NotifyFilters.LastWrite;
         //monitor.Filter = this.filename;
         monitor.Changed += docChanged;
         monitor.Renamed += docChanged;
         monitor.Created += docChanged;
         monitor.Deleted += docChanged;
         monitor.EnableRaisingEvents = true;
     }
     if (process != null)
     {
         if (process.HasExited)
             app_closed(null, null);
         else
             process.Exited += app_closed;
     }
     while (!closed)
     {
         Thread.Sleep(1000);
         if (!closed)
         {
             try
             {
                 System.IO.File.Open(file_path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.None).Close();
                 app_closed(null,null);
                 break;
             }
             catch (Exception) {
                 if (doc_lock != null && doc_lock.updater.error != null)
                 {
                     readOnly = true;
                     monitor.Changed -= docChanged;
                     monitor.EnableRaisingEvents = false;
                     monitor.Dispose();
                     monitor = null;
                     if (process != null && !process.HasExited)
                     {
                         for (int i = 0; i < 10 && !process.HasExited; i++)
                         {
                             try { process.Kill(); }
                             catch (Exception) { }
                             Thread.Sleep(500);
                         }
                     }
                     app_closed(null, null);
                     break;
                 }
             }
         }
     }
     while (!closed) Thread.Sleep(100);
     if (doc_lock != null)
         doc_lock.unlock();
     doc_lock = null;
     monitor = null;
     thread = null;
     process = null;
 }
Exemplo n.º 2
0
        public void run()
        {
            if (System.IO.Directory.Exists(DocumentOpener.app_path + "/" + this.storage_id))
            {
                MessageBox.Show("The file " + this.filename + " is already open.", "File already open", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                return;
            }
            DocumentOpener.op_mutex.WaitOne();
            DocumentOpener.op_win.Invoke((MethodInvoker) delegate
            {
                DocumentOpener.op_win.text.Text           = "";
                DocumentOpener.op_win.progressBar.Visible = false;
                DocumentOpener.op_win.TopLevel            = true;
                DocumentOpener.op_win.Show();
            });
            if (!readOnly)
            {
                DocumentOpener.op_win.Invoke((MethodInvoker) delegate
                {
                    DocumentOpener.op_win.text.Text = "Asking to edit " + filename;
                });
                doc_lock = new DocumentLock(this);
                if (doc_lock.error != null)
                {
                    DocumentOpener.op_win.Invoke((MethodInvoker) delegate
                    {
                        DocumentOpener.op_win.Hide();
                    });
                    DocumentOpener.op_mutex.ReleaseMutex();
                    MessageBox.Show(doc_lock.error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                    return;
                }
            }
            DocumentDownload download = new DocumentDownload(this);

            last_change = System.IO.File.GetLastWriteTime(file_path).Ticks;
            DocumentOpener.op_win.Invoke((MethodInvoker) delegate
            {
                DocumentOpener.op_win.Hide();
            });
            DocumentOpener.op_mutex.ReleaseMutex();
            if (download.error != null)
            {
                MessageBox.Show(download.error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                download = null;
                return;
            }
            download = null;
            if (!readOnly)
            {
                monitor              = new System.IO.FileSystemWatcher();
                monitor.Path         = DocumentOpener.app_path + "/" + this.storage_id;
                monitor.NotifyFilter = System.IO.NotifyFilters.CreationTime | System.IO.NotifyFilters.LastAccess | System.IO.NotifyFilters.LastWrite | System.IO.NotifyFilters.Size | System.IO.NotifyFilters.FileName;
                //monitor.NotifyFilter = System.IO.NotifyFilters.LastWrite;
                //monitor.Filter = this.filename;
                monitor.Changed            += docChanged;
                monitor.Renamed            += docChanged;
                monitor.Created            += docChanged;
                monitor.Deleted            += docChanged;
                monitor.EnableRaisingEvents = true;
            }
            if (process != null)
            {
                if (process.HasExited)
                {
                    app_closed(null, null);
                }
                else
                {
                    process.Exited += app_closed;
                }
            }
            while (!closed)
            {
                Thread.Sleep(1000);
                if (!closed)
                {
                    try
                    {
                        System.IO.File.Open(file_path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.None).Close();
                        app_closed(null, null);
                        break;
                    }
                    catch (Exception) {
                        if (doc_lock != null && doc_lock.updater.error != null)
                        {
                            readOnly                    = true;
                            monitor.Changed            -= docChanged;
                            monitor.EnableRaisingEvents = false;
                            monitor.Dispose();
                            monitor = null;
                            if (process != null && !process.HasExited)
                            {
                                for (int i = 0; i < 10 && !process.HasExited; i++)
                                {
                                    try { process.Kill(); }
                                    catch (Exception) { }
                                    Thread.Sleep(500);
                                }
                            }
                            app_closed(null, null);
                            break;
                        }
                    }
                }
            }
            while (!closed)
            {
                Thread.Sleep(100);
            }
            if (doc_lock != null)
            {
                doc_lock.unlock();
            }
            doc_lock = null;
            monitor  = null;
            thread   = null;
            process  = null;
        }