无法从方法组转换为System.Func< string> [英] Cannot convert from method group to System.Func<string>

查看:351
本文介绍了无法从方法组转换为System.Func< string>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩.Net 4.0 Task类,以使用线程在后台下载google网页。问题是,如果我的函数具有1个或多个参数,则应用程序将无法编译(idk如何传递该参数)。因此,我想知道如何在DoWork()方法中传递函数的参数。

I'm playing a bit with the .Net 4.0 Task class to download the google webpage in background using a thread. The problem is that if my function has 1 or more parameters, the application won't compile (idk how to pass that parameter). So I wonder how can I pass the function's parameter in the DoWork() method.

这可行:

    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://...."));

这篇关于无法从方法组转换为System.Func&lt; string&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆