C#多线程实现等待提示窗体

等等窗体代码,UI只有一个lbl 显示提示信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication16
{
    public partial class WaitForm : Form
    {
        public WaitForm()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;   
            SetText("正在执行,请耐心等待....");
        }

        private delegate void SetTextHandler(string text);
        public void SetText(string text)
        {
            if (this.label1.InvokeRequired)
            {
                this.Invoke(new SetTextHandler(SetText), text);
            }
            else
            {
                this.label1.Text = text;
            }
        }
    }
}
服务单元 ,主要进行对等待窗体的调用

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

namespace WindowsFormsApplication16
{
    
    /// <summary>  
    /// Using Singleton Design Pattern  
    /// </summary>  
    public class WaitFormService
    {

        /// <summary>  
        /// 单例模式  
        /// </summary>  
        public static WaitFormService Instance
        {
            get
            {
                if (WaitFormService._instance == null)
                {
                    lock (syncLock)
                    {
                        if (WaitFormService._instance == null)
                        {
                            WaitFormService._instance = new WaitFormService();
                        }
                    }
                }
                return WaitFormService._instance;
            }
        }

        /// <summary>  
        /// 为了单例模式防止new 实例化..  
        /// </summary>  
        private WaitFormService()
        {
        }


        private static WaitFormService _instance;
        private static readonly Object syncLock = new Object();
        private Thread waitThread;
        private static WaitForm waitForm;
        /// <summary>  
        /// 显示等待窗体  
        /// </summary>  
        public static void Show()
        {

            //try  
            //{              
            WaitFormService.Instance._CreateForm();
            //isclose = false;  
            //}  
            //catch (Exception ex)  
            //{   
            //}  
        }

        /// <summary>  
        /// 关闭等待窗体  
        /// </summary>  
        public static void Close()
        {

            //if (isclose == true)  
            //{  
            //    return;  
            //}  
            //try  
            //{  
            Thread.Sleep(100);
            WaitFormService.Instance._CloseForm();
            //isclose = true;  
            //}  
            //catch (Exception ex)  
            //{   
            //}  
        }

        /// <summary>  
        /// 设置等待窗体标题  
        /// </summary>  
        /// <param name="text"></param>  
        public static void SetText(string text)
        {
            //if (isclose == true)  
            //{  
            //    return;  
            //}  
            //try  
            //{  
            WaitFormService.Instance.SetWaiteText(text);
            //}  
            //catch (Exception ex)  
            //{   
            //}  
        }


        /// <summary>  
        /// 创建等待窗体  
        /// </summary>  
        public void _CreateForm()
        {           
            waitForm = null;
            waitThread = new Thread(new ThreadStart(this._ShowWaitForm));
            waitThread.Start();
            Thread.Sleep(100);
        }

        private void _ShowWaitForm()
        {
            try
            {                
                waitForm = new WaitForm();
                waitForm.ShowDialog();
                //Application.Run(waitForm);  
            }
            catch (ThreadAbortException e)
            {
                waitForm.Close();
                Thread.ResetAbort();
            }
        }

        /// <summary>  
        /// 关闭窗体  
        /// </summary>  
        private void _CloseForm()
        {

            //waitForm.Close();  
            //waitForm = null;  
            if (waitThread != null)
            {
                waitForm.Close(); //waitThread.Abort();
            }
        }


        /// <summary>  
        /// 设置窗体标题  
        /// </summary>  
        /// <param name="text"></param>  
        public void SetWaiteText(string text)
        {
            if (waitForm != null)
            {
                //try  
                //{  
                
                waitForm.Show();

                waitForm.SetText(text);
                //}  
                //catch (Exception ex)  
                //{   
                //}  
            }
        }

    }  
}

测试程序

uI添加1个button

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Threading;

namespace WindowsFormsApplication16
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        
        }

      
        int times;
        private void ExecWaitForm()
        {
             try
            {
                WaitFormService.Show();

                
                
                times++;
          
                for (int i = 0; i < 10000; i++)
                {
                    WaitFormService.SetText(times.ToString() + "正在执行 ,请耐心等待...." + i.ToString());
                    
                }

                WaitFormService.Close();
                if (times == 3)
                {                   
                    button1.Enabled = true; 
                    return;
                }
                ExecWaitForm();
               

            }
            catch (Exception ex)
            {
                WaitFormService.Close();
            }
        }

       
        private void button1_Click(object sender, EventArgs e)
        {
            times = 0;
            Thread th =new Thread(new ThreadStart(this.ExecWaitForm));
            th.Start();
            button1.Enabled = false;
        }

    }
}



  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
与前版功能基本相似,修正了部分bug,对界面进行了美化,目前这个美化相信应该够用了,因为时间问题没有增加可自定义界面功能,等有时间再提供吧,压缩文件使用“好压”做的,里面有一个例子,图片资源编辑工具,大家可以测试一下效果,里面也有详细说明,图片资源编辑器大家也可以用到自己的程序中,方便程序的图片统一管理和调用,菜鸟,达人们别笑话我了。 这里还是在说一下等待窗体的具体使用方法吧 首先将LOADing.dll,DevComponents.DotNetBar2.dll两个dll文件复制到你程序目录中,在程序项目中引用LOADing.dll,在要使用的地方 //先实例 LOADing.FORMshow FRload = new LOADing.FORMshow(); //再调用showto方法,其中的参数this为你调用等待窗体的主窗体对象,delegate { }为委托,IMGclass_AddFlie_r()为功能处理函数,其中所传递的参数第一的FRload必须为固定的创建等待窗体的实例对象,后面跟所需要传递的对象参数。 FRload.showto(this, delegate { IMGclass_AddFlie_r(new object[] { FRload, iclass, fileDialog1.FileNames, _at.SelectedNode.Text }); },true); //这个为数据处理部分 private void IMGclass_AddFlie_r(object[] d) { for (int i = 0; i <= ((string[])d[2]).Length - 1; i++) { ((IMGclass)d[1]).top[d[3].ToString()].Add("标" + ((IMGclass)d[1]).top[d[3].ToString()].Count, BinToCmd(((string[])d[2])[i])); f_new_hand(new object[] { ((IMGclass)d[1]).top[d[3].ToString()], "标" + (((IMGclass)d[1]).top[d[3].ToString()].Count - 1) }); ((LOADing.FORMshow)d[0]).send("加载图片文件:", Convert.ToInt32((Convert.ToSingle(i) / (Convert.ToSingle(((string[])d[2]).Length) / Convert.ToSingle(100))))); } BinToclass(((IMGclass)d[1]), _path[_at.SelectedNode.Parent.Text]); } 好了,使用起来很简单,看看上面的例子就会了,如需索要源码或者要提问的话,请联系QQ76230454.
实现多线程步骤编辑器的步骤如下: 1. 创建一个窗体应用程序或控制台应用程序。 2. 创建一个类来表示每个步骤,包括步骤名称、描述和执行的代码。 3. 创建一个数据结构来保存步骤列表。 4. 在主线程中创建一个 GUI,用于显示步骤列表和当前步骤的详细信息。 5. 创建一个后台工作线程来执行步骤。 6. 在后台线程中循环遍历步骤列表,依次执行每个步骤。 7. 在主线程中使用委托将更新 GUI 的任务委托给主线程。 8. 在后台线程中使用线程同步机制来确保数据的正确性和同步性。 以下是一个简单的 C# 代码示例,展示了如何实现一个多线程步骤编辑器: ```csharp using System; using System.Threading; using System.Windows.Forms; namespace MultiThreadedStepEditor { public partial class MainForm : Form { private StepList _steps; private Thread _workerThread; private bool _isRunning; public MainForm() { InitializeComponent(); _steps = new StepList(); _steps.Add(new Step("Step 1", "Description of step 1", () => { /* Code to execute for step 1 */ })); _steps.Add(new Step("Step 2", "Description of step 2", () => { /* Code to execute for step 2 */ })); _steps.Add(new Step("Step 3", "Description of step 3", () => { /* Code to execute for step 3 */ })); _steps.Add(new Step("Step 4", "Description of step 4", () => { /* Code to execute for step 4 */ })); } private void MainForm_Load(object sender, EventArgs e) { UpdateStepList(); } private void UpdateStepList() { lstSteps.Items.Clear(); foreach (var step in _steps) { lstSteps.Items.Add(step.Name); } } private void btnRun_Click(object sender, EventArgs e) { if (_isRunning) { _workerThread.Abort(); _isRunning = false; btnRun.Text = "Run"; return; } _isRunning = true; btnRun.Text = "Stop"; _workerThread = new Thread(() => { foreach (var step in _steps) { if (!_isRunning) { break; } // Execute the code for the current step step.Action(); // Update the UI with the current step this.Invoke(new Action(() => { lblCurrentStep.Text = step.Name; })); } // Reset the UI when the work is done this.Invoke(new Action(() => { lblCurrentStep.Text = "None"; btnRun.Text = "Run"; _isRunning = false; })); }); _workerThread.Start(); } } public class Step { public string Name { get; set; } public string Description { get; set; } public Action Action { get; set; } public Step(string name, string description, Action action) { Name = name; Description = description; Action = action; } } public class StepList : System.Collections.Generic.List<Step> { } } ``` 在该示例中,我们创建了一个 `Step` 类来表示每个步骤,然后创建了一个 `StepList` 类来保存步骤列表。在主窗口中,我们使用一个 `ListBox` 控件来显示步骤列表,并使用一个 `Label` 控件来显示当前步骤的详细信息。当用户单击“运行”按钮时,我们创建一个后台工作线程来执行步骤列表,并在界面上更新当前步骤。如果用户单击“停止”按钮,我们终止后台线程并重置界面状态。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值