Skip to main content

Microsoft Silverlight

Unanswered Question multi threadingRSS Feed

(0)

mexican
mexican

Member

Member

164 points

713 Posts

multi threading

Hi,

I am looking at understanding multi threading and I am not sure what method to use in SL 3.

I want the backgroundworker and dont want the UI thread . I believe you need to specify which you want.

http://www.silverlight.net/learn/videos/all/using-multiple-threads-with-the-backgroundworker/

or

http://msdn.microsoft.com/en-us/library/cc221403(VS.95,classic).aspx

or something else. I am after a simple example to learn on

Pravinkumar R. D.
Pravinku...

Contributor

Contributor

4300 points

708 Posts

Re: multi threading

Hi Mexican,

I have a simple demo of BackgroundWorker. Check if this helps you understand what you are looking for-

http://pkrd.blogspot.com/2009/05/working-with-background-worker-in.html

Thanks,

Pravin

"Please mark as answered, if this answers your question"

mexican
mexican

Member

Member

164 points

713 Posts

Re: multi threading

Hi,

 I have this working with background thread.

 What I need to know is how to run 2 threads that do this for example as i can do 1 thread in the background ok. How can i add 2 threads to do the same thing?

 

    Dim bw As BackgroundWorker = New BackgroundWorker
...

        AddHandler bt1.Click, AddressOf bt1_click
        AddHandler bt2.Click, AddressOf bt2_click
        bw.WorkerReportsProgress = True
        bw.WorkerSupportsCancellation = True
        AddHandler bw.DoWork, AddressOf bw_DoWork
        AddHandler bw.ProgressChanged, AddressOf bw_ProgressChanged
        AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted


    End Sub

    Private Sub bt1_click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        bw.RunWorkerAsync()

    End Sub

    Private Sub bt2_click(ByVal sender As Object, ByVal e As RoutedEventArgs)


    End Sub
    Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)

        For i = 1 To 10
            System.Threading.Thread.Sleep(500)
            bw.ReportProgress(i * 10)
            a.X = i
            a.Y = i
            e.Result = "l"
        Next

    End Sub

    Private Sub bw_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)

        If e.Cancelled = True Then
            Me.tb1.Text = "Canceled!"
        ElseIf e.Error IsNot Nothing Then
            Me.tb2.Text = "Error: " & e.Error.Message
        Else
            Me.tb3.Text = "Done!" & e.Result & a.X
        End If
    End Sub
    Private Sub bw_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
        Me.tb1.Text = e.ProgressPercentage.ToString() & "%"
    End Sub
 

Pravinkumar R. D.
Pravinku...

Contributor

Contributor

4300 points

708 Posts

Re: multi threading

Hi Mexican,

Go through this article -

 http://msdn.microsoft.com/en-us/library/6kac2kdh(VS.95).aspx

And find the example of using multiple threads here-

http://msdn.microsoft.com/en-us/library/6kac2kdh(VS.95).aspx

Let me know if this helps you out-

Thanks,

Pravin

"Please mark as answered, if this answers your question"

mexican
mexican

Member

Member

164 points

713 Posts

Re: multi threading

Hi,

I read the webpage and found this as well.

I checked out http://www.silverlighthack.com/post/2008/09/07/Silverlight-MultiThreading-with-a-Computational-Process-(Counting-Primes).aspx

This does a simple sync of threads  and claims you need to use a wait handle. this seems outdated but i can understand it.

What is the wait handle they talk about and how can the threads be in sync better .

I ask because this is what i am after

Pravinkumar R. D.
Pravinku...

Contributor

Contributor

4300 points

708 Posts

Re: multi threading

Hi Mexican,

  1. // Poor way to do a waithandle. Don't do this in production code.   
  2.     // Use the waithandle...that's what it's there for.  I am keeping this simple.    
  3.     // until Silverlight 2 gets full async support   

This is shown in the code from the link which you have shared. As far as I know, if your main thread is finished, and your child thread are still running, it keeps your main thread alive till the time all the threads have not finished the work. I might be wrong.

Thanks,

Pravin

"Please mark as answered, if this answers your question"

mexican
mexican

Member

Member

164 points

713 Posts

Re: multi threading

  1. // Poor way to do a waithandle. Don't do this in production code.   
  2.     // Use the waithandle...that's what it's there for.  I am keeping this simple.    
  3.     // until Silverlight 2 gets full async support 

 how is this done is what i am asking. how do i code this?

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities