webclient Asyncdownloadfile...

cugone

Member
Joined
Apr 30, 2008
Messages
19
...works fine for my purposes for checking for a new file version, but it still locks up the UI when it is connecting to the server. Once it connects, the UI unlocks but Id like to eliminate the brief 3 second lag it causes.
 
The user clicks on a menuitem then this code executes:

Code:
     <summary>
     Gets the update information.
     </summary>
     <remarks></remarks>
    Friend Sub GetUpdate()

        If My.Computer.FileSystem.FileExists(MainForm.Path & "newVersion.txt") Then
            My.Computer.FileSystem.DeleteFile(MainForm.Path & "newVersion.txt")
        End If

        Dim baseUri As New Uri("http://www.blisspointranch.com/")
        Dim basePath As New Uri(baseUri, "programs/charactersheet/")
        Dim filePath As New Uri(basePath, "version.txt")

        Disable menu item.
        MainForm.menuitemUpdate.Text = "Checking for Updates..."
        MainForm.menuitemUpdate.Enabled = False

        Try to download file. If cant: Display returned server error, re-enable menus, exit subroutine.
        Try
            MainForm.webclient.DownloadFileAsync(filePath, MainForm.Path & "newVersion.txt")
        Catch ex As UriFormatException
            MessageBox.Show("Could not check for new version, server reported an error: " & vbCrLf & _
                                       ex.Message & vbCrLf & _
                                       "If this problem persists contact the program vender for support.", _
                                       My.Application.Info.Title, _
                                       MessageBoxButtons.OK, _
                                       MessageBoxIcon.Error)
        Catch ex As WebException
            MessageBox.Show("Could not check for new version, server reported an error: " & vbCrLf & _
                                      ex.Message & vbCrLf & _
                                      "If this problem persists contact the program vender for support.", _
                                       My.Application.Info.Title, _
                                       MessageBoxButtons.OK, _
                                       MessageBoxIcon.Error)
        Catch ex As InvalidOperationException
            MessageBox.Show("Could not check for new version, server reported an error: " & vbCrLf & _
                                      ex.Message & vbCrLf & _
                                     "If this problem persists contact the program vender for support.", _
                                     My.Application.Info.Title, _
                                     MessageBoxButtons.OK, _
                                     MessageBoxIcon.Error)
        Finally
            MainForm.menuitemUpdate.Text = "Check for Updates..."
            MainForm.menuitemUpdate.Enabled = True
        End Try

    End Sub
 
You may try something ~ like this: [youll need to use threads]

Code:
using System.Threading;
// add top
public delegate void CheckForUpdateCallback();
private Thread updateChecker;

// menu_click
updateChecker = new System.Threading.Thread(GetUpdate);
updateChecker.Start();

// add to end of GetUpdate
this.Invoke(new CheckForUpdateCallback(Finished), new object[] {  });

// create new method for "finished"
private void Finished()
{
MessageBox.Show("Finished - " + DateTime.Now.ToString());
}


This should cause the entire operation to run Asnyc from the UI.
 
Back
Top