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;
        }

    }
}



呵呵,关键自己程序里要用到 所以开发了这个小功能 很多地方很粗糙,俺菜鸟,高手们就别贬我了。 使用的时候把2个dll放到你的程序目录,在资源管理器引用LOADing.dll 就可以了,DevComponents.DotNetBar2.dll为确定按钮控件的引用 列子: private void dl_Click(object sender, EventArgs e) { LOADing.FORMshow load = new LOADing.FORMshow(); load.showto(this, delegate { hand(new object[] { load, "正在处理数据" }); },false); } private void hand(object fr) { int i = 0; while (i < 100) { i++; ((LOADing.FORMshow)((object[])(fr))[0]).send((string)((object[])(fr))[0]+i.ToString()); System.Threading.Thread.Sleep(100); } } 主要用于处理数据的时候,提示用户处理过程,防止界面假死,数据处理完毕后会自动关闭窗体。 注:this为所要调用等待窗体的主窗体对象,中间为数据传递的委托,显示数据处理的过程.load.showto(this, delegate { hand(new object[] { load, "正在处理数据" }); });中new object[] 第一个参数一定要为固定的参数:创建LOADing.FORMshow的实例,后面再就可跟任意数据,都可在方法的过程中调用显示,最后的bool参数:false方法函数执行完毕后自动关闭窗体显示;true为方法执行完毕后出现确定按钮并阻塞主线程UI,点击确定后关闭提示窗体并取消阻塞线程,这么简单,用相信大家都会用了。 界面没有进行美化,感觉这样的就可以了,随后会升级为可自定义界面! 有问题加我QQ76230454
与前版功能基本相似,修正了部分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.
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值