1

I am new in C# and I'm getting an error System.InvalidOperationException with my code that calls API I created with PHP. My goal is to create a loading animation before the API is being loaded and hide it after.

loading.Visible = true;
login.Enabled = false;
var Client = new RestClient("http://localhost/online-lms/login.php");
var request = new RestRequest(Method.POST);
Client.ExecuteAsync<Form1>(request, (response) => {
    loading.Width = 200;
    login.Enabled = true;
    if (response.StatusCode == System.Net.HttpStatusCode.OK) {
        JObject o = JObject.Parse(response.Content);
    }
});

Error:

System.InvalidOperationException: 'Cross-thread operation not valid: Control 'loading' accessed from a thread other than the thread it was created on.'

PS: I'm creating a Windows Form Application.

Any help with this?

4
  • Are you trying to create WPF application or windows forms application? Commented Mar 26, 2017 at 16:18
  • I am creating a Windows Form Application @ThiyaguRajendran Commented Mar 26, 2017 at 16:19
  • Its simple invoke your UIelement when you are trying to acces from different thread.. I will post an example now Commented Mar 26, 2017 at 16:20
  • You need to update UI using a delegate (callback) Commented Mar 26, 2017 at 16:21

1 Answer 1

1

Invoke your UIelement to avoid cross thread error.

        this.Invoke(new MethodInvoker(delegate {
            // run all your code here
        }));
        return;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.