无法从方法组转换为 System.Func<string>
Cannot convert from method group to System.Func<string>
我正在尝试使用 .Net 4.0 任务 class 在后台使用线程下载 google 网页。问题是如果我的函数有 1 个或多个参数,应用程序将无法编译(不知道如何传递该参数)。所以我想知道如何在 DoWork() 方法中传递函数的参数。
这个有效:
public Task<String> DoWork() {
//create task, of which runs our work of a thread pool thread
return Task.Factory.StartNew<String>(this.DownloadString);
}
private String DownloadString()
{
using (var wc = new WebClient())
return wc.DownloadString("http://www.google.com");
}
这不是:
public Task<String> DoWork() {
//create task, of which runs our work of a thread pool thread
return Task.Factory.StartNew<String>(this.DownloadString);
}
private String DownloadString(String uri)
{
using (var wc = new WebClient())
return wc.DownloadString(uri);
}
错误是:
cannot convert from 'method group' to 'System.Func<string>'
提前致谢!
return Task.Factory.StartNew(() => this.DownloadString("http://...."));
return Task.Factory.StartNew(() => DownloadString("https://www.google.com"));
或
return Task.Factory.StartNew(() =>
{
using (var wc = new WebClient())
return wc.DownloadString("https://www.google.com");
});
我正在尝试使用 .Net 4.0 任务 class 在后台使用线程下载 google 网页。问题是如果我的函数有 1 个或多个参数,应用程序将无法编译(不知道如何传递该参数)。所以我想知道如何在 DoWork() 方法中传递函数的参数。
这个有效:
public Task<String> DoWork() {
//create task, of which runs our work of a thread pool thread
return Task.Factory.StartNew<String>(this.DownloadString);
}
private String DownloadString()
{
using (var wc = new WebClient())
return wc.DownloadString("http://www.google.com");
}
这不是:
public Task<String> DoWork() {
//create task, of which runs our work of a thread pool thread
return Task.Factory.StartNew<String>(this.DownloadString);
}
private String DownloadString(String uri)
{
using (var wc = new WebClient())
return wc.DownloadString(uri);
}
错误是:
cannot convert from 'method group' to 'System.Func<string>'
提前致谢!
return Task.Factory.StartNew(() => this.DownloadString("http://...."));
return Task.Factory.StartNew(() => DownloadString("https://www.google.com"));
或
return Task.Factory.StartNew(() =>
{
using (var wc = new WebClient())
return wc.DownloadString("https://www.google.com");
});