11

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;
}
4
  • 3
    Why do you need to return a Task<int> rather than just a normal int?
    – thecoop
    Mar 17, 2013 at 14:43
  • i need it to run on another thread. How do you pass in instructions from the UI to a task?
    – ploxtic
    Mar 17, 2013 at 14:50
  • 4
    You seem to be under the mistaken impression that an async method runs on another thread. It does not. An async method contains an asynchronous await, and returns immediately when the await happens. It then resumes where it left off when the awaited value is available. Make sure you understand that; it is absolutely not the case that an async method necessarily runs on another thread; in fact, one of the reasons we added async methods to C# was to decrease the reliance on threads as an asynchrony mechanism. Mar 17, 2013 at 14:55
  • 2
    Looking at your code, it appears to be both reading and writing fields of controls; if that's the case, this method may only be run on the UI thread. Frankly this code looks very suspicious. What I would do if I were you is start a new question, describe carefully what you are trying to do, and ask for help making that happen. Mar 17, 2013 at 14:59

4 Answers 4

33

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.

10

As the error clearly states, you can't return an int as a Task<int>. (unless you make it an async method, which does compile-time magic to create a Task<T>.

If your method isn't asynchronous, you shouldn't be returning a Task<T> in the first place.
Instead, just return int directly.

If, for some reason, you need to return a Task<T>, you can call Task.FromResult() to create a finished task with a given value.

4

The code here is obviously incorrect. Try to look at the return type in your code. You are returning and int which mismatch the return type that expecting a Task<int>. If you are not going to use async await in this method, you can just change your return type to int.

However, if you insist on returning Task<int> instead of int, you can use the following for your return statement

return Task.FromResult(count)

This will wrap your int into Task<int>. For more information of Task.FromResult, you can visit : https://msdn.microsoft.com/en-us/library/hh194922(v=vs.110).aspx What is the use for Task.FromResult<TResult> in C#

3

There is nothing in this method indicating that it is an asynchronous method, except for the fact that you've declared it to return Task<int>.

However, you're not returning a Task<int>, you're returning count, an int.

Since you're not waiting for the action to complete, I would remove the Task<int> return type and replace it with just int instead as this method is completely synchronous (except for the part you're not waiting for anyway).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.