예제 #1
0
 private void StartInOneThread()
 {
     corut = new Corutine(this, dumbCorut(LOWER, HIGHER));
     //corut.binders.Add(new CorutineProgressbarBinder(progressBar1));
     //corut.binders.Add(new CorutineLabelBinder(label1));
     /*corut.BindLabel(label1);
     corut.BindProgressBar(progressBar1);*/
     SimpleProgressForm form = new SimpleProgressForm(corut) { showCompletedDialog = false };
     corut.OnCompleted(delegate(object result)
     {
         List<int> results = result as List<int>;
         MessageBox.Show(results.Count.ToString());
     });
     corut.OnCancelled(delegate
     {
         MessageBox.Show("process cancelled");
     });
     form.Start();
 }
예제 #2
0
 private void StartInMultiThreads(int num)
 {
     /* our task shoud be available to be split into subtasks
      * for this example we simply split our range to some subranges
      * */
     IEnumerable<CorutineReport>[] cors = new IEnumerable<CorutineReport>[num];
     int now = LOWER;
     int delta = (HIGHER - LOWER)/num;
     for (int i = 0; i < num; i++)
     {
         if (i < num - 1)
         {
             cors[i] = dumbCorut(now, now + delta);
             now += delta;
         }
         else
         {
             cors[i] = dumbCorut(now, HIGHER);
         }
     }
     //create multithreaded corutine
     corut = new CorutineMultithreaded(this, cors);
     //oh yes, everything else is same, except the result!
     SimpleProgressForm form = new SimpleProgressForm(corut) { showCompletedDialog = false };
     corut.OnCompleted(delegate(object result)
     {
         //here we get a list of results (from each thread)
         List<CorutineReportResult> results = result as List<CorutineReportResult>;
         int sum = (from res in results
             select (res.result as List<int>).Count).Sum();
         MessageBox.Show(sum.ToString());
     });
     corut.OnCancelled(delegate
     {
         MessageBox.Show("process cancelled");
     });
     form.Start();
 }