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

  • SEARCH
  • Home
  • 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 7676399
In Process

The Archive Base Latest Questions

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

thanks to this answer I was able to determine if Server is listening on

  • 0

thanks to this answer I was able to determine if Server is listening on a given port or not:

How to configure socket connect timeout

now I’m trying to create an endless loop, which will be loaded on form_load event and will be constantly checking if server is listening.

here is my code:

            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IAsyncResult result = socket.BeginConnect("192.168.0.131", 1095, null, null);

            bool success = result.AsyncWaitHandle.WaitOne(500, true);

            if (!socket.Connected)
            {label3.Text = "can't use"; socket.Close();}
            else
            {label3.Text = "start action";}

If I put following code into “on_button_click” event – everything works fine (except for – I have to click the button every single time I want to refresh status)

and when I create endless loop – I’m not getting any results at all:

while (true)
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IAsyncResult result = socket.BeginConnect("192.168.0.131", 1095, null, null);

            bool success = result.AsyncWaitHandle.WaitOne(500, true);

            if (!socket.Connected)
            {
                label3.Text = "can't use";
                socket.Close();
            }
            else
            {
                //success = true;
                label3.Text = "start action";
                socket.Close();
            }
        }

I guess it has something to do with threading but I just can’t figure it out. What might be the problem?

Edit:

timer tick solution:

private void Form1_Load_1(object sender, EventArgs e)
        {
            System.Windows.Forms.Timer MyTimer = new System.Windows.Forms.Timer();
            MyTimer.Interval = (200);
            MyTimer.Tick += new EventHandler(MyTimer_Tick);
            MyTimer.Start();
        }

        public void MyTimer_Tick(object sender, EventArgs e)
        {

                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IAsyncResult result = socket.BeginConnect("192.168.0.131", 1095, null, null);

                bool success = result.AsyncWaitHandle.WaitOne(500, true);

                if (!socket.Connected)
                {

                    label3.Text = "can't use";
                    socket.Close();
                    //throw new ApplicationException();
                }
                else
                {
                    //success = true;
                    label3.Text = "start action";
                    socket.Close();
                }   

        }
  • 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-31T17:11:28+00:00Added an answer on May 31, 2026 at 5:11 pm

    You can add a Timer object to your form. Start the timer on the form load, and in the timer Tick event, run your code that you have in the infinite loop.

    Note: DO NOT include the while(true) loop, just the code inside the loop. You really don’t want an infinite loop in your GUI thread 🙂

    EDIT
    I still think you may need to consider a redesign of the app behavior, but this should be better than the Timer I suggested earlier

    A somewhat better approach to the Timer (which should take care of the UI lag issue) would be for you to add a BackgroundWorker to your UI. Add a DoWork method similar to this:

    void DoWork(object sender, DoWorkEventArgs e)
    {
        while(!_bw.CancellationPending)
        {
            // Do your socket connection stuff
    
            // you can either update some member variables and call
            // the progresschanged method or you can use a BeginIvoke call
            // to update the labels, you CANNOT update the labels in this method
            if (!_bw.CancellationPending)
            {
                // Checking the cancel pending before sleeping so that we don't sleep
                // while a cancel is pending. There are better ways to do this with
                // event handles, but this should get you off and running.
                Thread.Sleep(1000);
            }
        }
        e.Cancel = true;
    }
    

    Note: If you were using .Net 4.0, you may want to consider using the task factory and a cancellation token rather than the BackgroundWorker, but either one should get you up and running.

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

Sidebar

Related Questions

Thanks to the answer on this stackoverflow question I was able to get the
Thanks to this answer I was able to get javascript to execute when being
Thanks in Advance for reading and answer this question. I got button in asp
UPDATE: Solved. Thanks BusyMark! EDIT: This is revised based on the answer below from
I posted this answer (more of an idea really) but haven't been able to
I think there are people who may be able to answer this, this is
Now that I can make useful user controls in WPF (thanks to this stackoverflow
This post on SO answers most of the questions I have (much thanks to
Thanks for looking, all helpful answers are voted up. This is my markup. I'm
Thanks to this question (click me!) , I have the Source property of my

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.