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 496311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:40:27+00:00 2026-05-13T05:40:27+00:00

I have a program that needs to connect to a server to gather some

  • 0

I have a program that needs to connect to a server to gather some data. I start a new thread and have it perform the connection sequence. In this sequence it will continue to try to connect until it successfully does so.

The code is as follows for the connect sequence:

// Code for InitializeConnection
// Our address to our Authentication Server
IPEndPoint authenServerEP = new IPEndPoint(IPAddress.Parse("192.168.1.100"), 8007);

// Connect to the Authentication server
while (!this.connected)
{
    try
    {
        this.sock.Connect(authenServerEP);
        this.connected = true;
    }
    catch (SocketException retryConnectException)
    {
        if (false == retried)
        {
             retried = true;
        }
    }
}  

After I start the thread, in the parent/main thread I go on to loop and check a variable declared in my main form/dialog on whether or not it is connected.

The code for the main thread is as follows:

// Connect to the Authentication Server                             
new Thread(InitializeConnection).Start();
// Loop till connected
while (!this.connected)
{
     if ((true == this.retried) && (false == this.establishingConnectionForm.Visible))
     {
        this.establishingConnectionForm.Show();
     }
}
this.establishingConnectionForm.Dispose();

If in the InitilizeConnection code it retries connecting because it failed the first time I would like it to show a new dialog/form that lets the user know it is retrying to connect. When I use the .Show() method on establishingConnectionForm (in the parent thread) it brings up the dialog/form but it doesn’t display properly. Some of the tools on the form are shaded out white and the mouse icon turns into the thinking/doing work icon and doesn’t let you click on the form and interact with it.

What I imagined is that I would be able to show the form and interact with it (IE close it by ‘X’ing it in the top right corner) and move it around and stuff. When the connection was established I would break out of my while(!this.connected) loop in the main/parent thread and then close/dispose of the establishingConnectionForm.

I have tried creating/initializing the form on the thread that does the connecting sequence but I still get the same result with tools on the form shaded out and not being able to interact with it. Using the .ShowDialog() method works in terms of making it display correctly and lets me interact with it, but I don’t want to block as I don’t have a DialogResult. I want the form to close by itself once a connection is established.

I appreciate you reading my thread and any input you have. 😀

  • 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-13T05:40:27+00:00Added an answer on May 13, 2026 at 5:40 am

    You cannot loop like this in the UI thread, it will not be able to perform basic duties like paint windows and respond to input. Don’t use DoEvents(), that will leave your code running without any user interface when the user closes the main form. You’ll get an ObjectDisposedException if you’re lucky.

    The solution is to let the thread notify the UI thread that it is having trouble making the connection. You can cleanly do so with a BackgroundWorker’s ReportProgress event.

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

Sidebar

Related Questions

I have a program that needs to... In Activity A , do some jobs
I have a Flash program that needs to make url requests to send data
I'm writing a chat program in Python that needs to connect to a server
I have a job that needs to connect to two fileshares and copy some
I have a program that needs several third-party libraries, and at the moment it
I have a program that needs a lot of memory and want to set
In an embedded program I have a screen object that needs to manage a
I have a program that needs to sit in the background and when a
I have a simple program, which needs to make sure I can connect to
I have 2 java web apps that need to connect to SQL Server Database

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.