无法隐式转换类型'诠释'到'......任务和LT; INT>' [英] Cannot implicitly convert type 'int' to '...Tasks<int>'

查看:217
本文介绍了无法隐式转换类型'诠释'到'......任务和LT; INT>'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这是异步,它会没有返回错误,为什么会引发错误而不异步,异步是该操作不值钱。

 公共任务< INT> COUNTUP()
{
字符串比较= txtTag.Text;
诠释计数= 0;
的for(int i = 0; I< dataGridView1.Rows.Count;我++)
{
如果(比较== dataGridView1 [0,1] .Value.ToString())
{
的BeginInvoke(新动作(()=>
{
计数++;
txtCount.Text = count.ToString();
})) ;
}
}

返回计数;
}


解决方案

那么,你的可以的返回一个完成任务:

 返回Task.FromResult(计数); 

http://msdn.microsoft.com/en-us/library/hh194922.aspx



为什么你会想要返回一个任务是一个有点神秘,但。从概念上讲,任务代表一个承诺的东西将在未来一段时间内发生。根据你的情况,它已经发生了,所以用一个任务是完全没有意义的。


if this is async, it'll return with no error, why is it throwing an error without being async, async is worthless in this operation.

public Task<int> countUp()
{
    string compare = txtTag.Text;
    int count = 0;
    for (int i = 0; i < dataGridView1.Rows.Count; i++)
    {
        if (compare == dataGridView1[0, i].Value.ToString())
        {
            BeginInvoke(new Action(() =>
            {
                count++;
                txtCount.Text = count.ToString();
            }));
        }
    }

    return count;
}

解决方案

Well, you could return a completed Task:

return Task.FromResult(count);

http://msdn.microsoft.com/en-us/library/hh194922.aspx

Why you'd want to return a Task is a bit of a mystery though. Conceptually, a Task represents a promise that something will happen at some time in the future. In your case, it's already happened, so using a Task is completely pointless.

这篇关于无法隐式转换类型'诠释'到'......任务和LT; INT&GT;'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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