Beispiel #1
0
        // write given buffer to db
        // displays form to user to please wait
        // could make use of BackgroundWorker to prevent lockup, or async
        public void Write(WatcherEventBuffer buffer)
        {
            // get lock on db
            lock (lockObj) {
                // ensure the connection is alive first
                if (!AliveCheck())
                {
                    return;
                }

                // get buffer as new queue (to avoid having to keep is locked during DB write)
                Queue <WatcherEventArgs> queue = buffer.DequeueToQueue();

                // form to display to user while writing to db
                FormWriteWait waitForm = new FormWriteWait();
                waitForm.Show();
                waitForm.Refresh();

                try {
                    // create command obj
                    SQLiteCommand com = sqlite_connection.CreateCommand();

                    while (queue.Count > 0)
                    {
                        // dequque
                        WatcherEventArgs args = queue.Dequeue();
                        // generate insert and execute
                        com.CommandText = GenerateInsert(args);
                        com.ExecuteNonQuery();
                    }
                } catch (Exception e) {
                    // check if is an SQLite exception
                    // just to avoid having to do 2 of almost the same catch blocks
                    if (e is SQLiteException)
                    {
                        System.Windows.Forms.MessageBox.Show(string.Format("Failed to write to database\nSQLite Exception: {0}", e.Message));
                    }
                    else
                    {
                        System.Windows.Forms.MessageBox.Show(string.Format("Failed to write to database\nException: {0}", e.Message));
                    }
                }

                // close form
                waitForm.Close();

                // pass remaining queue back to buffer (if there was an error)
                while (queue.Count > 0)
                {
                    buffer.Enqueue(queue.Dequeue());
                }
            }
        }
Beispiel #2
0
 // Watcher event
 public void EH_MonitorEvent(WatcherEventArgs args)
 {
     if (DataGridViewEvents.InvokeRequired)
     {
         // Need to invoke so is on main thread
         WatcherEventHandler handler = EH_MonitorEvent;
         Invoke(handler, args);
     }
     else
     {
         // add to data grid
         DataGridViewEvents.Rows.Add(args.ItemName, args.ItemType, args.Path, args.EventName, args.EventDateTime);
         // only push to buffer if db is active
         if (eventDB.Active)
         {
             // push to buffer
             eventBuffer.Enqueue(args);
         }
     }
 }