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
        // Form constructor
        public FormMonitor()
        {
            // Build GUI
            InitializeComponent();

            // setup event between Watcher and Form
            Watcher.WatcherEvent = new WatcherEventHandler(EH_MonitorEvent);

            // Create buffer
            eventBuffer = new WatcherEventBuffer();

            // create DB obj (not connected to anything)
            eventDB = new EventDatabase();
        }