LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C#执行耗时进程时显示动态等待效果实现

admin
2024年11月27日 18:47 本文热度 202

在C#中,当执行耗时操作时,显示一个动态等待效果(如一个旋转的图标或进度条)可以提升用户体验。以下是一个简单的实现示例,使用 TaskCancellationToken, 和 ProgressBar 控件(或者你可以自定义任何动态效果控件)。

示例步骤:

  1. 创建Windows Forms应用程序

    • 使用Visual Studio创建一个新的Windows Forms应用程序。
  2. 添加控件

    • 在主窗体上添加一个 ProgressBar 控件(可以选择添加一个 Label 控件来显示文本信息)。
    • 你也可以自定义一个动态效果的控件,比如一个旋转的图标。
  3. 编写代码

    • 使用异步编程来执行耗时操作,并在UI线程中更新动态等待效果。

示例代码:

以下是一个完整的示例代码,演示了如何实现动态等待效果。

using System;

using System.Threading;

using System.Threading.Tasks;

using System.Windows.Forms;


namespace DynamicWaitingExample

{

    public partial class MainForm : Form

    {

        public MainForm()

        {

            InitializeComponent();

        }


        private async void btnStartTask_Click(object sender, EventArgs e)

        {

            // 初始化 ProgressBar

            progressBar1.Style = ProgressBarStyle.Marquee;

            progressBar1.MarqueeAnimationSpeed = 30; // 调整以更改动画速度


            // 禁用按钮以防止重复点击

            btnStartTask.Enabled = false;


            // 执行耗时任务并等待完成

            await RunLongRunningTaskAsync();


            // 还原UI状态

            progressBar1.Style = ProgressBarStyle.Blocks;

            progressBar1.MarqueeAnimationSpeed = 0;

            btnStartTask.Enabled = true;


            MessageBox.Show("任务完成!");

        }


        private async Task RunLongRunningTaskAsync()

        {

            // 使用 CancellationTokenSource 以便可以取消任务(可选)

            var cts = new CancellationTokenSource();


            try

            {

                // 模拟耗时任务

                await Task.Run(() =>

                {

                    for (int i = 0; i < 100; i++)

                    {

                        // 模拟工作的一部分

                        Thread.Sleep(50); // 模拟耗时操作


                        // 报告进度(可选,用于更新UI进度条)

                        // 这里进度条仅仅是示意,因为使用的是Marquee风格

                        this.Invoke(new Action(() =>

                        {

                            // 可以根据需要更新其他UI控件

                            // progressBar1.Value = i; // 仅对Blocks风格有效

                        }));


                        // 检查是否请求取消

                        if (cts.Token.IsCancellationRequested)

                        {

                            cts.Token.ThrowIfCancellationRequested();

                        }

                    }

                }, cts.Token);

            }

            catch (OperationCanceledException)

            {

                // 任务取消处理(可选)

                MessageBox.Show("任务已取消。");

            }

            finally

            {

                // 清理资源

                cts.Dispose();

            }

        }


        // 初始化窗体控件

        private void InitializeComponent()

        {

            this.progressBar1 = new System.Windows.Forms.ProgressBar();

            this.btnStartTask = new System.Windows.Forms.Button();

            this.SuspendLayout();

            // 

            // progressBar1

            // 

            this.progressBar1.Location = new System.Drawing.Point(12, 12);

            this.progressBar1.Name = "progressBar1";

            this.progressBar1.Size = new System.Drawing.Size(358, 23);

            this.progressBar1.Style = System.Windows.Forms.ProgressBarStyle.Marquee;

            this.progressBar1.TabIndex = 0;

            // 

            // btnStartTask

            // 

            this.btnStartTask.Location = new System.Drawing.Point(158, 50);

            this.btnStartTask.Name = "btnStartTask";

            this.btnStartTask.Size = new System.Drawing.Size(75, 23);

            this.btnStartTask.TabIndex = 1;

            this.btnStartTask.Text = "开始任务";

            this.btnStartTask.UseVisualStyleBackColor = true;

            this.btnStartTask.Click += new System.EventHandler(this.btnStartTask_Click);

            // 

            // MainForm

            // 

            this.ClientSize = new System.Drawing.Size(382, 90);

            this.Controls.Add(this.btnStartTask);

            this.Controls.Add(this.progressBar1);

            this.Name = "MainForm";

            this.Text = "动态等待示例";

            this.ResumeLayout(false);

        }


        private System.Windows.Forms.ProgressBar progressBar1;

        private System.Windows.Forms.Button btnStartTask;

    }

}arp

关键点:

  1. 异步编程

    • 使用 async 和 await 关键字来避免阻塞UI线程。
    • 使用 Task.Run 将耗时操作放到后台线程执行。
  2. UI更新

    • 使用 this.Invoke 方法确保UI更新在UI线程中执行。
  3. 取消支持

    • 使用 CancellationTokenSource 以便在需要时可以取消任务。
  4. ProgressBar控件

    • 使用 ProgressBarStyle.Marquee 实现动态效果。

通过以上步骤,你可以在C# Windows Forms应用程序中实现一个简单的动态等待效果,使用户在等待耗时操作时有良好的体验。


该文章在 2024/11/27 18:47:17 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved