It is necessary to execute the methods sequentially in the order they were started, but without stopping the UI. In the example that I made, the operations are performed asynchronously, which leads to incorrect entries in the ListNumber list.
public Form1()
{
InitializeComponent();
ListNumber = new List<string>();
}
List<string> ListNumber { get; set; }
private async void button1_Click(object sender, EventArgs e)
{
textBox1.Text = await Task.Run(() => MessageAsync());
}
private async Task<string> MessageAsync()
{
var concat = "";
await NumberAsync();
foreach (string number in ListNumber)
{
concat += number + ", ";
}
return concat;
}
private async Task NumberAsync()
{
for(int i = 0; i < 30; i++)
{
ListNumber.Add(i.ToString());
await Task.Delay(300);
}
}
If you quickly click on the button, the calling method gives the following result: the result of the program