Take the 2-minute tour ×

My multithreading program is not working, the dropdownlists will either be blank or only one will load.

    Imports System.Threading
Partial Class _Default
    Inherits System.Web.UI.Page

    Public Delegate Sub WaitCallback(ByVal state As Object)

    Private Sub LongTimeTask(ByVal s As Object)

        Dim i As Integer
        Dim str As String
        str = s.ToString

        For i = 0 To 1000
            str = str + "--" + str

    End Sub

    Protected Sub Button1_Click(ByVal sender As _
         Object, ByVal e As System.EventArgs) Handles Button1.Click

        If ThreadPool.QueueUserWorkItem( _
          New WaitCallback(AddressOf LongTimeTask), TextBox1.Text) _
            Label2.Text = "Queued successfully"
            Label2.Text = "Failed"
        End If
    End Sub
share|improve this question
What leads you to believe that this should work? And why would you bother? –  Joel Etherton Feb 11 '11 at 14:52
does multithreading not work in asp.net, this isnt what i actually want to use it for i just want to practice. –  MyHeadHurts Feb 11 '11 at 14:54
can you not multithread in asp.net/vb.net –  MyHeadHurts Feb 11 '11 at 14:55
@MyHeadHurts - You CAN. You shouldn't. It's unpredictable and can lead to unwanted results, race conditions, deadlocks, improperly terminated processes, locked files, etc. It's just bad news for the most part. –  Joel Etherton Feb 11 '11 at 14:59
@MyHeadHurts - You aren't updating them all at once. Keep in mind that each thread has to wait its turn on the processor (since they'll all live on the same cpu), so they'll each get done synchronously. What you're attempting to do makes no sense when applied to the Asp.Net lifecycle. –  Joel Etherton Feb 11 '11 at 15:09
show 1 more comment

2 Answers

up vote 1 down vote accepted

I wouldn't worry about threading those things. The Page life cycle doesn't wait for threads to return.


share|improve this answer
i want to thread other things like 2 sql stored procedures, or maybe update my updatepanels at the same time –  MyHeadHurts Feb 11 '11 at 14:56
+1 - That was the article I was looking for, but apparently it didn't pop up in the search because I had it refined to Asp.Net only. –  Joel Etherton Feb 11 '11 at 14:58
New WaitCallback(AddressOf LongTimeTask), TextBox1.Text) _ –  MyHeadHurts Feb 11 '11 at 15:05
i got an error Error 1 Value of type '_Default.WaitCallback' cannot be converted to 'System.Threading.WaitCallback'. –  MyHeadHurts Feb 11 '11 at 15:06
add comment

First, threads can't access objects that "belong" to each other. Since your web page life cycle is processing in thread A, the objects being processed there are inaccessible to th1 and th2. This includes cmb1 and cmb2.

Secondly, in the Asp.Net lifecycle, child threads are alive only as long as the parent thread is alive. In your code you call th1.Start() and th2.Start() in a "fire and forget" kind of way. As the code looks right there, the page lifecycle finishes off well before the threads can complete their processes and return to the render event of the page lifecycle. Once the page thread is ended and the response is pushed to the client, the child threads are orphaned and terminated unfinished.

share|improve this answer
add comment

Your Answer


