Hey!

I have a question.
I have this code:

int size = Convert.ToInt32(numericUpDown1.Value) + 1;
            var item_c = listView1.Items.Count;

            int id = 0;
            while (true)
            {

                if (id == item_c)
                {
                    break;
                }

                for(int i = 0; i < size; i++) 
                {
                    if (id == item_c)
                    {
                        break;
                    }

                    Thread[] array = new Thread[size];

                    for (int k = 0; k < size; k++)
                    {
                        if (id == item_c)
                        {
                            break;
                        }

                        ParameterizedThreadStart start = new ParameterizedThreadStart(DoChecking);
                        array[k] = new Thread(start);
                        array[k].Start(id);
                        id++;
                    }
                }
            }

It works well.. But only problem is that when I run it then it crashes.. BUT if I add messagebox to display "id" below "id++" then it works perfectly and it doesn't crash.

Have you any idea how to fix it ?

What error do you get when it crashes? And what is the DoChecking method?

I don't get any errors, it just freezes. Also DoChecking checks if that thing I'm searching exists or not and updates listview fields.

I suspect that DoChecking isn't thread safe. Can you post the code?

It looks like this:

public void DoChecking(object iid)
        {
            var lid = Convert.ToInt32(iid);

            string[] urls = url_list;            

            if (CheckExist(urls[lid]))
            {
                success++;
                ShowSuccessful();
                UpdateColumn("", lid, 1);
            }
            else
            {
                failed++;
                ShowFailed();
                UpdateColumn("", lid, 0);
            }

            var total = failed + success;
            UpdateTotalCounter(total);

        }

Since this also calls CheckExist, ShowSuccessful, UpdateColumn, ShowFailed, and UpdateTotalCounter we'll need to see those too. Also where is url_list set?

url_list:

private void LoadLinks(string path)
        {
            listView1.Clear();
            listView1.Columns.Add("Link", 603, HorizontalAlignment.Left);
            listView1.Columns.Add("Status", 75, HorizontalAlignment.Left);
            listView1.Columns.Add("Anchor", 168, HorizontalAlignment.Left); 

            StreamReader read = new StreamReader(path);
            var items = read.ReadToEnd();

            string[] item = items.Split('\n');
            url_list = item;

            foreach (string i in item)
            {
                if (i != "")
                {
                    listView1.Items.Add(i);
                }
            }

            UpdateTotalCounter();
        }

UpdateTotalCounter:

private void UpdateTotalCounter(int trace = 0)
        {

            var itemscount = listView1.Items.Count;

            if (itemscount == 0)
            {
                toolStripStatusLabel1.Text = "Checked links: 0 out of 0";
            }
            else
            {
                toolStripStatusLabel1.Text = "Checked links: " + trace + " out of " + listView1.Items.Count;
            }

        }

CheckExist:

public bool CheckExist(string item)
        {
            
            if (item != null)
            {
                try
                {
                    HttpWebRequest WebRequestObject = (HttpWebRequest)HttpWebRequest.Create(item);
                    WebRequestObject.Timeout = Convert.ToInt32(numericUpDown2.Value);
                    try
                    {
                        WebResponse Response = WebRequestObject.GetResponse();

                        Stream WebStream = Response.GetResponseStream();
                        StreamReader Reader = new StreamReader(WebStream);
                        string PageContent = Reader.ReadToEnd();
                        Reader.Close();
                        WebStream.Close();
                        Response.Close();

                        var regex = "<a?(.*?)href=\"" + pattern + "\"?(.*?)>(.*?)</a>";

                        Match match = Regex.Match(PageContent, @regex, RegexOptions.IgnoreCase);
                        
                        if (match.Success)
                        {
                            var a = match.Groups[3].Value;
                            Match am = Regex.Match(a, @"(\<(/?[^\>]+)\>)?(?=(.*))", RegexOptions.IgnoreCase);
                            anchor = am.Groups[3].Value;
                            
                            status = true;
                        }
                        else
                        {
                            status = false;
                        }

                    }
                    catch
                    {
                        status = false;
                    }

                }
                catch
                {
                    status = false;
                }

            }

            return status;           
        }

Show failed and successful:

public void ShowSuccessful()
        {
            if (toolStripStatusLabel2.Text == "")
            {
                toolStripStatusLabel2.Text = "Success: 0";
            }
            else
            {
                toolStripStatusLabel2.Text = "Success: " + success.ToString();
            }
        }

        public void ShowFailed()
        {
            if (toolStripStatusLabel3.Text == "")
            {
                toolStripStatusLabel3.Text = "Failed: 0";
            }
            else
            {
                toolStripStatusLabel3.Text = "Failed: " + failed.ToString();
            }
        }

None of these are thread safe. You get/set values in various objects without considering that multiple threads might be trying to do the same thing. Start with this article and see what you can do.

Hm, okay I locked everything down, but again.. 4 links work 1000 links doesnt work..

Have you thought about what is going on when you start 1000 threads? You might want to limit the number of running threads.

Well, I have limited threads to 100.

So, can somebody help me ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.