2

I have web forms application. On one form I have a few functions. Which are called syncronously and takes some time. So I need to call them in different threads.

This is the sample of what I'm doing:

    protected void Page_Load(object sender, EventArgs e)
    {
        Thread t1 = new Thread(new ThreadStart(Function1));
        t1.Start();
        Thread t2 = new Thread(new ThreadStart(Function2));
        t2.Start();
    }

    private void Function1()
    {
        Thread.Sleep(5000);
        lbl1.Text = "Function1 completed";
    }

    private void Function2()
    {
        Thread.Sleep(5000);
        lbl2.Text = "Function2 completed";
    }

If I debug (set breackpoints) lbl1.Text = "Function1 completed"; and lbl2.Text = "Function2 completed"; is getting called, but there texts are not changing on final html page.

Also Page load does not takes 5 sec.

p.s. I know asp net works different but I have no idea what I'm doing wrong.

David Yaw
  • 27,383
  • 4
  • 60
  • 93
user1972654
  • 23
  • 1
  • 1
  • 3

3 Answers3

9

Nothing is waiting for your threads to complete before the page is rendered and returned - that's what's wrong.

To the end of your Page_Load() function (or at the latest-possible point in the page rendering lifecycle), add:

t1.Join();
t2.Join();

Additionally: you should not update lbl1 and lbl2 within the thread proc - you should store the result(s) in variables and reflect the calculated values in the main rendering thread (i.e. once Join has returned).

Edit: Although this fixes the problem in the question, have a look at PageAsyncTask as recommended by Vano's answer.

Steve Mayne
  • 22,285
  • 4
  • 49
  • 49
  • Although this gets your code working, I suggest you look at PageAsyncTask as recommended by Vano's answer to this question. – Steve Mayne Jan 13 '13 at 12:53
  • I have taken a look at this, but I dont understand what benefit the PageAsyncTask method offers to the one you posted as an answer, Could you possibly explain why one is better than the other, they both appear to work the same way or does something under the hood actually prevent the thread.runs and then joins from actually running asynchronously? – d0rf47 Jun 24 '22 at 14:08
  • 1
    @d0rf47 PageAsyncTask will probably be more CPU-efficient than using a Thread - even though they achieve the same result. – Steve Mayne Jul 11 '22 at 12:22
2

If you want to execute functions asynchronously, you need some steps to do:

1). you need to set Async="true" page directive.

<%@ Page Language="C#" Async="true" ... %>

2). You need to create PageAsyncTask object and register it using Page.RegisterAsyncTask() function.

RegisterAsyncTask function registers your function in syncronization context so ASP.NET will run your function asynchronously and also waits for all registered function to complete and then continues page processing.

Vano Maisuradze
  • 5,829
  • 6
  • 45
  • 73
0

You start new threads but don't wait for them to complete. Hence your page_load runs to completion since thread.start() is non blocking.

Have a look at async/wait in the latest .NET version. It makes things a lot easier and predictable

lboshuizen
  • 2,746
  • 17
  • 20