简体   繁体   English

无法将类型“字符串”隐式转换为“System.Threading.Tasks.Task”<string> ' 在 c#</string>

[英]Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>' in c#

I have this code我有这个代码

    private void button1_Click(object sender, EventArgs e)
    {
        //===========> Getting error here <==================//
        textBox2.Text = CallFunc(textBox1.Text);
    }

    static async Task<string> CallFunc(string str)
    {
        Program p = new Program();
        string s = await p.DocumentToText(str);
        return s;
    }


public async Task<string> DocumentToText(string path)
    {
        string txt = String.Empty;
        AmazonTextractClient client = new AmazonTextractClient(key, Skey, reg);
        //some AWS functionality

        Thread.Sleep(2000);
        txt = "hello world";
        return txt;
     }

I changed this button1_Click function to我将此button1_Click function 更改为

    private void button1_Click(object sender, EventArgs e)
    {
        var d = await CallFunc(textBox1.Text);
        textBox2.Text = d.Results();
    }

as was recommended by one answers of this question正如这个问题的一个答案所建议的那样

Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>' 无法将类型“string”隐式转换为“System.Threading.Tasks.Task<string>”

but that still does not work但这仍然行不通

在此处输入图像描述

Add async in button1_Click eventbutton1_Click事件中添加async

private async void button1_Click(object sender, EventArgs e)
{
    var d = await CallFunc(textBox1.Text);
    textBox2.Text = d;
}

Use Async Task in button1_Click method在 button1_Click 方法中使用异步任务

private async Task button1_Click(object sender, EventArgs e)
{
    var d = await CallFunc(textBox1.Text);
    textBox2.Text = d.Results();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法隐式转换类型&#39;System.Threading.Tasks.Task <String> “串” - Cannot implicitly convert type 'System.Threading.Tasks.Task<String> to 'string' 错误 CS0029:无法将类型“字符串”隐式转换为“System.Threading.Tasks.Task”<string> '</string> - Error CS0029: Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>' 无法将类型“字符串”隐式转换为“System.Threading.Tasks.Task”<string> &#39; - Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>' 无法隐式转换类型&#39;System.Threading.Tasks.Task <object> 在SignalR中从&#39;到&#39;string&#39; - Cannot implicitly convert type 'System.Threading.Tasks.Task<object>' to 'string' in SignalR 无法将类型&#39;void&#39;隐式转换为&#39;System.Threading.Tasks.Task&#39; - Cannot implicitly convert type 'void' to 'System.Threading.Tasks.Task' 无法隐式转换类型&#39;System.Threading.Tasks.Task - Cannot implicitly convert type 'System.Threading.Tasks.Task 无法将类型&#39;bool&#39;隐式转换为&#39;System.Threading.Tasks.Task&#39; - Cannot implicitly convert type 'bool' to 'System.Threading.Tasks.Task' 无法将类型&#39;System.Collections.Generic.List &lt;&gt;&#39;隐式转换为&#39;System.Threading.Tasks.Task &lt;&gt;&gt; - Cannot implicitly convert type 'System.Collections.Generic.List<>' to 'System.Threading.Tasks.Task<>> 无法将类型&#39;bool&#39;隐式转换为&#39;system.threading.tasks.task bool&#39; - Cannot implicitly convert type 'bool' to 'system.threading.tasks.task bool' 无法将类型“System.Threading.Tasks.Task”隐式转换为“Windows.Foundation.IAsyncAction”。 存在显式转换 - Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'Windows.Foundation.IAsyncAction'. An explicit conversion exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM