0

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

我宁愿同时运行它们。

到目前为止,这是我使用 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)' 兼容的签名。

任何帮助是极大的赞赏!

4

2 回答 2

2
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);
      }
   }
}
于 2013-05-02T13:28:47.277 回答
1

这是 WinForms 还是 asp.net?

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

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

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)
于 2013-05-01T23:22:33.587 回答