且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法隐式转换类型'诠释'到'......任务和LT; INT>'

更新时间:2022-12-14 21:50:06

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

 返回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.