Winform C# 简单实现子窗口显示进度条
主窗口代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProgressbarTest
{
public partial class Form1 : MaterialSkin.Controls.MaterialForm
{
public Form1()
{
InitializeComponent();
Bkg_ClaculateStatus.WorkerReportsProgress = true;
Bkg_ClaculateStatus.WorkerSupportsCancellation = true;
Bkg_ClaculateStatus.DoWork += DoWork_Handler;
Bkg_ClaculateStatus.ProgressChanged += ProcessChanged_Handler;
Bkg_ClaculateStatus.RunWorkerCompleted += RunWorkerCompleted_Handler;
}
ProgressBar progressBar= new ProgressBar();
private void materialFlatButton1_Click(object sender, EventArgs e)
{
if (!Bkg_ClaculateStatus.IsBusy)
{
Bkg_ClaculateStatus.RunWorkerAsync();
progressBar.StartPosition = FormStartPosition.CenterParent;
progressBar.ShowDialog();
}
}
/// <summary>
/// Use less variables to implement Fibonacci
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
static int Fn2(int n)
{
if (n <= 0)
{
throw new ArgumentOutOfRangeException();
}
int a = 1;
int b = 1;
for (int i = 3; i <= n; i++)
{
b = checked(a + b); // when n>46 memory will overflow
a = b - a;
}
return b;
}
private void DoWork_Handler(object sender, DoWorkEventArgs args)
{
BackgroundWorker worker= sender as BackgroundWorker;
for (int i = 1; i < 10; i++)
{
if (worker.CancellationPending)
{
args.Cancel = true;
break;
}
else
{
worker.ReportProgress(i*10);
Thread.Sleep(500);
}
}
}
private void ProcessChanged_Handler(object sender, ProgressChangedEventArgs e)
{
progressBar.SetValue(e.ProgressPercentage);
}
private void RunWorkerCompleted_Handler(object sender, RunWorkerCompletedEventArgs e)
{
progressBar.SetValue(0);
this.progressBar.Close();
}
}
}
进度条窗口代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProgressbarTest
{
public partial class ProgressBar : MaterialSkin.Controls.MaterialForm
{
public ProgressBar()
{
InitializeComponent();
}
public void SetValue(int value)
{
this.materialProgressBar1.Value = value;
}
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 上周热点回顾(1.20-1.26)
· 【译】.NET 升级助手现在支持升级到集中式包管理