Verify Connetion when Form Loaded

  • Thread starter Thread starter Soul
  • Start date Start date
S

Soul

Hi,

I am a beginner learning how to develop application for smart device. I want
my application to verify network connection when application startup. When
verifying network connection, I display status using ProgressBar. I
understand that I will need to run the verify network procedure on a new
thread so that the Form interface can completely load.

Below is my code snippet:

private Thread threadVerifyConnection;

public FormMain()
{
InitializeComponent();
threadVerifyConnection = new Thread(new
ThreadStart(this.VerifyConnection));
threadVerifyConnection.Start();
}

private void VerifyConnection()
{
WebRequest webRequest = WebRequest.Create("UrlString");
webRequest.Timeout = 5000;
IAsyncResult result = webRequest.BeginGetResponse(null, null);

while (!result.IsCompleted)
{
if (progressBarConnection.Value == progressBarConnection.Maximum)
progressBarConnection.Value = 0;
else
progressBarConnection.Value = progressBarConnection.Value + 1;

progressBarConnection.Update();
}

HttpWebResponse response = (HttpWebResponse)
webRequest.EndGetResponse(result);

//... do something here
}

But I think I must be understand something wrongly as when I run my
application onto the Pocket PC 2002 Emulator, nothing come out... not even
the Form!! But I can see from the emulator's memory that the application is
running but not responding.

Can someone enlighten me?

Thank you.
 
Hello Soul

There is nothing intuitively wrong with your code. However, try creating and starting the verifying thread from the form's Load event or the OnLoad method instead of within the constructor.

All the best1
 
Back
Top