How to use threads in asp.net?

前端 未结 3 1721
清歌不尽
清歌不尽 2020-12-22 00:15

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.

Thi

3条回答
  •  暖寄归人
    2020-12-22 01:06

    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.

提交回复
热议问题