2016-10-15 29 views
0

ASP.NET 3.5 VB.NET帶有參數的ASP.NET線程?

我有我想要從10個不同的URL的每一個獲取PNG圖像的代碼。

我發現獲取每個PNG最多需要2-3秒,因此可能最好在同一時間獲取所有PNG。

有人可以通過調整下面的代碼爲每個PNG提取製作一個線程來幫助我嗎?在一個小時左右的嘗試中,我還沒有做過很多線程的工作,我希望得到一些幫助。 TIA

Imports Microsoft.VisualBasic 
Imports System.Collections.Generic 
Imports System.Drawing 
Imports System.Drawing.Drawing2D 


Public Class TestImageSuggestionsCreate 

    Dim intClsWidthMaximumAllowed As Integer = 600 
    Dim intClsHeightMaximumAllowed As Integer = 400 
    Dim intClsWidthMinimumAllowed As Integer = 200 
    Dim intClsHeightMinimumAllowed As Integer = 200 

    Dim strClsImageOriginalURL As String = "" 
    Dim lstClsWebsitesImageURLs As New List(Of String) 


    Public Function fWebsitesImageSuggestionsCreate() As Boolean 

     'Load URLS strings into class List variable: lstClsWebsitesImageURLs 
     fWebsitesImageURLSuggestionsGet() 

     'Go through each URL and download image to disk 
     For Each strURL1 As String In lstClsWebsitesImageURLs 

      'This needs to be done in a separate thread 
      '(Up to 10 threads): 

      fAddImageIfSuitable(strURL1) 

     Next 

    End Function 

    Private Function fWebsitesImageURLSuggestionsGet() As Boolean 

     Dim strURL As String = "" 

     strURL = "https://upload.wikimedia.org/wikipedia/en/thumb/f/f7/Sheraton_Hotels.svg/1231px-Sheraton_Hotels.svg.png" 
     lstClsWebsitesImageURLs.Add(strURL) 

     strURL = "http://wall--art.com/wp-content/uploads/2014/10/sheraton-logo-png.png" 
     lstClsWebsitesImageURLs.Add(strURL) 

     'Up to 10 strURL items 
     '............. 

    End Function 

    Private Function fAddImageIfSuitable(ByVal strImageURL As String) As Boolean 

     'Get bitmap from URL 
     Dim btmImage1 As Bitmap = New Bitmap(fGetStreamBitmap(strImageURL)) 

     'Don't add if image too small 
     If btmImage1.Width < intClsWidthMinimumAllowed Or _ 
      btmImage1.Height < intClsHeightMinimumAllowed Then 
      Exit Function 
     End If 

     'Save image to disk here 
     '.............. 

    End Function 

    Private Function fGetStreamBitmap(ByVal strURL As String) As Bitmap 

     Try 
      Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(strURL) 
      Dim response As System.Net.WebResponse = request.GetResponse() 
      Dim responseStream As System.IO.Stream = response.GetResponseStream() 
      Dim btmBitmap1 As New Bitmap(responseStream) 

      Return btmBitmap1 

     Finally 
     End Try 

    End Function 

End Class 

回答

1

使用Parallel.ForEach來簡化線程。

How to: Write a Simple Parallel.ForEach Loop

您可以限制線程的數量與MaxDegreeOfParallelism

Parallel.ForEach(
    lstClsWebsitesImageURLs, 
    new ParallelOptions { MaxDegreeOfParallelism = 10 }, 
    strURL1 => { fAddImageIfSuitable(strURL1); } 
); 
+0

謝謝,但很可惜的項目在ASP.NET 3.5中。 – user1946932

+0

@ user1946932 - 在開始的時候會很高興知道。 – Enigmativity

1

創建10個任務,每個任務任務陣調用異步方法(該過程中URL下載圖像到磁盤同樣的方法) 等待所有線程返回

var tasks = new List<Task>(); 
foreach(task in tasks){ 
task[0] = GetImageAsync();} 

Task.WaitAll(tasks.ToArray()); 
+0

非常感謝這個項目在ASP.NET 3.5中。 – user1946932

+0

@ user1946932 - 在開始的時候很高興知道。 – Enigmativity

+0

@Enigmativity - 是的,道歉。 – user1946932