WinForm中异步刷新窗体

来源:互联网 发布:淘宝客服年度总结 编辑:程序博客网 时间:2024/06/05 06:47
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;
using System.Threading;


namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }


        private void Form3_Load(object sender, EventArgs e)
        {
            Flag.IsOpen = true;
        }


        public class Flag
        {
            public static bool IsOpen { set; get; }
            public static object lockObj = new object();
        }


        //声明委托
        public delegate void FunDelegate();
        public delegate void SetValue(ProductClass i);
        public delegate void CloseForm();


        public void CallBack(IAsyncResult result)
        {
            MessageBox.Show(result.AsyncState.ToString());
        }


        public void FunProc()
        {
            for (int i = 0; i < 5000; i++)
            {
                Thread.Sleep(100);


                if (Flag.IsOpen && this != null && !this.IsDisposed)
                {
                    lock (Flag.lockObj)
                    {
                        if (Flag.IsOpen && this != null && !this.IsDisposed)
                        {
                            ProductClass pc = new ProductClass(i, i.ToString(), i.ToString());
                            this.Invoke(new SetValue(SetListBox), pc);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                else
                {
                    break;
                }
            }
        }


        public void SetListBox(ProductClass i)
        {
            if (Flag.IsOpen && this != null && !this.IsDisposed)
            {
                dataGridView1.Rows.Add(i.Id.ToString(), i.Name, i.Address);
                listBox1.Items.Add(i.Id);
            }
        }


        private void Form3_FormClosing_1(object sender, FormClosingEventArgs e)
        {
            if (Flag.IsOpen)
            {
                Flag.IsOpen = false;


                if (Monitor.TryEnter(Flag.lockObj, 2000))
                {
                    Monitor.Exit(Flag.lockObj);
                }
                else
                {
                    e.Cancel = true;
                    if (this.IsHandleCreated)
                    {
                        new Thread(() =>
                        {
                            Thread.Sleep(100);
                            this.Invoke(new CloseForm(() => this.Close()));
                        }).Start();
                    }
                }
            }


        }


        private void button1_Click_1(object sender, EventArgs e)
        {
            //执行
            FunDelegate fundelegate = new FunDelegate(FunProc);
            AsyncCallback callback = new AsyncCallback(CallBack);
            IAsyncResult result = fundelegate.BeginInvoke(callback, "处理结束");


        }


    }
    public class ProductClass
    {
        private int id;


        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private string name;


        public string Name
        {
            get { return name; }
            set { name = value; }
        }


        private string address;


        public string Address
        {
            get { return address; }
            set { address = value; }
        }


        public ProductClass(int id, string name, string address)
        {
            this.Id = id;
            this.Name = name;
            this.Address = address;
        }
    }
}
原创粉丝点击