Ejemplo n.º 1
0
        //public delegate void Populate();

        public MainWindow()
        {
            InitializeComponent();

            // Set up event handler to wait for a usb device to connect to
            deviceNotifier = DeviceNotifier.OpenDeviceNotifier();
            deviceNotifier.OnDeviceNotify += new EventHandler<DeviceNotifyEventArgs>(deviceNotifier_OnDeviceNotify);

            // try to connect to device (if device was connected previously). Show status in status bar
            try
            {
                traqpaq = new TraqpaqDevice();
                // update status bar
                statusBarItemTraqpaq.Content = "Device connected: " + traqpaq.reqSerialNumber();
            }
            catch (TraqPaqNotConnectedException)
            {
                // Device not found
                traqpaq = null;
                // update status bar
                statusBarItemTraqpaq.Content = "Device not found";
            }

            // Create the pages
            homePage = new HomePage(this);
            settingsPage = new SettingsPage(this);
            uploadPage = new UploadPage(this);

            // assign the pages to the respective tabs
            frameUpload.Navigate(uploadPage);
            frameSettings.Navigate(settingsPage);

            // Go to the welcome page
            frameHome.Navigate(homePage);

            // create the log book page
            logBookPage = new LogBookPage(this);
            frameLogBook.Navigate(logBookPage);
            if (traqpaq != null)
            {
                logBookPage.populateTracks();
            }

            // configure the background worker
            //bw.DoWork += bw_DoWork;
            //bw.WorkerReportsProgress = false;
            // attempt with backgroundworker if connected
            //if (traqpaq != null)
            //{
            //    logBookPage.populateTracks();
            //    //bw.RunWorkerAsync();
            //}


            //// create the log book page as a background task
            //Thread threadCreateRecordPage = new System.Threading.Thread(
            //    new ThreadStart(
            //        delegate()
            //        {
            //            System.Windows.Threading.DispatcherOperation dispatcherOp = frameLogBook.Dispatcher.BeginInvoke(
            //                System.Windows.Threading.DispatcherPriority.Normal,
            //                new Action(delegate()
            //                {
            //                    logBookPage.populateTracks();                                
            //                }));
            //        }));
            //threadCreateRecordPage.SetApartmentState(ApartmentState.STA);
            //threadCreateRecordPage.Start();

            //Action createRecordPage = delegate()
            //{
            //    logBookPage.populateTracks();
            //};
            //Task taskLogPage = Task.Factory.StartNew(createRecordPage);

        }
Ejemplo n.º 2
0
        //void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        //{
        //    MessageBox.Show("Complete");
        //}

        //void bw_DoWork(object sender, DoWorkEventArgs e)
        //{
        //    Populate handler = logBookPage.populateTracks;
        //    logBookPage.Dispatcher.BeginInvoke(handler);
        //}

        /// <summary>
        /// Called whenever a usb device is plugged in or unplugged
        /// </summary>
        void deviceNotifier_OnDeviceNotify(object sender, DeviceNotifyEventArgs e)
        {
            // Detected a device, try to see if it is the traqpaq
            if (e.Device.IdProduct == Constants.PID && e.Device.IdVendor == Constants.VID)
            {
                if (e.EventType == EventType.DeviceArrival)  // check for device arrival
                {
                    if (traqpaq == null)
                    {
                        // try to connect again
                        try
                        {
                            traqpaq = new TraqpaqDevice();
                            statusBarItemTraqpaq.Content = "Device connected: " + traqpaq.reqSerialNumber();
                            // populate tracks
                            //TODO fix populate tracks
                            logBookPage.populateTracks();                            
                        }
                        catch (TraqPaqNotConnectedException) { return; }    // Silently fail and exit method

                        //BackgroundWorker bw = new BackgroundWorker();
                        //bw.DoWork += bw_DoWork;
                        //bw.RunWorkerAsync(); 
                    }
                }
                else    // device removal
                {
                    traqpaq.disconnectDevice();
                    traqpaq = null;
                    // update status bar
                    statusBarItemTraqpaq.Content = "Traqpaq disconnected";
                }
            }
        }