//查询按钮点击事件
private void btnSearch_Click(object sender, EventArgs e)
{
if (dtpBeginTime.Value > dtpEndTime.Value)
{
MessageBox.Show("开始时间不能大于结束时间", "查询提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (cobCustoms.Text == "")
{
MessageBox.Show("请选择客户", "查询提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
try
{
tmcounter.Enabled = true;
searchSoEven = new FlushControl(SearchDataMethod);
AsyncCallback ac = new AsyncCallback(CallBack);
searchSoEven.BeginInvoke(ac,null);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString() + ",数据查询失败", "查询提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
panel1.Visible = false;
tmcounter.Enabled = false;
return;
}
} //导步回调方法
private void CallBack(IAsyncResult ir)
{
searchSoEven.EndInvoke(ir);
} //查询数据方法
private void SearchDataMethod()
{
panel1.Visible = true;
Search_BLL sb = new Search_BLL();
dgvSOshow.DataSource = sb.SearchSoBLL(dtpBeginTime.Value.ToString().Substring(0, 9), dtpEndTime.Value.ToString().Substring(0, 9), cobCustoms.Text.ToString());
dgvSOshow.Refresh();
this.Invalidate();
this.Update();
panel1.Visible = false;
} 数据查出来了,但是点击窗体的时候还是会假死,无法操作,好像是异步线程没有停下来,回到主线程。这是怎么事呀!
private void btnSearch_Click(object sender, EventArgs e)
{
if (dtpBeginTime.Value > dtpEndTime.Value)
{
MessageBox.Show("开始时间不能大于结束时间", "查询提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (cobCustoms.Text == "")
{
MessageBox.Show("请选择客户", "查询提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
try
{
tmcounter.Enabled = true;
searchSoEven = new FlushControl(SearchDataMethod);
AsyncCallback ac = new AsyncCallback(CallBack);
searchSoEven.BeginInvoke(ac,null);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString() + ",数据查询失败", "查询提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
panel1.Visible = false;
tmcounter.Enabled = false;
return;
}
} //导步回调方法
private void CallBack(IAsyncResult ir)
{
searchSoEven.EndInvoke(ir);
} //查询数据方法
private void SearchDataMethod()
{
panel1.Visible = true;
Search_BLL sb = new Search_BLL();
dgvSOshow.DataSource = sb.SearchSoBLL(dtpBeginTime.Value.ToString().Substring(0, 9), dtpEndTime.Value.ToString().Substring(0, 9), cobCustoms.Text.ToString());
dgvSOshow.Refresh();
this.Invalidate();
this.Update();
panel1.Visible = false;
} 数据查出来了,但是点击窗体的时候还是会假死,无法操作,好像是异步线程没有停下来,回到主线程。这是怎么事呀!
我看你这个代码像是在非UI线程上直接更新UI上的东西.
因为还没想到更好的办法,所以只能这样。不知道各位有没有一些好的方法,或有好的窗体线程程序例子
在主线程中waitone,在子线程中set。楼主说的
Control.CheckForIllegalCrossThreadCalls = false;
个人感觉效果不好。
{
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
} private void Form1_Load(object sender, EventArgs e)
{
InitControl();
} private void btnReset_Click(object sender, EventArgs e)
{
InitControl();
} private void InitControl()
{
listView1.Items.Clear();
for (int i = 0; i < 100; i++)
{
listView1.Items.Add(new ListViewItem(i.ToString()));
}
} private void btnDo_Click(object sender, EventArgs e)
{
MyTestClass cls = new MyTestClass();
cls.MyDoEvent += new DoHandler(cls_MyDoEvent);
cls.Count = listView1.Items.Count;
ThreadStart start = new ThreadStart(cls.DoSome);
Thread th = new Thread(start);
th.Start();
} void cls_MyDoEvent(int i)
{
listView1.Items[i].Text = string.Format("{0}...",i);
} public delegate void DoHandler(int i); private class MyTestClass
{
public event DoHandler MyDoEvent; private int count = 0;
public int Count
{
set
{
count = value;
}
}
public void DoSome()
{
if(MyDoEvent!=null)
{
for (int i = 0; i <count; i++)
{
Thread.Sleep(100);
MyDoEvent.Invoke(i);
}
}
}
}
}