首页 > 代码库 > C#Winform之等待窗体
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 | /// /// Using Singleton Design Pattern /// 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
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 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之等待窗体
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。