March 10, 2003

How To Pass Parameters to Threads in Windows Forms Applications—and Get Results

he .NET framework makes it easy to start code execution on a new thread?improving user interface design by letting you execute lengthy procedures on one thread while leaving the UI thread active and available to handle user interaction. Starting a new thread is simple in VB.NET?you add the System.Threading namespace

GetSystemUpTime – Retrieving the system’s up time

‘ Returns a TimeSpan for the interval of time the system has been up.’ EXAMPLE:’ Dim ts As TimeSpan = GetSystemUpTime()’ Console.WriteLine(String.Format(“The system’s up time is: {0} days, {1} ‘ hours, {2} minutes and {3} seconds.”, ts.Days, ts.Hours, ts.Minutes, ‘ ts.Seconds))Function GetSystemUpTime() As TimeSpan Dim pc As PerformanceCounter = New

DecodeBase64 – Decoding a string from base64

‘ Returns the input string decoded from base64Private Function DecodeBase64(ByVal input As String) As String Dim strBytes() As Byte = System.Convert.FromBase64String(input) Return System.Text.Encoding.UTF8.GetString(strBytes)End Function

GetUrlParameters – Retrieving the key-value pairs from the specified url

‘ Returns a hashtable with the key-value pairs extracted from the querystring ‘ of the specified url’ EXAMPLE:’ Dim ht As Hashtable = GetUrlParameters’ (“http://www.mysite.com?param1=123&param2=&param3=234”)’ ‘ print the key=value pairs to the console window’ Dim myEnumerator As IDictionaryEnumerator = ht.GetEnumerator()’ While myEnumerator.MoveNext()’ Console.WriteLine(“{0}: {1}”, myEnumerator.Key, myEnumerator.Value)’ End While’ ‘ print

EncodeBase64 – Encoding a string to base64

‘ Returns the input string encoded to base64Private Function EncodeBase64(ByVal input As String) As String Dim strBytes() As Byte = System.Text.Encoding.UTF8.GetBytes(input) Return System.Convert.ToBase64String(strBytes)End Function

DownloadFile – Downloading and saving the file at the specified URI

‘ Downloads the file at the specified URI, and saves it where specifiedSub DownloadFile(ByVal uri As String, ByVal destFile As String, _ Optional ByVal username As String = Nothing, Optional ByVal pwd As String = _ Nothing) Dim wc As New System.Net.WebClient() If Not username Is Nothing AndAlso Not pwd