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;
}
cdbitesky
  • 1,390
  • 1
  • 13
  • 30
ploxtic
  • 273
  • 3
  • 4
  • 20
  • 3
    Why do you need to return a `Task` rather than just a normal `int`? – thecoop Mar 17 '13 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 '13 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. – Eric Lippert Mar 17 '13 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. – Eric Lippert Mar 17 '13 at 14:59

4 Answers4

35

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.

spender
  • 117,338
  • 33
  • 229
  • 351
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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
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#

Community
  • 1
  • 1
jet_choong
  • 382
  • 3
  • 13
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).

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825