0

The problem is that UI hanging. Below the code.

private void Button1_Click(object sender, EventArgs e)   
{
    Thread childThread = new Thread(getlistAsync);
    childThread.Start();
}

private void getlistAsync()
{
    while (true)
    {
        add("a", "b", "c", "d");
    }
}

public async Task add(string prob, string reg, string data, string user) //async task
{
    String[] row = { prob, reg, data, user };

    ListViewItem item = new ListViewItem(row);

    this.listView1.BeginInvoke(
    new Action(() =>
    {
        listView1.Items.Add(item);
        listView1.Refresh();
    }));
}

private void Form1_Shown(object sender, EventArgs e)  //on shown
{
    listView1.Columns.Add("Problems", 80);
    listView1.Columns.Add("Data", 120);
    listView1.Columns.Add("Registry Key", 130);
    listView1.Columns.Add("users", 80);
}
Max
  • 6,821
  • 3
  • 43
  • 59
Alekperov
  • 1
  • 1
  • 1
  • 5
  • 2
    Could it be the `while (true)`? – A Friend Sep 23 '19 at 09:05
  • Your `while` loop needs to give the UI time to response to your add method. You are constantly without any break calling your add method. So when does the UI have time to breath and response to anything else? – Rand Random Sep 23 '19 at 09:09
  • See this related [post](https://stackoverflow.com/questions/14455293/how-and-when-to-use-async-and-await?rq=1) – Max Sep 23 '19 at 09:28
  • In this post I cannot find anything for me. I know it`s easy. But how? Tell me please. Is it impossible do async threads with listview in Visual C#? – Alekperov Sep 24 '19 at 06:00
  • In this post I cannot find anything for me. I know it`s easy. But how? Tell me please. Is it impossible do async threads with listview in Visual C#? All examples in microsoft with consol or with progressbar. In this examples not need for synchronization. Nothing with listview or richtext. – Alekperov Sep 24 '19 at 06:07
  • I did it.Thank you all. – Alekperov Sep 24 '19 at 07:12

0 Answers0