zoukankan      html  css  js  c++  java
  • 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();
            }

        }
    }
  • 相关阅读:
    心得体悟帖---200130(专业长才(敲门砖))(希望)
    心得体悟帖---200130(一举多的)(少了发自内心的从容)
    范仁义css3课程---19、流动布局
    范仁义css3课程---18、overflow
    日常英语---200130(inspire)
    日常英语---200130(Basketball fans around the world are mourning the death of American superstar Kobe Bryant.)
    视频中的ts文件是什么
    如何美化windows桌面
    心得体悟帖---200127(囚笼-它会推着我的,不必多想)(过好当下,享受当下)
    心得体悟帖---有哪些越早知道越好的人生经验?(转自知乎)
  • 原文地址:https://www.cnblogs.com/publicbill/p/401153.html
Copyright © 2011-2022 走看看