Click here to Skip to main content
15,937,779 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i have created 10 Backgroundworkers in code, and i assign them the same DoWork, ProgressChanged and RunWorkerCompleted like this

VB
 Private bwkWorkers As New List(Of BackgroundWorker)
'Star by creating the maximum number of worker threads.
private sub startworking()
        For index As Integer = 1 To Me.maxThreadCount Step 1
            'Creat the new worker and add it to the collection.
            worker = New BackgroundWorker
            worker.WorkerReportsProgress = True
            worker.WorkerSupportsCancellation = True
            Me.bwkWorkers.Add(worker)

            'Attach the event handlers.
            AddHandler worker.DoWork, AddressOf DoWork
            AddHandler worker.ProgressChanged, AddressOf ProgressChanged
            AddHandler worker.RunWorkerCompleted, AddressOf RunWorkerCompleted

            'Start working.
            worker.RunWorkerAsync(Me.workIndex)
        Next
end sub


the fear i have is in the dowork sub would the backgroundworkers not overwrite the same variable

VB
Private Sub DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
      Dim worker As BackgroundWorker = DirectCast(sender, BackgroundWorker)
      Dim workItem As String = DirectCast(e.Argument.ToString, String)

      If worker IsNot Nothing AndAlso workItem IsNot Nothing Then
       'Process the work item here.

       WorkData = GetWebpagePageContent(urlrlist(cint(workItem)))
       If WorkData > 50 Then '//safe check make sure it did not return error string
      '// process the workdatahere

     Else
      '// report error here
     End If

     End If
  End Sub


how do i make sure that each thread does not overwrite the variable in the DoWork before one thread finish with it.

Considering Snylock, would it not cause performance issue?, or should i put the work needed to be done in another sub and call it from DoWork?
Posted
Updated 9-Jun-11 22:06pm
v3
Comments
Tarun.K.S 5-Jun-11 16:28pm    
As far as I know, Lock can be used and it shouldn't cause any performance issue.

VB is such an ugly, verbose language. I assume the variable you are concerned about is WorkData? If you were using c# I would recommend the lock statement [^] but I don't know what it translates to in VB.net
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Jun-11 17:14pm    
To throw out idiotic political correctness, it is an ugly language. Being verbose is its best feature, others are worse. It has been strongly revived by .NET but did not stop being VB, a language serious developers usually don't consider. So I don't want to blame you apparent lack of neutrality -- my 5. (Neutrality advocates, compare it with the term "neutering" which means castration.)
--SA
[no name] 5-Jun-11 17:26pm    
Damned if I do, damned if I don't :) If I had said outright it sucks as a language I'd have been flamed. If try to be nice I still get socked :)
Simon_Whale 5-Jun-11 18:40pm    
VB.net lock equivalent is

SyncLock someLock
list.Add(someItem)
End SyncLock
Cool Smith 6-Jun-11 3:39am    
@simon thanks, am aware of synclock, but this line

WorkData = GetWebpagePageContent(urlrlist(cint(workItem)))

takes about a 30s and waiting for each thread to finish before the next thread is not a good idea
I do not think it is possible to have the same variable used on different threads without overwriting it and not using SyncLock or something similiar. Also I think you should ask yourself if you need 10 backgroundworkers or 1 backgroundworker which does DoWork 10 times. And even if you need to do this on seperate threads at all. It seems to be getting very complicated with 10 bgw's that each trigger x times etc. Even if you could make it work, if you look at it next week you'll be lost in a maze of threads. And most computers cannot even handle so much threads at the same time. Anyway, I doubt your software will be much faster with so much overhead. One thing is sure, it is quite error prone.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Jun-11 17:08pm    
Absolutely. My 5.
--SA
Sander Rossel 5-Jun-11 17:13pm    
Thanks! :)
Cool Smith 6-Jun-11 3:45am    
the reason am using 10 is that each work operation uses about 3-5 minutes to finish, and i have about 10,000 works to process, for a single background worker using say at most 5min, the this would take 50,000mins to process all items, this is bad not so?, but with 10 workers, it is reduced to about 5,000mins and less
Sander Rossel 6-Jun-11 5:02am    
You do realize that whatever you are trying to do takes up 3,5 days to process and that is when you are using multiple (10!) threads... That means that whatever you do uses up just about ALL of your computers resources (even the fastest i7 cannot properly handle so many threads) for 3,5 days or longer!
I do not know what you want to achieve, but anything that takes that long to process probably should not be... Are you sure there is no alternative?
Sergey Alexandrovich Kryukov 6-Jun-11 11:57am    
Naerling, let me note: there are always important task which would take days to solve, if not more; and those tasks are very important. Maybe they always will be run, no matter how much powerful computers become. Do you know such examples? There are quite a few...
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900