Asp.net uses multithreading to perform long-time tasks

Source: Internet
Author: User

Implementation ideas:Start a new thread, run a long time task in the new thread, save the sub-thread execution progress, use ajax to get the execution progress, and report it to the user.

1. Use session to save the sub-thread execution progress.

Note: 1. When using session to save the execution status, you must first set the session value in the main thread; otherwise, an error will be reported (the error message is "enable the page sessionstate ).

2. The session must be saved in the process. If it is stored in StateServer or sqlserver, no results will be obtained.

2. Use cache to save the sub-thread execution progress.

Using Cache to save the execution status can solve two problems encountered during session usage,

However, when cache is used, only one user can perform the operation. Otherwise, only the execution status of the last user can be obtained for multi-user operations.

 

Some examples are as follows:Code:

 

// Main thread Thread handlethread = New Thread ( New Parameterizedthreadstart ( This . Updatedata ));
Handlethread. Start (SaveFile );

///   <Summary>
/// Process Data
///   </Summary>
///   <Param name = "filesrc"> Excel files uploaded </Param>
Private Void Updatedata ( Object Filesrc)
{
If (Filesrc = Null )
{
Return ;
}

Caching. Set (productcachekey. tb_data_update_status, " 1 " , Null , Datetime. Now. addminutes (30 )); // Processing

Try
{
If (! This . Handdata (filesrc. tostring ()))
{
Caching. Set (productcachekey. tb_data_update_status, " 0 " , Null , Datetime. Now. addminutes ( 30 )); // Handling error
Return ;
}

Caching. Set (productcachekey. tb_data_update_status, " 2 " , Null , Datetime. Now. addminutes ( 30 )); // Processed
}
Catch
{
Caching. Set (productcachekey. tb_data_update_status, " 0 " , Null , Datetime. Now. addminutes ( 30 )); // Handling error
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.