窗体主要代码:

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. SQLserver备份数据库示例

     BACKUP DATABASE [yee]TO  DISK = N'D:\数据库备份\yee2015.9.11.bak'WITH  NAME = N'yee - 备份',  NOFORMAT, NO ...

  2. asp.net core mvc视频A:笔记3-2.表单使用

    页面上呈现表单的两种方式 新建项目,增加Test控制器,增加Index视图 方式一:HTML构建表单 运行 方式二:HTML 运行 数据绑定 处理方法 调用结果 登录后返回值 默认值绑定 方式一: 控 ...

  3. 1.5.2 WHERE子句

    1.5.2 WHERE子句正在更新内容,请稍后

  4. Struts2 后台获取路径的几种方法

    Struts2 后台获取路径的几种方法 package actions.app; import java.io.File; import org.apache.struts2.ServletActio ...

  5. [Material Design] 教你做一个Material风格、动画的button(MaterialButton)

    原创作品,转载请注明出处:http://blog.csdn.net/qiujuer/article/details/39831451 前段时间Android L 公布了,相信看过公布会了解过的朋友都为 ...

  6. Codeforces 14D Two Paths 树的直径

    题目链接:点击打开链接 题意:给定一棵树 找2条点不反复的路径,使得两路径的长度乘积最大 思路: 1.为了保证点不反复,在图中删去一条边,枚举这条删边 2.这样得到了2个树,在各自的树中找最长链.即树 ...

  7. 403/you don't have the permission to access on this server

    Localhost/index.php出现 错误403 you don't have the permission to access on this server 现在已经解决,特将方法与大家分享. ...

  8. IE8 兼容 getElementsByClassName

    IE8以下版本没有getElementsByClassName这个方法,以下是兼容写法 function ieGetElementsByClassName() { if (!document.getE ...

  9. Hadoop MapReduce八大步骤以及Yarn工作原理详解

    Hadoop是市面上使用最多的大数据分布式文件存储系统和分布式处理系统, 其中分为两大块分别是hdfs和MapReduce, hdfs是分布式文件存储系统, 借鉴了Google的GFS论文. MapR ...

  10. BestCoder Round #63 (div.2)

    感觉有些无聊的比赛. A 暴力枚举下就行 B 简单的dp,但是wa了一发后就去先把C做了,然后发现如果输入的100个数,是如1,2,3,4,...,100,然后k=50,个数为c(100,50).果断 ...