How to call multiple different methods simultaneously

I have some different methods that each import products from different sites. If I execute these sequentially the entire process takes a lot of time, particularly when 1 method is running on a site that is less responsive.

I'd rather run them simultaneously.

This is what I have so far with 1 example method:

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

I tried this:

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

Then I get 3 errors that I have not specified arguments for the 3 parameters: productIdPrefix, publishersite and feedURL

I also tried:

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

I get this error: Method 'Protected Sub ImportProductsPublisherA(productIdPrefix As String, publishersite As String, feedURL As String)' does not have a signature compatible with delegate 'Delegate Sub WaitCallback(state As Object)'.

Any help is greatly appreciated!

Answers


Is this WinForms or asp.net?

If winforms create a background worker for each function and start all of them, they will work independently in a separate thread.

If asp.net you want to look at using 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))

If you functions take any parameters you can pass them like this:

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

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);
      }
   }
}

Need Your Help

Python stream file

python stream

I want to stream a file (mp3) in python. I've written the server code (which doesn't work):

How can I add a downloadable file to my Github.io page?

html github github-pages

I have set up my professional website/homepage using Github Pages. I know if this was just HTML being served up from somewhere, my downloadable file would need to be in the directory of my .html fi...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.