Hi

On 2013.04.17 01:22, Paul Johnson wrote:
Hi,

I've used async calls before now in a couple of different projects and wanted to see if the new async stuff in the 4.7 branch will make things simpler.

My old code looks like this
(in OnCreate)

Button login = FindViewById<Button> (Resource.Id.buttonSignIn);
login.Click += SignIn_Click;
MyEvents.UserSignIn_Completed += UserSignIn_Completed;

private void SignIn_Click(object src, EventArgs e)
{
    MyEvents.UserSignIn_Async (userId, userPwd);
}

private void UserSignIn_Completed (object sender, MyStuff_EventArgs_Bool e)
{
string result = String.Format ("Sign-in {0} successful", e.Result ? "was" : "was NOT");
    try
    {
        if ( e.Success )
        {
            StartActivity(typeof(nextActivity);
        }
        else
        {
result = String.Format ("Something went wrong [{0}]", e.ErrorMessage);

            Console.WriteLine("Error message=" + e.ErrorMessage);
        }
    }
    catch(Exception err)
    {
Console.WriteLine("UserExists_Completed Exception:" + err.Message);
    }
}

If I've read MSDN correctly, I should be able to transform that into something much simpler...

(in OnCreate)
login.Click += delegate
{
                loginUser(username.Text, password.Text, mobile.Text);
};

private async void loginUser(string username, string password, string mobile)
{
Task<MyStuff_EventArgs_Bool> getLoginTask = MyEvents.UserSignIn_Async(username, password);
      MyStuff_EventArgs_Bool e = await getLoginTask;
      if (e.Success)
      {
           StartActivity(typeof(FrontPage));
      } else
      {
RunOnUiThread(() => GeneralUtils.Alert(context, Resource.String.errLoginTitle, e.ErrorMessage));
           return;
      }
}

The problem though is the UserSignIn_Async returns void and you can't have Task<void>. Changing the return type of the method to Task causes a problem at the caller line

First convention is Async w/o _ (this should not matter)
Async functions can have return types:

 * void
 * Task
 * Task<T>

If the return type is void then new function should return void too. Actually compiler makes this
magic for us if async function returns string or int You can write
    return some_string;
or
    return some_int;
compiler will wrap it into Task<string>, Task<int>. So it should leave
    return;
(or lack of it) as it is or wrap it in non-generic task.

Why don't  You try
    var getLoginTask = MyEvents.UserSignIn_Async(username, password);

and see returning type??


Can anyone point out what I've misinterpreted please as I'm pretty sure the new async code in Xam.Android will be much quicker than the old way and I'd rather be using that.
Much quicker... Resources, cycles get used, optimization is how they do it, some say they do
not create threads (use some from OS, ThreadPool), some say the do.
I'm trying to get under the hood too...

BTW RunOnUIThread is not needed too. I think it gets right Context when code after await (it
used to be callback) returns. Right?
Can You try without it?

night

mel

Paul


--
Miljenko Cvjetko dipl.ing. ET
        Direktor/CEO
        Projektant rjes(enja/Solution Architect 
        Razvojni programer/Senior developer
        Voditelj projekta/Project Manager

IX juz(na obala 13
Kajzerica Zagreb
T: 385 1 7775555
M: 385 91 557 447 3
F: 385 1 7779556
e: [email protected]
w: http://holisticware.net

_______________________________________________
Monodroid mailing list
[email protected]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to