简体   繁体   中英

ASP.NET threading with parameter?

ASP.NET 3.5 VB.NET

I have code which I want to fetch PNG images from each of 10 different URL's.

I have found that it can take up to 2-3 seconds to get each PNG so thought it might be best to fetch them all at the same time.

Can someone help me out by adjusting the code below to make a Thread for each PNG fetch? I haven't done a lot of Thread stuff before and after an hour or so of trying I was hoping for some help. 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

Use Parallel.ForEach for simplified threading.

How to: Write a Simple Parallel.ForEach Loop

You can limit the number of threads with MaxDegreeOfParallelism .

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

Create 10 Task , Task Array on each Task call a Asynchronous Method (same method which process the URL to Download image to Disk) wait for all thread to return

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

Task.WaitAll(tasks.ToArray());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM