C# winform 多线程中创建等待窗体

1.首先创建一个WinForm窗体,可讲窗体的FormBorderStyle属性设置为"None",将窗体的标题栏去掉。窗体中可放一个PictureBox控件和两个Label控件。其中PictureBox控件存放加载等待的图片。一个Label控件可放置Text文本。

WinForm代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Test
{
    public partial class WaitForm : Form
    {
        public WaitForm()
        {            
            InitializeComponent();
            SetText("");
        }

        private delegate void SetTextHandler(string text);
        public void SetText(string text)
        {
            if (this.label2.InvokeRequired)
            {
                this.Invoke(new SetTextHandler(SetText), text);
            }
            else
            {
                this.label2.Text = text;
            }
        }
    }
}

2.新建一个类WaitFormService,代码如下“
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Test
{

    public class WaitFormService
    {
        public static void CreateWaitForm()
        {
            WaitFormService.Instance.CreateForm();
        }

        public static void CloseWaitForm()
        {
            WaitFormService.Instance.CloseForm();
        }

        public static void SetWaitFormCaption(string text)
        {
            WaitFormService.Instance.SetFormCaption(text);
        }

        private static WaitFormService _instance;
        private static readonly Object syncLock = new Object();

        public static WaitFormService Instance
        {
            get 
            {
                if (WaitFormService._instance == null)
                {
                    lock (syncLock)
                    {
                        if (WaitFormService._instance == null)
                        {
                            WaitFormService._instance = new WaitFormService();
                        }
                    }
                }
                return WaitFormService._instance;
            }
        }

        private WaitFormService()
        {
        }

        private Thread waitThread;
        private WaitForm waitForm;

        public void CreateForm()
        {
            if (waitThread != null)
            {
                try
                {
                    waitThread.Abort();
                }
                catch (Exception)
                {
                }
            }

            waitThread = new Thread(new ThreadStart(delegate()
            {
                waitForm = new WaitForm();
                Application.Run(waitForm);
            }));
            waitThread.Start();
        }

        public void CloseForm()
        {
            if (waitThread != null)
            {
                try
                {
                    waitThread.Abort();
                }
                catch (Exception)
                {
                }
            }
        }

        public void SetFormCaption(string text)
        {
            if (waitForm != null)
            {
                try
                {
                    waitForm.SetText(text);
                }
                catch (Exception)
                {
                }
            }
        }
    }
}
3.调用如下:
  try
    {
        WaitFormService.CreateWaitForm();
        Assembly asmb = Assembly.GetExecutingAssembly();
        WaitFormService.CloseWaitForm();
    }
    catch (Exception ex)
    {
        WaitFormService.CloseWaitForm();
    }
 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 C# WinForm 多线程的例子,它使用了 BackgroundWorker 组件来实现多线程处理任务: 1. 在 WinForm 窗体添加一个按钮和一个 Label 控件。 2. 双击按钮控件,打开 Click 事件处理程序。 3. 在事件处理程序添加以下代码: ```csharp private void button1_Click(object sender, EventArgs e) { // 启动后台任务 backgroundWorker1.RunWorkerAsync(); } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { // 模拟一个长时间运行的任务 for (int i = 0; i < 100; i++) { Thread.Sleep(100); backgroundWorker1.ReportProgress(i); } } private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { // 更新 Label 控件的显示内容 label1.Text = $"已完成 {e.ProgressPercentage}%"; } private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { // 显示任务完成提示 MessageBox.Show("任务已完成!"); } ``` 4. 在窗体的构造函数,添加以下代码: ```csharp public Form1() { InitializeComponent(); // 启用支持多线程的控件样式 Control.CheckForIllegalCrossThreadCalls = false; // 配置 BackgroundWorker 组件 backgroundWorker1.WorkerReportsProgress = true; backgroundWorker1.WorkerSupportsCancellation = false; } ``` 以上代码的作用如下: - 当用户单击按钮时,启动后台任务; - 后台任务模拟一个长时间运行的任务,每隔一段时间更新一次进度; - 当进度更新时,更新 Label 控件的显示内容; - 当任务完成时,弹出一个提示框。 需要注意的是,在多线程应用程序,UI 线程和后台线程是分别运行的,不能直接访问 UI 控件。为了避免出现访问冲突,可以启用支持多线程的控件样式,并使用 Invoke 或 CheckForIllegalCrossThreadCalls 属性来解决这个问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值