private void BackgroundThread()
 {
     try
     {
         while (true)
         {
             try
             {
                 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://example.com/samplepath?query=query");
                 request.Method = "GET";
                 using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                 {
                     using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8))
                     {
                         // get what I need here (just the entire contents as a string for this example)
                         string result = reader.ReadToEnd();
                         // put it into the results
                         BackgroundResult backgroundResult = new BackgroundResult {
                             Response = result
                         };
                         System.Threading.Interlocked.Exchange(ref _BackgroundResult, backgroundResult);
                     }
                 }
             }
             catch (Exception ex)
             {
                 // the request failed--cath here and notify us somehow, but keep looping
                 System.Diagnostics.Trace.WriteLine("Exception doing background web request:" + ex.ToString());
             }
             // wait for five minutes before we query again.  Note that this is five minutes between the END of one request and the start of another--if you want 5 minutes between the START of each request, this will need to change a little.
             System.Threading.Thread.Sleep(5 * 60 * 1000);
         }
     }
     catch (Exception ex)
     {
         // we need to get notified of this error here somehow by logging it or something...
         System.Diagnostics.Trace.WriteLine("Error in BackgroundQuery.BackgroundThread:" + ex.ToString());
     }
 }
        private void buttonStart_Click(object sender, RoutedEventArgs e)
        {
            progressBar.Value          = 0;
            buttonStart.IsEnabled      = false;
            groupBoxSettings.IsEnabled = false;

            mBackgroundResult           = new BackgroundResult();
            mBackgroundResult.corrupted = 0;
            mBackgroundResult.ok        = 0;

            mBw = new BackgroundWorker();
            mBw.WorkerReportsProgress = true;
            mBw.DoWork             += new DoWorkEventHandler(Background_DoWork);
            mBw.ProgressChanged    += new ProgressChangedEventHandler(Background_ProgressChanged);
            mBw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Background_RunWorkerCompleted);
            var args = new BackgroundParams();

            args.path         = textBoxFolder.Text;
            args.parallelScan = radioButtonSsd.IsChecked == true;
            mBw.RunWorkerAsync(args);

            mStopwatch.Reset();
            mStopwatch.Start();
        }
Esempio n. 3
0
        private object DoWork(BackgroundRequest request, BackgroundWorker worker, DoWorkEventArgs e)
        {
            object result = null;

            if (worker.CancellationPending)
            {
                e.Cancel = true;
            }
            else
            {
                switch (request.Name)
                {
                case CommandName.Connect:
                    var computerName = txtComputerName.Text;
                    var port         = int.Parse(txtPort.Text);
                    var username     = txtUsername.Text;
                    var password     = txtPassword.Text;
                    if (rdbAuthBasic.Checked)
                    {
                        _machineManager = new MachineManager(computerName, port, username, password.ToSecureString(), AuthenticationMechanism.Basic);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
                        {
                            _machineManager = new MachineManager(computerName, port);
                        }
                        else
                        {
                            _machineManager = new MachineManager(computerName, port, username, password.ToSecureString(), AuthenticationMechanism.Default);
                        }
                    }
                    _machineManager.EnterSession();
                    result = new BackgroundResult
                    {
                        Name = CommandName.Connect
                    };
                    break;

                case CommandName.Disconnect:
                    _machineManager?.ExitSession();
                    result = new BackgroundResult
                    {
                        Name = CommandName.Disconnect
                    };
                    break;

                case CommandName.Upload:
                    _machineManager.CopyFileToSession(request.Request[0].ToString(), request.Request[1].ToString());
                    result = new BackgroundResult
                    {
                        Name = CommandName.Upload
                    };
                    break;

                case CommandName.Download:
                    _machineManager.CopyFileFromSession(request.Request[0].ToString(), request.Request[1].ToString());
                    result = new BackgroundResult
                    {
                        Name = CommandName.Download
                    };
                    break;

                case CommandName.Delete:
                    _machineManager.RemoveFileFromSession(request.Request[0].ToString());
                    result = new BackgroundResult
                    {
                        Name   = CommandName.Delete,
                        Result = request.Request[0]
                    };
                    break;

                case CommandName.Rename:
                    _machineManager.RenameFileFromSession(request.Request[0].ToString(), request.Request[1].ToString());
                    result = new BackgroundResult
                    {
                        Name   = CommandName.Rename,
                        Result = request.Request[0]
                    };
                    break;

                case CommandName.Extract:
                    _machineManager.ExtractFileFromSession(request.Request[0].ToString(), request.Request[1].ToString());
                    result = new BackgroundResult
                    {
                        Name   = CommandName.Extract,
                        Result = request.Request[0]
                    };
                    break;

                case CommandName.RefreshFiles:
                    result = new BackgroundResult
                    {
                        Name   = CommandName.RefreshFiles,
                        Result = _machineManager.LoadFilesCommand(request.Request[0].ToString())
                    };
                    break;

                case CommandName.RefreshDirectories:
                    result = new BackgroundResult
                    {
                        Name   = CommandName.RefreshDirectories,
                        Result = _machineManager.LoadDirectoriesCommand(request.Request[0].ToString())
                    };
                    break;

                case CommandName.RefreshDrives:
                    result = new BackgroundResult
                    {
                        Name   = CommandName.RefreshDrives,
                        Result = _machineManager.LoadDrives()
                    };
                    break;

                case CommandName.ProgressChanged:
                    result = new BackgroundResult
                    {
                        Name   = CommandName.ProgressChanged,
                        Result = request.Request[0]
                    };
                    break;
                }
            }

            return(result);
        }
Esempio n. 4
0
        public void Learn(BackgroundWorker backgroundWorker, DoWorkEventArgs e, double[] sequence, bool? showIteration)
        {
            ColumnVector[] learningMatrix = createLearningMatrix(sequence);
            double[] etalons = createEtalons(sequence);

            backgroundResult = new BackgroundResult();
            backgroundResult.IsComplete = false;

            double totalError;
            int iterations = 0;

            do
            {
                // learn
                for (int i = 0; i < learningMatrix.Length; i++)
                {

                    ColumnVector X = ConcatVertically(learningMatrix[i], contextNeurons);

                    ColumnVector Xn = Normalize(X);
                    double norm = X.FrobeniusNorm();

                    ColumnVector Y1 = weightMatrix1 * Xn;
                    double Y2 = weightMatrix2 * Y1;

                    double gamma2 = (Y2 * norm) - etalons[i];
                    RowVector gamma1 = gamma2 * weightMatrix2;

                    RowVector a = learningCoefficient * gamma1;
                    RectangularMatrix b = Xn * a;

                    weightMatrix1 = weightMatrix1 - b.Transpose();
                    weightMatrix2 = weightMatrix2 - ((gamma2 * learningCoefficient) * Y1).Transpose();

                    contextNeurons = Y1;
                }

                totalError = 0;

                // calculate total error
                for (int i = 0; i < learningMatrix.Length; i++)
                {

                    ColumnVector X = ConcatVertically(learningMatrix[i], contextNeurons);

                    ColumnVector Xn = Normalize(X);
                    double norm = X.FrobeniusNorm();

                    ColumnVector Y1 = weightMatrix1 * Xn;
                    double Y2 = weightMatrix2 * Y1;

                    double gamma2 = Y2 * norm - etalons[i];

                    totalError += Math.Pow(gamma2, 2);
                }

                backgroundResult.IterationNumber = iterations;
                backgroundResult.Error = totalError;
                backgroundResult.IsComplete = false;

                if (showIteration.Equals(true))
                {
                    backgroundWorker.ReportProgress(0, backgroundResult);

                    Thread.Sleep(200);

                    if (backgroundWorker.CancellationPending)
                    {
                        e.Cancel = true;
                        break;
                    }
                }

                iterations++;
            }
            while (totalError >= maxError && iterations <= maxIterations);

            backgroundResult.IterationNumber = iterations;
            backgroundResult.Error = totalError;
            backgroundResult.IsComplete = true;

            backgroundWorker.ReportProgress(0, backgroundResult);
        }