Threading and UI

 Download source files - 27 Kb



Calculations.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace AsynchTask
{
    public class frmCalculations : System.Windows.Forms.Form
    {
        Task Task1;
        
private System.Windows.Forms.Label lblRunLoops;
        
private System.Windows.Forms.Button btnRunLoops;
        
private System.Windows.Forms.TextBox txtValue;
        
private System.Windows.Forms.Label label1;
        private System.ComponentModel.Container components = null;

        
public frmCalculations()
        {
            InitializeComponent();

            Task1 
= new Task();
            Task1.LoopComplete 
+= new Task.LoopCompleteHandler(this.LoopDoneHandler);
        }

        //
..Windows Form Designer generated code.. //

        [STAThread]
        
static void Main() 
        {
            Application.Run(
new frmCalculations());
        }

        
private void btnRunLoops_Click(object sender, System.EventArgs e)
        {
            
try
            {
                Task1.varLoopValue 
= int.Parse(txtValue.Text);
            }
            
catch(FormatException)
            {
                MessageBox.Show(
"Enter that must be numeric.");
            }
            btnRunLoops.Enabled 
= false;
            lblRunLoops.Text 
= "Looping";

            Task1.StartTask();
        }

        
protected void LoopDoneHandler(double Calculations, int Count)
        {
            lblRunLoops.Text 
= Count.ToString();
            btnRunLoops.Enabled 
= true;
        }
 
    }
}


Task.cs
using System;
using System.Threading;

namespace AsynchTask
{
    
// The Task object encapsulates functionality known to be somewhat time consuming.
    public class Task
    {
        
public int varLoopValue;
        
public double varTotalCalculations = 0;

        
public Thread LoopThread;

        
public Task(){}


        
// Delegates for events that your component will use to communicate values to the form
        public delegate void LoopCompleteHandler(double TotalCalculations, int Counter);

        
// events that this (class)component will use to communicate with the app
        public event LoopCompleteHandler LoopComplete;

        
public void RunALoop()
        {
            
int varX;
            
double varTotalAsOfNow = 0;

            
// simulate a lengthy operation
            for(varX=1; varX <= varLoopValue; varX++)
            {
                
for (int varY=1; varY <= 500; varY++)
                {
                    varTotalCalculations 
+= 1;
                    varTotalAsOfNow 
= varTotalCalculations;
                }
            }

            
// determine if the object on which this delegate was invoked is a UI thread
            
// if the object is a control, then the object is a UI thread
            if (LoopComplete.Target is System.Windows.Forms.Control)
            {
                
// make sure execution is done on the UI thread
                System.Windows.Forms.Control t = LoopComplete.Target as System.Windows.Forms.Control;
                
if (t != null)
                {
                    t.BeginInvoke(LoopComplete, 
new Object[]{varTotalAsOfNow, varLoopValue});
                }
            }
            
else
            {
                
// object from the invocation list isn't a UI thread. 
                LoopComplete(varTotalAsOfNow, varLoopValue);
            }
        }

        
public void StartTask()
        {
            LoopThread 
= new Thread(new ThreadStart(this.RunALoop));
            LoopThread.Start();
        }

    }
}
原文地址:https://www.cnblogs.com/publicbill/p/401153.html