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;
}
}
}
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;
}
}
}
- WinForm中异步刷新窗体
- WinForm中异步刷新窗体
- Winform中如何实现子窗体刷新父窗体
- winform 刷新父窗体
- Winform中如何实现父窗体传递数据到子窗体并刷新子窗体
- [Winform]winform 刷新父窗体(转)
- Winform子窗体刷新父窗体
- Winform子窗体刷新父窗体
- winform中子窗体关闭刷新父窗体
- Winform子窗体刷新父窗体
- Winform子窗体刷新父窗体
- 异步调用解决winform窗体假死
- Winform开发系列 - 子窗体刷新父窗体
- WinForm关闭子窗体时刷新父窗体
- WinForm中的DataGridView子窗体刷新父窗体备忘
- WinForm中的DataGridView子窗体刷新父窗体备忘
- winform 子窗体数据改变刷新父窗体
- WinForm中创建不规则窗体
- eclipse 4.2 集成了Marketplace插件及其他插件的eclipse下载分享
- android之WebKit简介
- 牛新庄谈创新性应用
- VB引用文件不成功时你该怎么办
- 小的软件初始化之前的工作
- WinForm中异步刷新窗体
- MFC DLL类型
- ORACLE EBS中附件功能的使用(上传用户操作文档)
- 如何在Console中使用cin、cout、cerr
- 第八章 启动与执行业务流程(二)
- OGRESE1 ObjectEditHandler
- 将Emacs配置成一个C/C++的IDE编程环境
- 第八章 启动与执行业务流程(三)
- QT中如何进行DEBUG和使用cout,cin等