2016-12-27 87 views
0

我需要在参数列表上做一个taks:所有这些任务是独立的。 我不明白怎么做..我试图将参数分成一个“共享类”,并为列表中的每个项目创建一个类的不同实例,然后以异步方式在每个实例上启动该函数:VB.NET多线程

Imports System.Runtime.InteropServices 
Imports System.IO 
Public Class DataContainer 
    Public Parameters as double 'obviously simplified code ;-) 
End Class 
Public Class JobDoer 
    Public CommonData As DataContainer 
    Public PrivData as double 
    Public Async Function YesWeCan() As Task(Of Boolean) 
     Return Task.Factory.StartNew(Of Boolean)(
      DoIt(CommonData.Parameters , PrivData) 
     ) 

    End Function 
    Public Function DoIt(a as double,b as double) 
     return 0 
    end function 
End Class 

==>任务没有定义...

.NET框架3.0 VS 2015年

任何想法?

回答

1

The Async and Await关键字在.NET 3.0中不可用。它们已经在.NET 4.5中引入,尽管您已经可以在4.0中使用它们(有些修改,比如对于某些静态函数,必须使用TaskEx而不是Task),如果通过导入Microsoft.Bcl.Async包的NuGet。

你当然可以简单地开始新线程而不必使用Async/Await。可以使用ThreadPool。以下是我过去创建的一些代码,最初是用C#编写的。我现在将其转换并删除了至少需要.NET 4.0的部分。虽然没有测试过。

Private Sub SubmitWorkToThreadPool() 
    For i as Integer = 0 To yourWorkItems.Count 'Adjust this loop so it adds all your tasks to the thread pool. 
     'customParameter is passed as parameter 'state' to the DoParallelWork function. 
     ThreadPool.QueueUserWorkItem(AddressOf DoParallelWork, customParameter) 
    Next 
End Sub 

Private Sub DoParallelWork(state As Object) 
    'TODO: Add code to be executed on the threadpool 
End Sub 

在4.0版本把它写的方式,让我等待所有工作项目使用CountdownEvent其提交给线程池之后完成。但是这个类只存在于4.0之后,所以我删除了它。如果您需要等待一切完成,您可能需要找到另一种方式。

+0

感谢您的回答。不幸的是,我不能使用更新的框架,因为我使用DLL的方式......有没有办法在框架3.0中做多线程? – Pierre

+0

检查我的编辑。没有测试它,但应该在理论上工作。 –

+0

谢谢,我正在适应这一点,它似乎工作正常! – Pierre