我的主要表单有一个按钮。单击按钮时,我会填充网格。这是我的按钮点击例程
private void btnSearch_Click(object sender, EventArgs e)
{
if (chkExcludeCallCust.Checked)
{
if (chkEnable.Checked)
RangeExclude = 1;
else
RangeExclude = 0;
}
new Thread(() =>
{
lock (thisLock)
{
Search();
}
}).Start();
}
当用户点击搜索按钮时,会调用一个线程。从该线程我从数据库中获取数据并填充网格。
所以这是我的搜索程序:
private void Search()
{
DataSet ds = null;
if (wfrm.InvokeRequired)
{
wfrm.Invoke(new MethodInvoker(delegate
{
wfrm.Show();
wfrm.Refresh();
Application.DoEvents();
}));
}
else
{
wfrm.Show();
wfrm.Refresh();
Application.DoEvents();
}
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(delegate {
this.Cursor = Cursors.WaitCursor;
btnSearch.Enabled = false;
}));
}
else
{
this.Cursor = Cursors.WaitCursor;
btnSearch.Enabled = false;
}
if (!PingTest())
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(delegate
{
this.Cursor = Cursors.Default;
MessageBox.Show("VPN disconnected. Please connect and try again.", "DebtorCallerAssistance");
return;
}));
}
else
{
this.Cursor = Cursors.Default;
MessageBox.Show("VPN disconnected. Please connect and try again.", "DebtorCallerAssistance");
return;
}
}
if (chkExcludeCallCust.InvokeRequired)
{
chkExcludeCallCust.Invoke(new MethodInvoker(delegate
{
DataLayer.SetConnectionString(_country);
ds = LoadData(dtfrom.Value.ToString("yyyyMMdd"), dtto.Value.ToString("yyyyMMdd"), (chkExcludeCallCust.Checked ? 1 : 0), txtSearch.Text, 1, Pager.PageSize, false);
}));
}
else
{
DataLayer.SetConnectionString(_country);
ds = LoadData(dtfrom.Value.ToString("yyyyMMdd"), dtto.Value.ToString("yyyyMMdd"), (chkExcludeCallCust.Checked ? 1 : 0), txtSearch.Text, 1, Pager.PageSize, false);
}
if (ds.Tables.Count > 0)
{
if (ds.Tables[0] != null)
{
dtExport = ds.Tables[0];
}
}
if (outlookGrid1.InvokeRequired)
{
outlookGrid1.Invoke(new MethodInvoker(delegate
{
outlookGrid1.BindData(ds, "data");
View = "BoundInvoices";
DataGridViewCellEventArgs evt = new DataGridViewCellEventArgs(2, -1);
object sender = null;
//outlookGrid1_CellClick(sender, evt);
}));
}
else
{
//outlookGrid1.DataSource = ds.Tables[0];
outlookGrid1.BindData(ds, "data");
View = "BoundInvoices";
DataGridViewCellEventArgs evt = new DataGridViewCellEventArgs(2, -1);
object sender = null;
//outlookGrid1_CellClick(sender, evt);
}
if (wfrm.InvokeRequired)
{
wfrm.Invoke(new MethodInvoker(delegate
{
wfrm.Hide();
}));
}
else
{
wfrm.Hide();
}
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(delegate {
this.Cursor = Cursors.Default;
btnSearch.Enabled = true;
}));
}
else
{
btnSearch.Enabled = true;
this.Cursor = Cursors.Default;
}
}
在表单加载时,我实例化无边框窗口,如:
WaitForm wfrm = null;
public Feedback(string country)
{
InitializeComponent();
wfrm = new WaitForm();
}
我还为此窗口设置了一些属性,如:
startposition = CenterScreen
showinicon=false
showintaskbar=false
formboderstyle=none
WaitForm
仅在我第一次点击搜索按钮时显示我无法理解如果再次点击搜索按钮,为什么WaitForm
不会显示。
另一个问题是PictureBox
上WaitForm
已分配了动画GIF。第一次显示WaitForm
时,动画未播放。为什么动画不是我播放WinForm时播放的动画?
请告诉我需要更改代码才能在我点击搜索按钮时显示WaitForm
。我也想知道如何在WaitForm
的{{1}}动画中制作GIF。
答案 0 :(得分:1)
启动线程并将所有内容调回GUI线程完全是胡说八道。 DoEvents是代码气味的指标。捆绑您的调用作为调用创建一个大的开销
将您的代码更改为以下内容:
private void btnSearch_Click(object sender, EventArgs e)
{
this.btnSearch.Enabled = false;
this.Cursor = Cursors.WaitCursor;
this.wfrm.Show();
Thread t = new Thread(this.Search);
t.Start();
}
private void Search()
{
while (isWorking)
{
DoHeavyWork();
this.Invoke(new Action(ReportToWaitForm);
}
this.Invoke(new Action(() =>
{
this.btnSearch.Enabled = true;
this.Cursor = Cursors.Default;
this.wfrm.Hide();
}));
}