need a help for multithreading in , .net Tumkūr, India
  • 8 years ago

    Hi all,

       How to use multithreading concept in windows application to run the same method (with different parameters

    ie different sleep time values).

    thanks in advance Raja

  • 8 years ago

    Hi u can refer doc below to understand Threading in VB2005!!!

    Implementing Threading

    At this point you should have a basic understanding of threads and how they relate to the process and AppDomain concepts. You should also realize that for interactive applications, multithreading is not a way to improve performance, but rather is a way to improve the end user experience by providing the illusion that the computer is executing more code simultaneously. In the case of server-side code, multithreading enables higher scalability by allowing Windows to better utilize the CPU along with other subsystems such as IO. A Quick Tour When a background thread is created, it points to a method or procedure that will be executed by the thread. Remember that a thread is just a pointer to the current instruction in a sequence of instructions to be executed. In all cases, the first instruction in this sequence is the start of a method or procedure. When using the BackgroundWorker control, this method is always the control’s DoWork event handler. It is important to realize that this method can’t be a Function. There is no mechanism by which a method running on one thread can return a result directly to code running on another thread. This means that any time you design a background task, you should start by creating a Sub in which you write the code to run on the background thread. Also, because the goals for interactive applications and server programs are different, our designs for implementing threading in these two environments are different. This means that the way we design and code the background task will vary.

    By way of explanation, let’s work with a simple method that calculates prime numbers. This implementation is naïve, and so can take quite a lot of time when run against larger numbers, so it makes for a useful example of a long-running background task. Do the following: 1. Create a new Windows Forms Application project named Threading. 2. Add two Button controls, a ListBox and a ProgressBar control to Form1. 3. Add a BackgroundWorker control to Form1. 4. Set its WorkerReportsProgress and WorkerSupportsCancellation properties to True. 5. Add the following to the form’s code: Public Class Form1

    Region “ Shared data “

    Private mMin As Integer Private mMax As Integer Private mResults As New List(Of Integer)

    End RegionRegion “ Primary thread methods “

    Private Sub btnStartClick(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnStart.Click ProgressBar1.Value = 0 ListBox1.Items.Clear() mMin = 1 mMax = 10000 BackgroundWorker1.RunWorkerAsync() End Sub Private Sub btnCancelClick(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnCancel.Click BackgroundWorker1.CancelAsync() End Sub Private Sub BackgroundWorker1_ProgressChanged( _ ByVal sender As Object, ByVal e As _ System.ComponentModel.ProgressChangedEventArgs) _ Handles BackgroundWorker1.ProgressChanged ProgressBar1.Value = e.ProgressPercentage End Sub

    Threading Private Sub BackgroundWorker1_RunWorkerCompleted( _ ByVal sender As Object, ByVal e As _ System.ComponentModel.RunWorkerCompletedEventArgs) _ Handles BackgroundWorker1.RunWorkerCompleted For Each item As String In mResults ListBox1.Items.Add(item) Next End Sub

    End RegionRegion “ Background thread methods “

    Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, _ ByVal e As System.ComponentModel.DoWorkEventArgs) _ Handles BackgroundWorker1.DoWork mResults.Clear() For count As Integer = mMin To mMax Step 2 Dim isPrime As Boolean = True For x As Integer = 1 To CInt(count / 2) For y As Integer = 1 To x If x * y = count Then ‘ the number is not prime isPrime = False Exit For End If Next ‘ short-circuit the check If Not isPrime Then Exit For Next If isPrime Then mResults.Add(count) End If Me.BackgroundWorker1.ReportProgress( _ CInt((count - mMin) / (mMax - mMin) * 100)) If Me.BackgroundWorker1.CancellationPending Then Exit Sub End If Next End Sub

    End Region

    End Class

    The BackgroundWorker1_DoWork method implements the code to find the prime numbers. This method is automatically run on a background thread by the BackgroundWorker1 control. Notice that the method is a Sub, so it returns no value. Instead, it stores its results into a variable, in this case, a List(Of Integer). The idea is that once the background task is complete, we can do something useful with the results. When btnStart is clicked, the BackgroundWorker control is told to start the background task. In order to initialize any data values before launching the background thread, the mMin and mMax variables are set before the task is started. Of course, we want to display the results of the background task. Fortunately, the BackgroundWorker control raises an event when the task is complete. In this event handler we can safely copy the values from the List(Of Integer) into the ListBox for display to the user. Similarly, the BackgroundWorker control raises an event to indicate progress as the task runs. Notice that the DoWork method periodically calls the ReportProgress method. When this method is called, the progress is transferred from the background thread to the primary thread via the ProgressChanged event. Finally we have the need to cancel a long-running task. It is never wise to directly terminate a background task. Instead, we should send a request to the background task asking it to stop running. This allows the task to cleanly stop running so it can close any resources it might be using and shut down properly. To send the cancel request, call the BackgroundWorker control’s CancelAsync method. This sets the control’s CancellationPending property to True. Notice how this value is periodically checked by the DoWork method, and if it is True, we exit the DoWork method, thus effectively canceling the task. Running the code now demonstrates that the UI remains entirely responsive while the background task is running, and the results are displayed when available. Now that we’ve explored the basics of threading in an interactive application, let’s discuss the various threading options that are at our disposal.

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” - Rick Osborne