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
}
}