我正在开发一个c#user-control @ work。控件只加载一些信息和数据并显示它。
现在我想为控件的用户提供一个以异步方式加载数据的选项..像这样:
Cntrl.LoadSmthAsync(..)
Cntrl.LoadSmthComplete //EventHandler to give feedback if the Load was successfull.
我决定使下载功能Async并通过EventHandlers提供返回值。但是那个代码毕竟很复杂。
这里有一些代码来理解控件应该做什么:
public byte[] LoadByPos(int pos)
{
string url = Pos2Url(pos);
// update gui
this.textBox1.Text = url;
byte[] res = LoadByUrl(url);
// update gui
this.textBox2.Text = BytesToString(res);
return res;
}
public byte[] LoadByUrl(string url)
{
return Download(url);
}
//primary problem: download function
private byte[] Download(string url)
{
System.Threading.Thread.Sleep(1000 * 30);
return StringToBytes(url);
}
//secondary problem: an other function
private string Pos2Url(int pos)
{
System.Threading.Thread.Sleep(1000 * 5);
return pos.ToString();
}
// LoadByPosAsync
public delegate void LoadByPosDoneHandler(Object sender, byte[] e);
public event LoadByPosDoneHandler LoadByPosDone;
public void LoadByPosAsync(int pos)
{
string url = Pos2Url(pos);
// update gui
this.textBox1.Text = url;
LoadByUrlDone += new LoadByUrlDoneHandler(LoadByPosAsync_LoadByUrlDone);
LoadByUrlAsync(url);
}
public void LoadByPosAsync_LoadByUrlDone(object sender, byte[] e)
{
// update gui
this.textBox2.Text = BytesToString(e);
LoadByUrlDone = null;
LoadByPosDone(sender, e);
}
//LoadByUrlAsync
public delegate void LoadByUrlDoneHandler(Object sender, byte[] e);
public event LoadByUrlDoneHandler LoadByUrlDone;
public void LoadByUrlAsync(string url)
{
DownloadDone += new DownloadDoneHandler(LoadByUrlAsync_DownloadDone);
DownloadAsync(url);
}
private void LoadByUrlAsync_DownloadDone(object sender, byte[] e)
{
LoadByUrlDone(sender, e);
}
//DownloadAsync
private delegate void DownloadDoneHandler(Object sender, byte[] e);
private event DownloadDoneHandler DownloadDone;
private void DownloadAsync(string url)
{
BackgroundWorker bw_DownloadAsync = new BackgroundWorker();
bw_DownloadAsync.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_DownloadAsync_RunWorkerCompleted);
bw_DownloadAsync.DoWork += new DoWorkEventHandler(bw_DownloadAsync_DoWork);
bw_DownloadAsync.RunWorkerAsync(url);
}
void bw_DownloadAsync_DoWork(object sender, DoWorkEventArgs e)
{
byte[] res = Download((string)e.Argument);
e.Result = res;
}
void bw_DownloadAsync_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
DownloadDone(sender, (byte[])e.Result);
}
有没有更简单的方法来完成我打算做的事情?
事先提前答案 0 :(得分:0)
有没有更简单的方法来完成我打算做的事情?
分别有实现方面,但是如果你使用的是.NET 4,那么强烈建议你不要将此模型用于异步。我鼓励您使用Task<T>
,而不是使用事件处理程序。如果您没有任何固有支持,则可以使用TaskCompletionSource
创建任务,这意味着由于异步,您的API将更容易在C#5中使用 /等待支持。此时,您的UI线程方法将如下所示:
public async Task<byte[]> LoadByPos(int pos)
{
string url = await Pos2UrlAsync(pos);
// update gui
this.textBox1.Text = url;
byte[] res = await LoadByUrlAsync(url);
// update gui
this.textBox2.Text = BytesToString(res);
return res;
}
异步将自然地流经您的代码。