dotnet compact framework:
Hi, I am a beginer 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. -- Soul
From VerifyConnection() you cannot update the main UI thread as you do in progressBarConnection.Update(). You will have to do a progressBar.Invoke() or this.Invoke(). This will invoke the main UI thread where you will be able to update the progress bar Check out the code below Mark Arteag private void VerifyConnection( WebRequest webRequest = WebRequest.Create("UrlString") webRequest.Timeout = 5000 IAsyncResult result = webRequest.BeginGetResponse(null, null) while (!result.IsCompleted this.Invoke(new EventHandler(UpdateProgress); //Update the U HttpWebResponse response = (HttpWebResponse webRequest.EndGetResponse(result) //... do something her private void UpdateUI(object sender, EventArgs e if (progressBarConnection.Value == progressBarConnection.Maximum progressBarConnection.Value = 0 els progressBarConnection.Value = progressBarConnection.Value + 1 progressBarConnection.Update() //You can also update any other UI objects i.e. label
Don't see what you're looking for? Try a search.
|