C#Winform之等待窗体
2020-12-13 14:22
标签:des winform style http io color os ar 使用 等待窗体调用类: Demo下载地址:http://pan.baidu.com/s/1pJ9HSRD C#Winform之等待窗体 标签:des winform style http io color os ar 使用 原文地址:http://www.cnblogs.com/GarsonZhang/p/4062616.html
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"
);
}
}
///
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)
{
}
}
}
}