无法隐式转换类型'字符串'到'System.Threading.Tasks.Task&LT;串GT;' [英] Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>'
问题描述
我是新来的异步编程,因此通过一些异步采样codeS去后,我想编写一个简单的异步code的
I am new to asynchronous programming, so after going through some async sample codes, I thought of writing a simple async code
我创建了一个简单的WinForm应用程序和表单里面我写了下面code。但它只是不工作
I created a simple Winform application and inside the Form I wrote the following code. But its just not working
private Task<string> methodAsync() {
Thread.Sleep(10000);
return "Hello"; //Error: Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>'
}
private async void button1_Click(object sender, EventArgs e)
{
string s = await methodAsync();
MessageBox.Show(s);
}
可能有人请把一些光在这里..
Could someone please put some light here..
推荐答案
该方法的列出的返回类型是任务&LT;串GT;
。你试图返回字符串
。他们是不一样的,也没有从字符串隐式转换为任务&LT;字符串方式&gt;
,因此误差
The listed return type of the method is Task<string>
. You're trying to return a string
. They are not the same, nor is there an implicit conversion from string to Task<string>
, hence the error.
你很可能与异步
方法,其返回值是自动包装在这混乱工作
由编译器。目前,这种方法不能异步方法。你几乎可以肯定,为了做到这一点:
You're likely confusing this with an async
method in which the return value is automatically wrapped in a Task
by the compiler. Currently that method is not an async method. You almost certainly meant to do this:
private async Task<string> methodAsync()
{
await Task.Delay(10000);
return "Hello";
}
有两个重要的变化。首先,该方法被标记为异步
,这意味着返回类型包裹在一个工作
,使得该方法编译。接下来,我们不想做一个阻塞等待。作为一般规则,使用时的await
模式总是避免时,你可以阻止等待。 Task.Delay
是将指定的毫秒数后可以完成的任务。通过等待
-ing这一任务,我们有效地在这个时间进行非阻塞的等待(实际上该方法的其余部分是工作的延续)。
There are two key changes. First, the method is marked as async
, which means the return type is wrapped in a Task
, making the method compile. Next, we don't want to do a blocking wait. As a general rule, when using the await
model always avoid blocking waits when you can. Task.Delay
is a task that will be completed after the specified number of milliseconds. By await
-ing that task we are effectively performing a non-blocking wait for that time (in actuality the remainder of the method is a continuation of that task).
如果您preFER做这件事的方式4.0,而无需使用伺机
,你可以这样做:
If you prefer a 4.0 way of doing it, without using await
, you can do this:
private Task<string> methodAsync()
{
return Task.Delay(10000)
.ContinueWith(t => "Hello");
}
第一个版本将编译下来的东西,或多或少是这样,但它会产生一些额外的样板code在他们的支持错误处理和的其他功能等待
我们不充分利用这里。
如果你的 Thread.sleep代码(10000)
是真正的意思仅仅是对一些长期运行的方法的占位符,而不是仅仅等待了一段时间的方式,那么你需要确保工作在另一个线程中完成的,而不是当前的背景下,。这样做的最简单的方法就是通过 Task.Run
:
If your Thread.Sleep(10000)
is really meant to just be a placeholder for some long running method, as opposed to just a way of waiting for a while, then you'll need to ensure that the work is done in another thread, instead of the current context. The easiest way of doing that is through Task.Run
:
private Task<string> methodAsync()
{
return Task.Run(()=>
{
SomeLongRunningMethod();
return "Hello";
});
}
或更可能是:
private Task<string> methodAsync()
{
return Task.Run(()=>
{
return SomeLongRunningMethodThatReturnsAString();
});
}
这篇关于无法隐式转换类型'字符串'到'System.Threading.Tasks.Task&LT;串GT;'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!