コード例 #1
0
ファイル: WorkerThread.cs プロジェクト: FrUi7c4k3/Research
 /// <summary>
 /// Raise the ReportWorkDone event
 /// </summary>
 protected virtual void OnReportWorkDone(WorkDoneCancelEventArgs e)
 {
     if (ReportWorkDone != null)
     {
         ReportWorkDone(this, e);
     }
 }
コード例 #2
0
ファイル: Client.cs プロジェクト: FrUi7c4k3/Research
        void wt_ReportWorkDone(object sender, WorkDoneCancelEventArgs e)
        {
            //marshal call to UI thread
            context.Post((obj) => lstView.Items.Add(e.PrimeFound.ToString()), null);

            //should worker thread be cancelled, has user clicked cancel button?
            e.Cancel = primeThreadCancel;

            //+++++++++++++++++++++++++++++++++++++++++++++++++++++
            //NOTE : This would also work to marshal call to UI thread
            //+++++++++++++++++++++++++++++++++++++++++++++++++++++

            //this.Invoke(new EventHandler(delegate
            //{
            //    lstItems.Items.Add(e.PrimeFound.ToString());
            //}));
        }
コード例 #3
0
ファイル: WorkerThread.cs プロジェクト: FrUi7c4k3/Research
        private void DoWork(object data)
        {
            int divisorsFound = 0;
            int startDivisor  = 1;

            long primeNumberLoopToFind = (long)data;

            for (int i = 0; i < primeNumberLoopToFind; i++)
            {
                //wait for trigger
                trigger.WaitOne();

                divisorsFound = 0;
                startDivisor  = 1;

                //check for prime numbers, and if we find one raise
                //the ReportWorkDone event
                while (startDivisor <= i)
                {
                    if (i % startDivisor == 0)
                    {
                        divisorsFound++;
                    }
                    startDivisor++;
                }

                if (divisorsFound == 2)
                {
                    WorkDoneCancelEventArgs e = new WorkDoneCancelEventArgs(i);
                    OnReportWorkDone(e);
                    cancel = e.Cancel;

                    //check whether thread should carry on,
                    //perhaps user cancelled it
                    if (cancel)
                    {
                        return;
                    }
                }
            }
        }