2016-10-15 3 views
0

ASP.NET 3.5 VB.NET매개 변수가있는 ASP.NET 스레딩?

각각 10 개의 다른 URL에서 PNG 이미지를 가져 오려는 코드가 있습니다.

각 PNG를 가져 오는 데 최대 2-3 초가 걸릴 수 있으므로 동시에 모든 것을 가져 오는 것이 가장 좋을 것이라고 생각했습니다.

각 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

감사를 반환 할 수있는 비동기 방식 (디스크에 이미지를 다운로드 할 수있는 URL을 처리 같은 방법) 대기를 호출 각 작업에 대한 작업 배열 프로젝트는 ASP.NET 3.5에 있습니다. – user1946932

+0

@ user1946932 - 처음에 알면 좋았을 것입니다. – Enigmativity

1

만들기 (10) 작업, 모든 스레드가 있지만, 슬프게도

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

관련 문제