the task.Result stall the main thread until complete. you should use fire and forget.
private void btnTest_Click(object sender, EventArgs e)
{
// Usage example:
string template = File.ReadAllText("..\\..\\template.xml");
string filledTemplate = ScadaClient.ReplacePlaceholder(template, "$$$MyValue$$$", "ActualValue");
Task.Run(async () =>
{
// call service
var (bool responseState, string responseData, int statusCode) = await ScadaClient.PostXmlToScadaAsync("https://48ba6c60-1fcc-48d6-81be-b2a3ac734d79.mock.pstmn.io/PostTestRequest", filledTemplate, "your_auth_token"));
if (responseState)
{
// Process successful response
if (statusCode == 200)
{
Trace.WriteLine("Success: " + responseData);
}
else
{
Trace.WriteLine("Unexpected success status code: " + statusCode);
}
}
else
{
// Handle error
if (statusCode == 400)
{
Trace.WriteLine("Bad Request: " + responseData);
}
else if (statusCode == 500)
{
Trace.WriteLine("Internal Server Error: " + responseData);
}
else
{
Trace.WriteLine("Unhandled error status code: " + statusCode);
}
}
});
}
note: because you are in a thread, the code inside the Task.Run can not update the UI, unless it marshals to the main thread via the controls .Invoke() method. see docs.