C# 等待窗体 利用timer设置窗体是否关闭
2015-08-27 16:30
585 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;
namespace Myproject
{
public class WaitPlz
{
public Thread thread;
public FormWait formwait;
private bool finished; //指示线程工作是否完成
public bool has_finished()
{
return finished;
}
public WaitPlz()
{
this.thread = null;
this.formwait = null;
}
public void Show()
{
this.finished = false;
this.thread = new Thread(ThreadFunction);
thread.IsBackground = true;
thread.Start();
}
private void ThreadFunction()
{
this.formwait = new FormWait(this);
this.formwait.StartPosition = FormStartPosition.Manual;
Point center = new Point(Application.OpenForms[0].Location.X + (Application.OpenForms[0].Width - formwait.Width) / 2, Application.OpenForms[0].Location.Y + (Application.OpenForms[0].Height + formwait.Height) / 2);
this.formwait.Location = center;
this.formwait.ShowDialog();
}
public void Close()
{
this.finished = true;
this.thread.Join();
}
}
}
//////////////////////////////窗体类文件/////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Myproject
{
public partial class FormWait : Form
{
private WaitPlz owner; //窗体所属线程
public FormWait(WaitPlz owner)
{
InitializeComponent();
this.owner = owner;
timer1.Interval = 10;
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e) //timer 实时监测owner.has_finished()
{ //如果船体所属线程指示工作完成 则关闭窗体
if (this.owner.has_finished())
{
this.Close();
}
}
private void FormWait_FormClosing(object sender, FormClosingEventArgs e)
{
timer1.Enabled = false;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;
namespace Myproject
{
public class WaitPlz
{
public Thread thread;
public FormWait formwait;
private bool finished; //指示线程工作是否完成
public bool has_finished()
{
return finished;
}
public WaitPlz()
{
this.thread = null;
this.formwait = null;
}
public void Show()
{
this.finished = false;
this.thread = new Thread(ThreadFunction);
thread.IsBackground = true;
thread.Start();
}
private void ThreadFunction()
{
this.formwait = new FormWait(this);
this.formwait.StartPosition = FormStartPosition.Manual;
Point center = new Point(Application.OpenForms[0].Location.X + (Application.OpenForms[0].Width - formwait.Width) / 2, Application.OpenForms[0].Location.Y + (Application.OpenForms[0].Height + formwait.Height) / 2);
this.formwait.Location = center;
this.formwait.ShowDialog();
}
public void Close()
{
this.finished = true;
this.thread.Join();
}
}
}
//////////////////////////////窗体类文件/////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Myproject
{
public partial class FormWait : Form
{
private WaitPlz owner; //窗体所属线程
public FormWait(WaitPlz owner)
{
InitializeComponent();
this.owner = owner;
timer1.Interval = 10;
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e) //timer 实时监测owner.has_finished()
{ //如果船体所属线程指示工作完成 则关闭窗体
if (this.owner.has_finished())
{
this.Close();
}
}
private void FormWait_FormClosing(object sender, FormClosingEventArgs e)
{
timer1.Enabled = false;
}
}
}
相关文章推荐
- c#调用COM组件
- C#实现把指定数据写入串口
- C#动态创建button的方法
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#使用加边法计算行列式的值
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#通用邮件发送类分享
- C#中this的用法集锦
- C#.NET获取拨号连接的宽带连接方法
- C#实现AddRange为数组添加多个元素的方法
- C#中Equality和Identity浅析
- C#生成饼形图及添加文字说明实例代码
- C#判等对象是否相等的方法汇总
- C#简单的向量用法实例教程
- C#实现基于链表的内存记事本实例
- C#托管堆对象实例包含内容分析
- C#实现按照指定长度在数字前补0方法小结
- C# 通过 inline-asm 解决嵌入x86汇编