多个背景工问题 [英] Multiple backgroundworkers problem

查看:80
本文介绍了多个背景工问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在代码中创建了10个Backgroundworker,并为他们分配了相同的DoWork,ProgressChanged和RunWorkerCompleted,就像这样

i have created 10 Backgroundworkers in code, and i assign them the same DoWork, ProgressChanged and RunWorkerCompleted like this

 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



我担心在dowork子目录中,后台工作人员不会覆盖相同的变量



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

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



我如何确保每个线程在一个线程完成操作之前都不会覆盖DoWork中的变量.

考虑Snylock,这不会引起性能问题吗?还是应该将需要完成的工作放在另一个子目录中,然后从DoWork中调用它?



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?

推荐答案

VB就是这样的一种丑陋,冗长的语言.我假设您关注的变量是WorkData?如果您使用的是c#,则建议使用锁语句 [ ^ ],但我不知道它在VB.net中的含义.
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


我不认为有可能在不覆盖它的情况下在不同线程上使用相同的变量,并且不会使用SyncLock或类似的东西.另外,我认为您应该问自己是否需要10个背景工或1个做10次DoWork的背景工.即使您根本需要在单独的线程上执行此操作.每次触发x次等10次bgw似乎变得非常复杂.即使您可以使其正常工作,但如果下周查看它,您将迷失在线程的迷宫中.而且大多数计算机甚至无法同时处理这么多线程.无论如何,我怀疑您的软件会在这么大的开销下运行得更快.可以肯定的是,这很容易出错.
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.


这篇关于多个背景工问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆