Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6788849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:32:02+00:00 2026-05-26T17:32:02+00:00

I have a thread that returns a site’s http response status, but sometimes my

  • 0

I have a thread that returns a site’s http response status, but sometimes my program returns false results. and after a while it gives good results.
False result:
it takes a big a mount of time to check, and then it says that (for example) Google is down, which is quite not reasonable, but after a few seconds it returns good results

Can you take a look and tell me whats wrong? or how I can I improve it?
Checks all sites in datagrid:

 private void CheckSites()
        {
            if (CheckSelected())
            {
                int rowCount = dataGrid.BindingContext[dataGrid.DataSource, dataGrid.DataMember].Count;
                string url;
                for (int i = 0; i < rowCount; i++)
                {
                    url = dataGrid.Rows[i].Cells[2].Value.ToString();
                    if (url != null)
                    {
                        Task<string[]> task = Task.Factory.StartNew<string[]>
                         (() => checkSite(url));

                        // We can do other work here and it will execute in parallel:
                        //Loading...

                        // When we need the task's return value, we query its Result property:
                        // If it's still executing, the current thread will now block (wait)
                        // until the task finishes:
                        string[] result = task.Result;
                        selectRows();
                        if (result[0] != System.Net.HttpStatusCode.OK.ToString() && result[0] != System.Net.HttpStatusCode.Found.ToString() && result[0] != System.Net.HttpStatusCode.MovedPermanently.ToString())
                        {
                            //bad
                            notifyIcon1.ShowBalloonTip(5000, "Site Down", dataGrid.Rows[i].Cells[2].Value.ToString() + ", has a status code of:" + result, ToolTipIcon.Error);
                            dataGrid.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.Wheat;
                            TimeSpan ts;
                            TimeSpan timeTaken = TimeSpan.Parse(result[1]);
                            dataGrid.Rows[i].Cells[3].Value = result[0];
                            dataGrid.Rows[i].Cells[3].Style.BackColor = System.Drawing.Color.Red;
                            dataGrid.Rows[i].Cells[4].Value = timeTaken.Seconds.ToString() + "." + String.Format("{0:0.00000}", timeTaken.Milliseconds.ToString()) + " seconds.";
                            string sec = (DateTime.Now.Second < 10) ? "0" + DateTime.Now.Second.ToString() : DateTime.Now.Second.ToString();
                            string min = (DateTime.Now.Minute < 10) ? "0" + DateTime.Now.Minute.ToString() : DateTime.Now.Minute.ToString();
                            string hour = (DateTime.Now.Hour < 10) ? "0" + DateTime.Now.Hour.ToString() : DateTime.Now.Hour.ToString();
                            dataGrid.Rows[i].Cells[5].Value = hour + ":" + min + ":" + sec;

                            //loadbar
                        }
                        else if (result[0] == "catch")//catch
                        {
                            notifyIcon1.ShowBalloonTip(10000, "SITE DOWN", dataGrid.Rows[i].Cells[1].Value.ToString() + ", Error:" +result[1], ToolTipIcon.Error);
                            dataGrid.Rows[i].Cells[3].Value = result[1];
                            dataGrid.Rows[i].Cells[3].Style.BackColor = System.Drawing.Color.Red;
                            //loadbar

                        }
                        else
                        {
                            //good
                            TimeSpan timeTaken = TimeSpan.Parse(result[1]);
                            dataGrid.Rows[i].Cells[3].Value = result[0];
                            dataGrid.Rows[i].Cells[3].Style.BackColor = System.Drawing.Color.LightGreen;
                            dataGrid.Rows[i].Cells[4].Value = timeTaken.Seconds.ToString() + "." + String.Format("{0:0.00000}", timeTaken.Milliseconds.ToString()) + " seconds.";
                            string sec = (DateTime.Now.Second < 10) ? "0" + DateTime.Now.Second.ToString() : DateTime.Now.Second.ToString();
                            string min = (DateTime.Now.Minute < 10) ? "0" + DateTime.Now.Minute.ToString() : DateTime.Now.Minute.ToString();
                            string hour = (DateTime.Now.Hour < 10) ? "0" + DateTime.Now.Hour.ToString() : DateTime.Now.Hour.ToString();
                            dataGrid.Rows[i].Cells[5].Value = hour + ":" + min + ":" + sec;
                            //loadbar
                        }
                        selectRows();
                    }
                }
            }
        }

Checks a site:

  /////////////////////////////////
        ////Check datagrid websites-button - returns response
        /////////////////////////////////
        private string[] checkSite(string url)
        {
            string[] response = new string[2];
            url = dataGrid.Rows[0].Cells[2].Value.ToString();
            if (url != null)
            {
                try
                {
                    HttpWebRequest httpReq;


                    httpReq.Timeout = 10000;
                    //loadbar
                    dataGrid.Rows[0].DefaultCellStyle.BackColor = System.Drawing.Color.Wheat;
                    System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
                    timer.Start();
                    HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse(); //httpRes.Close();
                    timer.Stop();
                    //loadbar
                    HttpStatusCode httpStatus = httpRes.StatusCode;
                    response[0] = httpStatus.ToString();
                    response[1] = timer.Elapsed.ToString();//*
                    httpRes.Close();
                    return response;
                }
                catch (Exception he)
                {
                    response[0] = "catch";
                    response[1] = he.Message;
                    return response;
                }
            }
            response[0] = "catch";
            response[1] = "No URL entered";
            return response;
            //dataGrid.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.Blue;

        }

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:32:03+00:00Added an answer on May 26, 2026 at 5:32 pm

    Assuming the code provided is the actual code used:

    First of all, your definition of ‘False result’ and ‘Good result’ is wrong. If you expect A but get B, that doesn’t mean B is invalid. If your wife is giving birth and you expect a boy but it turns out the be a girl, its not a false result. Just unexpected.

    That said: lets analyze your work: If it takes a long long time to check a site only to finally get a ??? result which isn’t a 200 response code. We can almost savely assume you are dealing with a timeout. If your router, google or any fundamental network device in between is having problems, its expected to get an unexpected answer. “Timeout”, “Bad Request”, “Server not available” etc. Why would this happen? Its impossible to say for certain without having direct access to your environment.

    Looking at your code however, i see that you’re using the default TaskScheduler for making each check run as a task in the background (assuming you havent changed the default task scheduler which would be a vey bad practice to begin with). The default task scheduler, schedules each task on the threadpool which results in many many tasks running simultanious. Here we have a good candidate for overloading your network. Many sites (esspecially google) are kinda sensitive for handling many requests from the same source (esspecially if the frequency is high) so maybe google is blocking you temporarily or holding you back. Again, at this point it’s pure speculation but the fact that you’re running all checks simultaniously (unless the thread pool is on his max) is very likely the cause of your problem.

    UPDATE

    I would recommend working with a LimitedConcurrencyTaskScheduler ( see here: http://blogs.msdn.com/b/pfxteam/archive/2010/04/09/9990424.aspx ). Here you can limit the amount of tasks that can be run asynchronously. You have to do some testing for what number works ideally in your situation. Also make sure that the frequency is not ‘too’ high. Its hard to define what is too high, only testing can proof that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one thread that writes results into a Queue. In another thread (GUI),
I have a thread that downloads some images from internet using different proxies. Sometimes
In my C# program, I have a thread that represents a running test, which
I have a thread that sometimes freezes (I suspect) due to a DLL call
I have a thread that, when its function exits its loop (the exit is
I have a thread that needs to be executed every 10 seconds. This thread
I have a thread that appends rows to self.output and a loop that runs
I have a thread that does the following: 1) Do some work 2) Wait
I have one thread that is receiving data over a socket like this: while
I currently have a thread that I created using CreateRemoteThread(). Everything works great. Upon

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.