窗体主要代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public partial class frmWaitingNew : Form
   {
       public frmWaitingNew()
       {
           InitializeComponent();
       }
 
       private delegate void SetTextHandler(string text);
       public void SetText(string text)
       {
           if (this.lbl_UserMsg.InvokeRequired)
           {
               this.Invoke(new SetTextHandler(SetText), text);
           }
           else
           {
               this.lbl_UserMsg.Text = text;
           }
       }
 
       public bool IsShown = false;
       DateTime StarTime;
       private void frmWaitingNew_Shown(object sender, EventArgs e)
       {
           StarTime = ConvertEx.ToDateTimeEx("2014-06-15");
           timer1.Start();
           IsShown = true;
       }
 
       int RunSeconds = 0;
       private void timer1_Tick(object sender, EventArgs e)
       {
           txt_Time.Text = StarTime.AddSeconds(++RunSeconds).ToString("mm:ss");
       }
 
   }

等待窗体调用类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/// <summary>
   /// Using Singleton Design Pattern
   /// </summary>
   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 frmWaitingNew waitForm;
 
       public void CreateForm()
       {
           try
           {
               if (waitThread != null)
               {
                   try
                   {
                       waitThread.Abort();
                       waitThread.Join();
                   }
                   catch (Exception)
                   {
                   }
               }
               while (waitThread != null && waitThread.ThreadState != ThreadState.Aborted)
                   Thread.Sleep(200);
 
               waitThread = new Thread(new ThreadStart(delegate()
               {
                   waitForm = new frmWaitingNew();
                   Application.Run(waitForm);
               }));
               waitThread.Start();
               ThreadWaite();
           }
           catch (Exception ex)
           {
               string sm = ex.Message;
           }
       }
 
       void ThreadWaite()
       {
           Thread.Sleep(200);
           if (!waitThread.IsAlive || waitForm == null || !waitForm.IsShown)
               Thread.Sleep(20);
 
       }
 
       public void CloseForm()
       {
           if (waitThread != null)
           {
               try
               {
                   //waitThread.Abort();
                   //就是在分页里面,不停的按下一页,下一页。Application.Run(waitForm);就会报错。使用下面代码
                   //waitForm.Close();
                   //waitForm.Dispose();
                   ThreadWaite();
                   waitForm.Invoke(new DClose(Close), null);
                   waitThread.Abort();
                   waitThread.Join();
                   waitThread.DisableComObjectEagerCleanup();
 
               }
               catch (Exception ex)
               {
                   Thread.Sleep(200);
                   ThreadWaite();
                   waitForm.Invoke(new DClose(Close), null);
                   waitThread.Abort();
                   waitThread.Join();
                   waitThread.DisableComObjectEagerCleanup();
               }
           }
       }
       public delegate void DClose();
 
       public void Close()
       {
           while ((int)waitForm.Handle < 0)
               Thread.Sleep(20);
           waitForm.Close();
       }
 
       public void SetFormCaption(string text)
       {
           if (waitForm != null)
           {
               try
               {
                   waitForm.SetText(text);
               }
               catch (Exception)
               {
               }
           }
       }
 
   }

Demo下载地址:http://pan.baidu.com/s/1pJ9HSRD

C#Winform之等待窗体的更多相关文章

  1. (二十三)c#Winform自定义控件-等待窗体

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  2. 【C#】分享带等待窗体的任务执行器一枚

    -------------201504161039更新------------- 更新内容: IWaitForm接口删除System.Windows.Forms.DialogResult Dialog ...

  3. c# winForm 等待窗体的实现

    最近在做一个项目,需要用到等待窗体,在DevExpress下面有SplashScreen控件可以使用,同时也有ProgressIndicator控件能用,但是如果没有用Dev开发的话,我们就需要自定义 ...

  4. 【C#】带等待窗体的BackgroundWorker

    ---------------201504170911更新--------------- 更新内容:删除bgwUI新增的Start方法,改为通过new修饰符+可选参数的方式同时覆盖基类(Backgro ...

  5. C#中的“等待窗体”对话框

    这篇文章向您展示了如何在c#.net Windows窗体应用程序中创建一个等待窗体对话框.创建一个新表单,然后输入您的表单名称为frmWaitForm.接下来,将Label,Progress Bar控 ...

  6. winform打开子窗体后,在子窗体中刷新父窗体,或者关闭子窗体刷新父窗体

    winform打开子窗体后,在子窗体中刷新父窗体,或者关闭子窗体刷新父窗体,搜集了几个方法,列举如下: 一 . 所有权法 父窗体,名称为“fuForm”,在父窗体中有个公共刷新方法,也就是窗体数据初始 ...

  7. Devexpress 等待窗体

    加载窗体以及等待窗体 程序加载时,需要等待加载完成后在显示 窗体显示顺序 1. 给用户看的等待窗体 2. 加载完成后的主窗体 代码如下: 1. 等待窗体代码 #region using using S ...

  8. C#关于等待窗体(转)

    c#.net 中如果想在主窗口A里点击打开新窗口B(因为要数据库操作,Bload需一小段时间)之前弹出带有滚动条等待子窗口C来提示用户没有死机,应该怎么做?我用多线程打开了c窗口,但是问题:1.C窗口 ...

  9. WinForm中一个窗体调用另一个窗体

    [转] WinForm中一个窗体调用另一个窗体的控件和事件的方法(附带源码) //如果想打开一个 Form2 的窗体类,只需要: Form2 form = new Form2(); //有没有参数得看 ...

随机推荐

  1. Codeforces548E:Mike and Foam

    Mike is a bartender at Rico's bar. At Rico's, they put beer glasses in a special shelf. There are n  ...

  2. bzoj 2458: [BeiJing2011]最小三角形 题解

    [前言]话说好久没有写题解了.到暑假了反而忙.o(╯□╰)o [原题] 2458: [BeiJing2011]最小三角形 Time Limit: 10 Sec  Memory Limit: 128 M ...

  3. 出租车Jt/T 905协议与部标1078协议融合的网约车视频监控平台

    出租车jt/t 905协议,是jt/t 808协议的一个变种,设计者将部标808协议拿过来,并不是单纯的增加网约车相关的指令集,而且对原有的指令如定位0×0200指令也进行了修改,经过一通剧烈的修改, ...

  4. [译]GLUT教程(目录)

    http://www.lighthouse3d.com/tutorials/glut-tutorial/ GLUT是OpenGL Utility Toolkit的意思.作者Mark J. Kilgar ...

  5. IOS设置图片背景

    在UIViewController里面这样设置: self.view.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageN ...

  6. Truck History - poj 1789 (Prim 算法)

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20884   Accepted: 8075 Description Ad ...

  7. html自定义标签属性

    <a href="#" _asd="xxxx" onclick="test(event)">test</a> < ...

  8. 【入门】创建express项目

    1.创建项目(图解) 2.访问http://localhost:3000/就看到熟悉的页面了 3.查看项目目录     参考文档:http://jingyan.baidu.com/article/92 ...

  9. Java泛型的应用

    一.泛型类 package generics; /** * 泛型类,格式:public class 类名<泛型类型1, ...> * @author zhongfg * @date 201 ...

  10. 仿照ArrayList自己生成的MyList对象

    现在需要自己生成一个list集合,基本雷同ArrayList,不使用API的List接口. 实现如下: MyList的代码: public class MyList<T> { privat ...