views:

84

answers:

3

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. :D

+1  A: 

Don't use a polling loop. You are tying that thread up in the loop so it never dispatches any events. Without events being dispatched, the UI is non-functional.

  1. Show the Connecting form with ShowDialog
  2. Pass in the dialog into the connect function.
  3. In your connection thread after connection, use code as follows:

    form.Invoke(new ThreadStart(delegate() { form.Close(); }));

Dark Falcon
+1  A: 

You can also use Manual or Autoreset events with timeouts to control the polling intervals and update a progress display ("connecting", "connected", "failed") on each timeout interval until the event is "set"

AutoResetEvent connectEvent = new AutoResetEvent(false);
while (!this.connected)
{
     if (connectEvent.WaitOne()) {
        this.establishingConnectionForm.Show();
     } else {
        this.stilltryingForm.Show();
     }
}

Or something similar.

GrayWizardx
+1  A: 

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.

Hans Passant