コード例 #1
0
        public MainWindow()
        {
            ipMonitor  = new IPMonitor(new WebsiteTester(new HashSet <string>()));
            vpnMonitor = new VPN_Stability_Monitor(ipMonitor);
            InitializeComponent();
            InitializeTextFields();
            Task.Factory.StartNew(() => ipMonitor.Run());
            Task.Factory.StartNew(() => vpnMonitor.Run());

            //Shutdown app when called for (can be replaced with system shutdown)
            Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    if (vpnMonitor.shutdown)
                    {
                        Application curApp = Application.Current;
                        //curApp.Dispatcher.Invoke(curApp.Shutdown);
                        Thread.Sleep(200);
                        Process.Start("shutdown", "/s /t 0");
                    }
                }
            }
                                  );
            this.Dispatcher.Invoke(() => ipMonitor.AddWebsite(WEBSITE1));
            this.Dispatcher.Invoke(() => ipMonitor.AddWebsite(WEBSITE2));
            this.Dispatcher.Invoke(() => ipMonitor.AddWebsite(WEBSITE3));
        }
コード例 #2
0
        public MonitorInformation Deserialize(string fileName, VPN_Stability_Monitor vpnMonitor)
        {
            // Declare the hashtable reference.
            MonitorInformation mi;

            // Open the file containing the data that you want to deserialize.
            FileStream fs = new FileStream(fileName, FileMode.Open);

            try
            {
                BinaryFormatter formatter = new BinaryFormatter();

                // Deserialize the hashtable from the file and
                // assign the reference to the local variable.
                mi = (MonitorInformation)formatter.Deserialize(fs);
            }
            catch (SerializationException e)
            {
                Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
                throw;
            }
            catch (IOException e)
            {
                Console.WriteLine("Failed to access file: " + e.Message);
                throw;
            }
            finally
            {
                fs.Close();
            }

            // To prove that the table deserialized correctly,
            // display the key/value pairs.
            try
            {
                if (vpnMonitor.IsValidIP(mi.HomeIP))
                {
                    Console.WriteLine(mi.HomeIP);
                }
                else
                {
                    Console.WriteLine("Uh-oh!");
                }
                if (vpnMonitor.IsValidIP(mi.VPNIP))
                {
                    Console.WriteLine(mi.VPNIP);
                }
                else
                {
                    Console.WriteLine("Uh-oh!");
                }
                return(mi);
            }
            catch
            {
                //Error!
                return(new MonitorInformation());
            }
        }