winform batch update data _ will lead to the implementation of a long dead interface card

Original: WinForm batch update data _ will lead to the implementation of a long dead interface card

  Introduction: Use winform code is executed after a trigger event, if time-consuming very long, it will result in suspended animation window interface! I recently through the implementation of a winform form: batch update tasks when necessary operating data of a number of databases, since the amount of data to reach one million level, time-consuming, updating only slowly, slowly executed.   

However, during the execution of a wonderful experience problems: the form in debug mode, code can be executed cyclically slowly, did not appear abnormal. But when I run alone EXE program, he will now: program suspended animation, not responding state.  

Background Although Baidu did not find a direct answer, but also found the reason: because when very time-consuming operation, the program will be suspended animation, the solution is simple: In your time-consuming method where, with asynchronous processing, do not let the program you have been waiting for the results.

 

The following code:

By asynchronous Task.Run way for a quick end to the method, not the middle of the update process stage operation is the time-consuming, after the adoption of this transformation, the interface is no longer stuck a perfect solution.

///  <Summary> 
        /// Provisional Method - used to brush the user address, phone number data
         ///  </ Summary> 
        ///  <param name = "SENDER"> </ param> 
        ///  <param name = " E "> </ param> 
        Private  void btnUpdateUserAddressPhone_Click ( Object SENDER, EventArgs E) 
        { 
            the this .txtActionInfomation.Text $ = " The method begins " ;
             the this .txtActionInfomation.Update (); 
            Task.Run (() =>{
                 // consuming the code 
                do 
                { 
                    // my update code, you need to obtain and update the database page, time-consuming
                     //...
                     // ... 
                    the this .txtActionInfomation.Text $ = " X-pieces of data has been updated " ;
                     the this .txtActionInfomation.Update (); 

                } the while ( to true ); 
            }); 
            the this .txtActionInfomation.Text $ = " End Method " ;
             the this .txtActionInfomation.Update (); 

        }

 

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/11100204.html