In Mac OS X I can take advantage of more than 1 core using GCD (Grand Central Dispatch). What is the equivalent for a Vi开发者_StackOverflow社区sual Basic program?
Threading in VB.NET can be very simple.
Shared Sub New()
Dim t As New System.Threading.Thread(AddressOf GetWebpage)
t.Start()
End Sub
All the above is doing is causing a function to be executed in its own thread.
Here's the function being called:
Private Shared Sub GetWebpage()
Dim URL As String = ConfigurationManager.AppSettings("AppCacheURL")
Dim REQ As System.Net.HttpWebRequest = System.Net.WebRequest.Create(URL & "?Task=PopulateURLCache&Payload=App_Start")
REQ.GetResponse()
End Sub
This is a simplified example of an asyncronous call to a function that fires an HTTP Request to a webpage. The original code is a little more complex with error handling and logging, but it basically causes a "keep alive" style aspx page to be accessed without slowing down the calling application. (Fire and forget)
If you need a framework or library to accomplish your goals, have a look at:
- Parallel Extensions
- Task Parallel Library reference and tutorial
You should look at PLINQ and the Task Parallel Library, both of which can be used to utilise the number of cores available in a host computer for performing CPU bound operations.
Joseph Albahari has a great tutorial on using Parallel Programming techniques (in C#), but the exact same concepts apply to VB.NET.
精彩评论