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