您的位置:首页 > 编程语言 > C#

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;

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C# timer 等待界面