1

I am using threadpool method to do multi threading , in the same time the UI hanging do not know why , is there any solution for that. here is the snippet code:

private void CommentThread(object obj)
{
    CancellationToken token = (CancellationToken)obj;

    RandomNumber = new Random();

    // ListViewItemCollection URLCOllection;
    if (UrlListview.InvokeRequired)
    {

        UrlListview.Invoke(new MethodInvoker(delegate
        {
           // URLCOllection = UrlListview.Items;
        foreach (ListViewItem item in UrlListview.Items)
            {
                MessageBox.Show(item.Text);
                Thread.Sleep(3000);
            }
            /*
            foreach (ListViewItem url in URLCOllection)
            {
                if (token.IsCancellationRequested)
                {
                    cts.Cancel();
                    return;

                }
                MessageBox.Show(url.Text);
                int delayamount = RandomNumber.Next(int.Parse(CommentsMinDelayNumric.Value.ToString()), int.Parse(CommentsMaxDelayNumric.Value.ToString()));
                Thread.Sleep(delayamount*1000);
            }



    }
        */


        }));
    }

    else
    {

        URLCOllection = UrlListview.Items;
    }
        foreach (string url in URLCOllection)
        {
            if (token.IsCancellationRequested)
            {
                cts.Cancel();
                return;

            }
            MessageBox.Show(url);
            int delayAmount = RandomNumber.Next(int.Parse(CommentsMinDelayNumric.Value.ToString()), int.Parse(CommentsMaxDelayNumric.Value.ToString()));
            Thread.Sleep(delayAmount * 1000);


        }





}
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
Maven
  • 13
  • 1

0 Answers0