【发布时间】:2010-11-10 00:26:05
【问题描述】:
我正在使用 vs.NET 2010 和 C# windows 窗体开发一个 windows 应用程序。这个应用程序有一个用户控件来查询服务(WCF 托管在 win 服务上),并且需要在不阻塞 UI 的情况下执行此操作。用户控件包含将显示该结果的网格。我认为我的情况是最常见的。我的问题是用 C# 可以做什么,以使以下代码运行更顺畅并具有更好的错误处理。我正在使用 MehtodInvoker,所以我可以避免为此调用编写两个单独的方法 - 等待 - 填充场景。
public void LoadData()
{
StartWaitProgress(0);
ThreadPool.QueueUserWorkItem(x =>
{
try
{
MyDocMail[] mails;
var history = Program.NoxProxy.GetDocumentHistory(out mails, Program.MySessionId, docId);
this.Invoke(new MethodInvoker(delegate()
{
this.SuspendLayout();
gridVersions.Rows.Clear();
foreach (var item in history)
{
gridVersions.Rows.Add();
int RowIndex = gridVersions.RowCount - 1;
DataGridViewRow demoRow = gridVersions.Rows[RowIndex];
demoRow.Tag = item.Id;
if (gridVersions.RowCount == 1)
{
demoRow.Cells[0].Value = Properties.Resources.Document_16;
}
demoRow.Cells[1].Value = item.Title;
demoRow.Cells[2].Value = item.Size.GetFileSize();
demoRow.Cells[3].Value = item.LastModified;
demoRow.Cells[4].Value = item.CheckoutBy;
demoRow.Cells[5].Value = item.Cotegory;
}
gridEmails.Rows.Clear();
foreach (var item in mails)
{
gridEmails.Rows.Add();
int RowIndex = gridEmails.RowCount - 1;
DataGridViewRow demoRow = gridEmails.Rows[RowIndex];
demoRow.Tag = item.Id;
demoRow.Cells[1].Value = item.From;
demoRow.Cells[2].Value = item.To;
demoRow.Cells[3].Value = item.Date;
}
this.ResumeLayout();
}));
}
catch (Exception ex)
{
Program.PopError(ex);
this.Invoke(new MethodInvoker(delegate() { this.Close(); }));
}
finally { this.Invoke(new MethodInvoker(delegate() { StopWaitProgress(); })); }
});
}
【问题讨论】:
标签: c# winforms wcf asynchronous