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();
            }

        }
    }
  • 相关阅读:
    FlowPortal:流程节点定义有误,合流节点"合流"没有对应的聚焦节点
    FlowPortal 6.00c 使用xFormDesigner复制粘贴中文总是乱码
    SharePoint 2019 里安装FlowPortal6.00c报错
    与用户xxx一起提供的密码不正确。请确认输入的密码正确并重试
    SharePoint 2010 安装错误:请重新启动计算机,然后运行安装程序以继续
    SharePoint 2013: Workflow Manager Backend 服务意外地终止
    用户管理
    Linux及工具网站
    基于c开发的全命令行音频播放器
    Linux 下清空或删除大文件内容的 5 种方法
  • 原文地址:https://www.cnblogs.com/publicbill/p/401153.html
Copyright © 2011-2022 走看看