0

Note: This is C# in Xamarin Studio

I am trying to convert my currently synchronous method into an asynchronous one as the method triggered by the TouchUpInside event calls a second method in a different class that does a REST call which can take up to 45 seconds to complete.

Problem I am getting the error

Expression denotes a 'method group', where a 'variable', 'value' or `type' was expected

on the line

btnStartIdCheck.TouchUpInside += async (object sender, EventArgs e) => await runIDCheckCameraProcess;

I am new to threading and this has completely flummoxed me...

The documentation from https://developer.xamarin.com/guides/cross-platform/advanced/async_support_overview/ is where I initially started and then tried various solutions on SoF such as The 'await' operator can only be used within an async lambda expression and I'm trying to turn a synchronous method from some old code into an asynchronous method, but I'm having some trouble understand without much difference in my errors other than suggestions by intellisense telling me ensure my lambda was marked with the async keyword.

ViewController Class (paired down for brevity)

using Foundation;
using System;
using UIKit;
using Performance_Shared;
using System.Threading.Tasks;

namespace Performance
{
    partial class VCIDCheck : UIViewController
    {
        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();

            btnStartIdCheck.TouchUpInside += async (object sender, EventArgs e) => await runIDCheckCameraProcess;

            // Lambda attempt gives same error on enclosed await
            // btnStartIdCheck.TouchUpInside += async delegate {
            //   await runIDCheckCameraProcess;
            // };

            btnCloseIdCheck.TouchUpInside += btnCancelCloseTheWindow;
        }

        private async Task<bool> runIDCheckCameraProcess (Object sender, EventArgs ea) 
        {
            //...Omitted code to run camera, take photo, save to private folder etc...

                json_IDCheck postIDCheckData = new json_IDCheck();

                // Do REST call to remote server, this is the async call
                // 'postIDCheckData' is a Class holding some data that is passed to API

                DruidRest druidPostIDCheck = new DruidRest(SingletonAppSettngs.Instance ().baseApiUri);
                jsonIDCheckRequestStatus requestStatus = druidPostIDCheck.postIDCheck (postIDCheckData);

                // ...Do some more stuff with the response...
        }
    }
}

DruidRest Class showing only the postIDCheck method

namespace Performance_Shared
{
    public class DruidRest : IDruidRest
    {

        public DruidRest (string api_url)
        {
            this.apiBaseUrl = api_url;
        }

        public Task<jsonIDCheckRequestStatus> postIDCheckAsync(json_IDCheck idCheckData)
        {
            jsonIDCheckRequestStatus jsonIDCheckStatus = new jsonIDCheckRequestStatus ();
            jsonIDCheckStatus.div_check_status = "Completed";
            jsonIDCheckStatus.ErrorMessage = null;

            return Task.Factory.StartNew (() =>
            {
                    Thread.Sleep(3000);
                    return jsonIDCheckStatus;
            });
        }
    }
}
Community
  • 1
  • 1
John Cogan
  • 1,034
  • 4
  • 16
  • 39

1 Answers1

2

runIDCheckCameraProcess is a method - to call it, you must use parentheses and supply the necessary parameters. If the method does not actually use the two parameters, then you can probably just pass null.

btnStartIdCheck.TouchUpInside += async (object sender, EventArgs e) => await runIDCheckCameraProcess(null,null);

This is a common C# error - it really has nothing to do with async/await.

Jason
  • 86,222
  • 15
  • 131
  • 146