3 Replies - 2130 Views - Last Post: 22 July 2012 - 06:55 AM Rate Topic: -----

#1 scolty  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 259
  • Joined: 27-April 11

Task wont return a bool

Posted 21 February 2012 - 03:04 PM

Evening,

Im trying to get a bool returned from a task but it keeps displaying the following warning.

Quote

Not all code paths return a value in lambda expression of type 'System.Func<bool>'


Here is the code. Note: If i remove the 2 <bool> then it works fine except it doesnt return a value.
public void CallMethodCallAsync(int initial, int final, string filePath)
        {
            Task<bool> tsk = Task.Factory.StartNew<bool>(() =>
            {
                MethodCall(initial, final, filePath);
            });
        }

        public bool MethodCall(int initial, int final, string filePath)
        {
            return true;
        }



any pointers would be appreciated.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Task wont return a bool

#2 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Task wont return a bool

Posted 21 February 2012 - 03:20 PM

You are missing a return keyword. It has to be used since your lambda is multi line. So this would be correct:

            Task<bool> tsk = Task.Factory.StartNew<bool>(() =>
            {
                return MethodCall(initial, final, filePath);
            });



In one line lambdas it's not required so this works:

            Task<bool> tsk = Task.Factory.StartNew<bool>(() => MethodCall(initial, final, filePath));


Was This Post Helpful? 3
  • +
  • -

#3 scolty  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 259
  • Joined: 27-April 11

Re: Task wont return a bool

Posted 22 February 2012 - 12:20 AM

Morning Janne,
Thanks alot bro, ive spent a good few hrs scanning the web/books trying to find a similar example but they either returned a value or called a method with parameters. I cant believe how simple the answer was!

Thanks again
Was This Post Helpful? 1
  • +
  • -

#4 sbarnes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-July 12

Re: Task wont return a bool

Posted 22 July 2012 - 06:55 AM

Thanks, all. Spent considerably less than several hours once I resorted to googling this very error message AND finding this post.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1