PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2019 Threading.ThreadPool.QueueUserWorkItem Alternative-VBForums
Results 1 to 3 of 3

Thread: Threading.ThreadPool.QueueUserWorkItem Alternative

  1. #1

    Thread Starter
    Fanatic Member clarkgriswald's Avatar
    Join Date
    Feb 2000
    Location
    USA
    Posts
    728

    Threading.ThreadPool.QueueUserWorkItem Alternative

    We currently use the Threading.ThreadPool.QueueUserWorkItem, but we don't really like the implementation of this API, especially since you can't pass arguments in a type-safe way. Does anyone have any alternative suggestions to this, preferably making use/integrating with async/await?

    We are looking for something along the lines of Fire and Forget.

    Thanks!
    Last edited by clarkgriswald; Oct 11th, 2019 at 08:38 AM.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,995

    Re: Threading.ThreadPool.QueueUserWorkItem Alternative

    You can use the ThreadPool in a type-safe way using Lambda expressions, e.g.
    vb.net Code:
    1. Sub Main()
    2.     Dim numbers = Enumerable.Range(1, 10)
    3.  
    4.     For Each number In numbers
    5.         Dim n = number
    6.  
    7.         ThreadPool.QueueUserWorkItem(Sub() DoSomething(n))
    8.     Next
    9. End Sub
    10.  
    11. Private Sub DoSomething(nunmber As Integer)
    12.     '...
    13. End Sub
    That said, using the Tasks Parallel Library (TPL) is the preferred option these days, e.g.
    vb.net Code:
    1. Sub Main()
    2.     Dim numbers = Enumerable.Range(1, 10)
    3.  
    4.     For Each number In numbers
    5.         Dim n = number
    6.  
    7.         Task.Run(Sub() DoSomething(n))
    8.     Next
    9. End Sub
    10.  
    11. Private Sub DoSomething(nunmber As Integer)
    12.     '...
    13. End Sub
    Exactly how you might make use of Async/Await in your code depends on your code.

  3. #3

    Thread Starter
    Fanatic Member clarkgriswald's Avatar
    Join Date
    Feb 2000
    Location
    USA
    Posts
    728

    Re: Threading.ThreadPool.QueueUserWorkItem Alternative

    Quote Originally Posted by jmcilhinney View Post
    You can use the ThreadPool in a type-safe way using Lambda expressions, e.g.
    vb.net Code:
    1. Sub Main()
    2.     Dim numbers = Enumerable.Range(1, 10)
    3.  
    4.     For Each number In numbers
    5.         Dim n = number
    6.  
    7.         ThreadPool.QueueUserWorkItem(Sub() DoSomething(n))
    8.     Next
    9. End Sub
    10.  
    11. Private Sub DoSomething(nunmber As Integer)
    12.     '...
    13. End Sub
    That said, using the Tasks Parallel Library (TPL) is the preferred option these days, e.g.
    vb.net Code:
    1. Sub Main()
    2.     Dim numbers = Enumerable.Range(1, 10)
    3.  
    4.     For Each number In numbers
    5.         Dim n = number
    6.  
    7.         Task.Run(Sub() DoSomething(n))
    8.     Next
    9. End Sub
    10.  
    11. Private Sub DoSomething(nunmber As Integer)
    12.     '...
    13. End Sub
    Exactly how you might make use of Async/Await in your code depends on your code.
    Thanks @jmcilhinney, one important note I forgot to include in the initial post is, can TPL used in a fire and forget type manner? I am guessing not, since the Start is on a different thread and will not have the request context. Perhaps there is a way to do this?
    Last edited by clarkgriswald; Oct 11th, 2019 at 08:47 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width