Microsoft Technologies based on the .NET software framework. Miscellaneous topics that do not fit into specific categories.
Hi @Micheal Pepper , Welcome to Microsoft Q&A,
Directly using task.Result to synchronously wait for asynchronous operations may cause deadlock or block the UI thread.
For async operations you need to use await correctly.
For example, use: String id2 = await CreateDraftA();
Your solution is to use: String id2 = Task.Run(async () => await CreateDraftA()).Result;
Updated:
A simple async method shows:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Start the asynchronous initialization
InitializeAsync();
}
private async void InitializeAsync()
{
String id1 = CreateDraftB();
MessageBox.Show("CreateDraftB - id1: " + id1);
String id2 = await CreateDraftA();
MessageBox.Show("CreateDraftA - id2: " + id2);
}
// Rest of your form code...
// Your other methods here...
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.