Reputation: 73
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?
Upvotes: 1
Views: 302
Reputation: 663
Invoke your UIelement to avoid cross thread error.
this.Invoke(new MethodInvoker(delegate {
// run all your code here
}));
return;
Upvotes: 1