zoukankan      html  css  js  c++  java
  • C#Winform之等待窗体

    窗体主要代码:
    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

    慎于行,敏于思!GGGGGG
  • 相关阅读:
    [转]iOS应用程序多语言本地化解决方案
    【汇】iOS面试题
    [转]UIView 和 CALayer的那点事
    [转]25个增强iOS应用程序性能的提示和技巧 — 高级篇
    [转]25个增强iOS应用程序性能的提示和技巧 — 中级篇
    [转]25个增强iOS应用程序性能的提示和技巧 — 初级篇
    [转]NSNotification、delegate和KVO、KVC的区别
    [转]ViewController的生命周期
    [QualityCenter]设置工作流脚本-设置不同字段值关联不同列表
    [QualityCenter]设置工作流脚本-缺陷字段值发生变化时的处理
  • 原文地址:https://www.cnblogs.com/GarsonZhang/p/4062616.html
Copyright © 2011-2022 走看看