C#多线程实现等待提示窗体
2014-12-24 14:54
92 查看
等等窗体代码,UI只有一个lbl 显示提示信息
测试程序
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; 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; } } }
相关文章推荐
- !!C# BackgroundWorker异步操作以及实现等待提示窗体功能---C#中此方法比多线程用的多
- 黄聪:V2010中C#实现友好的等待任务完成时,出现的多线程悬浮窗体
- C#实现类似MSN Messenger的弹出提示窗体
- 实现等待窗体的几种方式
- BusyTipOperator——显示提示窗体的实现(窗体打开与关闭过程的同步)
- java多线程实现主线程等待所有子线程执行完毕
- JS实现 页面提交防刷新等待提示
- 如何在web上实现等待提示。
- BusyTipOperator——显示提示窗体的实现(一)
- 仿QQ会员右下角提示框c#实现,提示窗体不获得焦点
- ajax提交等待服务器响应友好提示信息的实现
- 实现等待窗体的几种方式
- JS弹出模式提示窗体,实现页面其他地方不可编辑效果
- 用JAVA实现线程等待提示框
- BusyTipOperator——显示提示窗体的实现(二)
- 用JAVA实现线程等待提示框
- 用JAVA实现线程等待提示框
- 如何实现拨通电话,IVR提示,转人工按1,转传真按2,繁忙时自动转到队列,并播放音乐,并提示按1继续等待,按2退出。
- SWING 窗体关闭提示确认 的实现总结
- C#仿QQ皮肤-Windows消息提示框窗体MessageBoxForm的实现