0

My Code :

foreach (ListViewItem item in list.Items)
        {
            string url, path, host;
            url = item.SubItems[0].Text; // example "www.google.com/"
            path =  item.SubItems[1].Text; // example = "login/"
            host = (item.SubItems[2].Text); // example = "123.123.123:232";
            bool ping = Request(url, path, host);
            if (ping) {
            item.Subitems[3].Text = "This is good";
            } else {
            item.Subitems[3].Text = "This is bad";
            }
        }

and this is the Request Method

public bool Request(string url, string path, string host)
    {
        HttpRequest httpRequest = new HttpRequest();
        //a lot if code with httpRequest 
        if (result == "good")
            return true;
        else
            return false;
    }

so this code dose not have any error when i put like 20 item in list and run this code its close the application so i have to add Task to make it run without close the app .

so i do this

foreach (ListViewItem item in list.Items)
        {
         await Task.Run(() => {
            string url, path, host;
            url = item.SubItems[0].Text; // example "www.google.com/"
            path =  item.SubItems[1].Text; // example = "login/"
            host = (item.SubItems[2].Text); // example = "123.123.123:232";
            bool ping = Request(url, path, host);
            if (ping) {
            item.Subitems[3].Text = "This is good";
            } else {
            item.Subitems[3].Text = "This is bad";
            }
          }
        }

but its give me this error Current thread must be set to single thread apartment (STA)

Meslzy
  • 104
  • 1
  • 9
  • Does this answer your question? [C# WinForms: How to set Main function STAThreadAttribute](https://stackoverflow.com/questions/6373645/c-sharp-winforms-how-to-set-main-function-stathreadattribute) – Circle Hsiao May 05 '20 at 09:26
  • 1
    Sorry, but why do you need to add a Task in order to prevent application exit? – Peter Csala May 05 '20 at 09:27

2 Answers2

0

You can't access UI elements from a background thread (i.e., code running in a Task.Run).

foreach (ListViewItem item in list.Items)
{
  string url, path, host;
  url = item.SubItems[0].Text; // example "www.google.com/"
  path =  item.SubItems[1].Text; // example = "login/"
  host = (item.SubItems[2].Text); // example = "123.123.123:232";

  var result = await Task.Run(() => {
    bool ping = Request(url, path, host);
    if (ping) {
      return "This is good";
    } else {
      return "This is bad";
    }
  });
  item.Subitems[3].Text = result;
}
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • after i look to my code i found i use Clipboard.setText() . and i remove it but still the give me error so i use your code and its work thanks (: – Meslzy May 06 '20 at 02:10
0

You can't change UI Content, when you're not calling it from the UI Thread. In order to do so from another Thread you need to Invoke your method from the Forms dispatcher like this:

this.Invoke(new MethodInvoker(() => 
{ 
      if (ping) {
         item.Subitems[3].Text = "This is good";
      } else {
         item.Subitems[3].Text = "This is bad";
      }
}));

//**this** is the form you want to update

Also check this Question which covers the problem too: Invoke in Windows Forms

VollRahm
  • 397
  • 1
  • 16