Beispiel #1
0
        public static TplWorker DoWork(SetProgressValueDelegate progress, CancellationToken token)
        {
            Console.WriteLine("DoWork先頭 ManagedThreadId={0}",
                              Thread.CurrentThread.ManagedThreadId);

            ParallelOptions po = new ParallelOptions();

            po.CancellationToken      = token;
            po.MaxDegreeOfParallelism = System.Environment.ProcessorCount;

            TplWorker instance = new TplWorker();

            try
            {
                Parallel.For(0, CountOfNumbers, po, index =>
                {
                    po.CancellationToken.ThrowIfCancellationRequested();
                    instance.ProcessANumber(progress, index);
                });
            }
            catch (OperationCanceledException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                throw e.InnerException;
            }

            Console.WriteLine("DoWork末尾 ManagedThreadId={0}",
                              Thread.CurrentThread.ManagedThreadId);

            return(instance);
        }
Beispiel #2
0
        private void ShowResult(TplWorker worker)
        {
            string msg = FormatResult(this._start, this._stop,
                                      worker, this._clickThreadId, this._callbackThreadId);

            SetTextbox1Text(msg);
        }
Beispiel #3
0
        private static string FormatResult(DateTime start, DateTime stop,
                                           TplWorker worker, int threadIdStart, int threadIdCallBack)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine(string.Format("開始:{0:HH:mm:ss.fff} @スレッド]{1}",
                                        start, threadIdStart));
            for (int i = 0; i < worker.OutputData.Length; i++)
            {
                sb.AppendFormat("{0}: '{1}'\r\n", i, worker.OutputData[i]);
            }
            sb.AppendLine(string.Format("終了:{0:HH:mm:ss.fff} @スレッド]{1}",
                                        stop, threadIdCallBack));

            return(sb.ToString());
        }
Beispiel #4
0
        private void button1_Click(object sender, EventArgs e)
        {
            progressBar1.Value = 0;
            button1.Enabled    = false;
            button2.Enabled    = true;
            textBox1.Text      = "実行中";
            Application.DoEvents();

            this._start         = DateTime.Now;
            this._clickThreadId = Thread.CurrentThread.ManagedThreadId;

            _cts = new CancellationTokenSource();
            CancellationToken token = _cts.Token;

            var taskScheduler = TaskScheduler.FromCurrentSynchronizationContext();

            Task task = Task.Factory.StartNew(
                () => TplWorker.DoWork(new SetProgressValueDelegate(SetProgressValue), token)
                )
                        .ContinueWith(
                t => Completed(t)
                );
        }