zoukankan      html  css  js  c++  java
  • WinForm 进度条

    目标:MyForm上有个Button,按下后会执行Task,Task会返回中间结果,然后在Label上实时显示中间结果。

    using System;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    public partial class MyForm : Form
    {
    
        //1)Label的更新
        private readonly object statusLabel = new object();
        private void UpdateLabel(string txt)
        {
            lock (statusLabel)
            {
                this.Label1.Text = txt;
            }
        }
    
        //2)Task定义
        Random rand = new Random();
        public void SearchFilesInTarget(IProgress<string> onSearchingFile) 
        {
            while (true)
            {
                string result = "file_name" + rand.Next();
                onSearchingFile.Report(result);
            }
        }
    
        //3)按钮按下
        private readonly CancellationTokenSource cancelToken = new CancellationTokenSource();
        private void SearchStart_Click(object sender, EventArgs e)
        {
            var onSearchingFile = new Progress<string>(txt => this.BeginInvoke(updateLabel, "Searching..." + txt));
            Task myTask = Task.Factory.StartNew(() => SearchFilesInTarget(onSearchingFile), cancelToken.Token);
        }
    
    
        //4)构造BeginInvoke的参数
        public delegate void DelegateUpdateLabel(string file);
        private DelegateUpdateLabel updateLabel;
    
        public MyForm()
        {
            updateLabel = new DelegateUpdateLabel(UpdateLabel);
        }
    }
    

      

  • 相关阅读:
    C/C++ 获取线程入口地址模块等
    C/C++ IAT HOOK MessageBoxW
    C/C++ HOOK 全局 API
    c/c++创建静态库
    如何画Windows窗体
    WindowsPE TLS表
    WindowsPE 延迟加载导入表
    WindowsPE 资源表
    WindowsPE 重定位表
    WindowsPE 导出表
  • 原文地址:https://www.cnblogs.com/wyvern0618/p/8926255.html
Copyright © 2011-2022 走看看