如何同时调用多种不同的方法

时间:2013-05-01 21:11:03

标签: asp.net vb.net multithreading

我有一些不同的方法,每个方法都从不同的网站导入产品。 如果我按顺序执行这些操作,整个过程会花费很多时间,尤其是当响应速度较慢的站点上运行1个方法时。

我宁愿同时运行它们。

这是我到目前为止用一个示例方法:

Public Class feedParameters

Private _productIdPrefix As String
Private _publishersite As String
Private _feedURL As String

Public Property productIdPrefix() As String
    Get
        Return _productIdPrefix
    End Get
    Set(value As String)
        _productIdPrefix = value
    End Set
End Property
Public Property publishersite() As String
    Get
        Return _publishersite
    End Get
    Set(value As String)
        _publishersite = value
    End Set
End Property
Public Property feedURL() As String
    Get
        Return _feedURL
    End Get
    Set(value As String)
        _feedURL = value
    End Set
End Property


End Class




Dim fpm As New feedParameters
fpm.publishersite = "mypublisher.nl"
fpm.feedURL = "http://www.domain.com/test.xml"
fpm.productIdPrefix = "10"

Protected Sub ImportProductsPublisherA(ByVal productIdPrefix As String, ByVal publishersite As String, ByVal feedURL As String)

End Sub

我试过了:

System.Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf ImportProductsPublisherA()), fpm)

然后我得到3个错误,我没有为3个参数指定参数:productIdPrefix,publishersite和feedURL

我也尝试过:

System.Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf ImportProductsPublisherA), fpm)

我收到此错误: 方法'Protected Sub ImportProductsPublisherA(productIdPrefix As String,publishersite As String,feedURL As String)'没有与委托'Delegate Sub WaitCallback(state As Object)'兼容的签名。

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

这是WinForms还是asp.net?

如果winforms为每个函数创建一个后台工作程序并启动所有函数,它们将在一个单独的线程中独立工作。

如果您想使用System.Threading.ThreadPool - http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx

查看asp.net
System.Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf ImportProductsSiteA))

System.Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf ImportProductsSiteB))

System.Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf ImportProductsSiteC))

System.Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf ImportProductsSiteD))

如果你的函数采用任何参数,你可以像这样传递它们:

System.Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf ImportProductsSiteA), parameter-here)

答案 1 :(得分:1)

using System.Threading.Tasks;

namespace Foo
{
   public class Bar
   {
      public void ImportProductsSiteA() { }
      public void ImportProductsSiteB() { }
      public void ImportProductsSiteC() { }
      public void ImportProductsSiteD() { }

      public void Execute()
      {
         var a = Task.Factory.StartNew(ImportProductsSiteA);
         var b = Task.Factory.StartNew(ImportProductsSiteB);
         var c = Task.Factory.StartNew(ImportProductsSiteC);
         var d = Task.Factory.StartNew(ImportProductsSiteD);
         Task.WaitAll(a, b, c, d);
      }
   }
}