Search code examples
.netwcfwindows-phone-8

InvalidOperationException when do a process twice


I had to write an async method to contact with web service. This is my method in WebServiceHelper class:

 public static Task<int> SignIn(string username, string password) 
    {

        try
        {
            TaskCompletionSource<int> tcs = new TaskCompletionSource<int>();
            service.LoginCompleted += (object sender, WebService.LoginCompletedEventArgs e) => 
            {
                if (e.Error != null) tcs.SetResult(-1);
                else
                    tcs.SetResult((int)e.Result);

            };
            service.LoginAsync(username, password);
            return tcs.Task;
        }
        catch (Exception ex)
        {

            MessageBox.Show("Error: " + ex.Message);
            return null;
        }

    }

Then I call it in a button clicked event like this:

private async void btLogIn_Click(object sender, RoutedEventArgs e)
    {                       
        try
        {
            int si = await WebServiceHelper .SignIn(tbUsername.Text, tbPassword.Text);               
            if (si != 0) MessageBox.Show("Signed in successfully!");
            else MessageBox.Show("Couldn't sign in");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }            
    }

It worked fine on the first time I clicked the button, but when I did in again, an error came up: "InvalidOperationException, An attempt was made to transition a task to a final state when it had already completed."

I did some little search and found something here: Tasks seem to start automatically

I understand that I should do something to stop the process before start it again but I don't know exactly how and why. Can someone please explain this for me?


Solution

  • I tried to use the method TrySetResult() instead of SetResult() and it worked!