C# winfrom利用多线程实现滚动条效果

本文介绍了一种使用C#实现多线程更新UI的简单方法。通过定义委托、使用线程帮助类及线程实例,实现了进度条的动态更新,适合初学者理解多线程原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     最近一直在搞多线程的东西,但是总是迷迷糊糊的。前几天有个哥们给我讲了一个方法,感觉不错。特此分享下!解决方案界面如下:

其中Form1.cs为主窗体,Program.cs为Main方法,ThreadHelpClass.cs为线程帮助类

Form1主窗体的界面如下:

点击button1运行效果如下:

具体代码如下:(按我写的顺序贴给大家)

首先是在Program类中声明两个委托,Program的源码如下:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Demo1
{
    //操作数据的委托
    public delegate void ThreadDelegate(Hashtable ht);
    //操作前台的委托
    public delegate void ThreadInvokeDelegate(Hashtable htInvoke);

    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
然后在线程帮助类(ThreadHelpClass.cs),具体实现代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Demo1
{
    //操作帮助类
    public class ThreadHelpClass
    {
        private ThreadDelegate threadDel;//创建委托对象
        
        //构造函数(参数为委托对象)
        public ThreadHelpClass(ThreadDelegate ht)
        { threadDel = ht; }
        
        //实现方法(主要就是一个 +1 操作)
        public void run()
        {
            Hashtable ht = new Hashtable();//声明Hashtable            
            ht["Total"] = 10;//设置滚动条的总长度
            for (int i = 0; i < 10; i++)
            {
                ht["single"] = i + 1;//对滚动条的每个单位进行+1操作                
                threadDel(ht);//最后添加到委托实例中               
                Thread.Sleep(1000);//挂起1s钟
            }
        }
    }
}
最后Form1.cs主窗体的主要代码如下:
using System;
using System.Collections;
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 Demo1
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// 加载主窗体界面
        /// </summary>
        public Form1()
        {InitializeComponent();}

        /// <summary>
        /// 启动操作按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //设置滚动条最小值为0
            this.progressBar1.Minimum = 0;
            //初始化线程帮助类实例,参数为委托对象
            ThreadHelpClass thc = new ThreadHelpClass(new ThreadDelegate(ThreadFun));
            //初始化线程,调用帮助类里的run方法
            Thread th = new Thread(new ThreadStart(thc.run));
            th.Start();//开启线程
        }

        /// <summary>
        /// 初始化线程帮助类调用的方法
        /// </summary>
        /// <param name="ht"></param>
        public void ThreadFun(Hashtable ht)
        {
            try
            {
                //调用Invoke方法获取数据
                this.Invoke(new ThreadInvokeDelegate(ThreadInvokeFun), new object[] { ht });
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 用控件赋值方法
        /// </summary>
        /// <param name="ht"></param>
        public void ThreadInvokeFun(Hashtable ht)
        {
            this.progressBar1.Maximum = Convert.ToInt32(ht["Total"]);
            this.progressBar1.Value = Convert.ToInt32(ht["single"]);
        }
    }
}


好了,利用上面的代码就可以实现功能了!

这个主要是帮助初学者或者和我一样不怎么懂多线程的人的,希望你看了后有所收获!






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值