Review of train schedule development (I): Background thread update Front-End Interface

Source: Internet
Author: User
Here, we will refer to the PPC and smartphone train schedules developed in the past few days.

Because of the performance of mobile devices, it takes a lot of time to perform an operation on a PC. For example, for the same data volume (about 48 thousand records), it takes less than 1 second to use SQL server as a SELECT statement on a PC, and it will be much slower on the device.
In the previous version of the train schedule, the results are queried and then updated based on the results. As a result, the interface response is too slow, or even crashes.
Later, I saw this article on msdn.Article, Microsoft. NET Framework lite multi-threaded tip, benefited a lot. In the new version of the train schedule, the query is changed to use the background data query and then the invoke control on the interface is used for processing. The result speed is greatly improved.

Code Example:
Code snippet 1 (the code used in the background to query by train name ): Public   Sealed   Class Asyncsearchbytrain: iasyncsearch
{
Private   String _ Trainname;
Public   String Trainname
{
Get
{
Return_ Trainname;
}
Set
{
_ Trainname=Value;
}
}

Public   Event Popupsearchbytraineventhandler popupresultoftrain;
Public   Event Searchovereventhandler searchover;

Iasyncsearch Member # Region Iasyncsearch Member

Public   Void Search ()
{
Int I = 0 ;

If ( This . Popupresultoftrain ! = Null )
{
String Text1 =   " Select. Where trainname =' "   + Trainname +   " '; " ;
Sqlitecommand command1 =   New Sqlitecommand (text1, database. Conn );
Idatareader reader1 = Command1.executereader ();

While (Reader1.read ())
{< br> // A record queried by the organization is then thrown
This . popupresultoftrain (sbtra);
I + ;< BR >}
Reader1.close ();

}
If ( This . Searchover ! = Null )
This . Searchover (I );
}

# Endregion

}

We can see that I put two events in it. One is used to throw a certain record, and the other is used to complete the query on the notification interface.

Code snippet 2 (where the query is submitted ): Core. asyncsearchbytrain asbt =   New Breakstring. trainsinfo. Core. asyncsearchbytrain ();
Asbt. trainname =   This . _ Trainname;
Thread searchthread =   New Thread ( New Threadstart (asbt. Search ));
Asbt. searchover + =   New Breakstring. trainsinfo. Core. searchovereventhandler (searchover );
Asbt. popupresultoftrain + =   New Popupsearchbytraineventhandler (popupresultoftrain );
Searchthread. Start ();

A thread is used for query.

Code snippet 3 (event processing ): Public   Void Updatelist ( Object Sender, eventargs E)
{
This . Trainlist. Items. Add ( New Listviewitem ( New   String [] {This. _ TS. sequence. tostring (),This. _ TS. Name,This. _ TS. arrivetime,This. _ TS. leavetime,This. _ TS. triptime,This. _ TS. Distance. tostring ()} ));
// Display the data in the local variable to the list
This . Trainlist. Update ();
}
Private   Void Popupresultoftrain (searchbytrainresultargs)
{
This. _ TS=Searchbytrainresultargs. trainstation;
//Store the parameters thrown by the event to a local variable.
This. Trainlist. Invoke (NewEventhandler (updatelist ));
//Then, call the invoke method of the local control to update the data.
}
Private   Void Searchover ( Int Count)
{
If (Count =   0 )
{
//If there is no result ....
}
Else
{
//If there is any result ..
}
}

Through such processing, our query thread searches for data in the background and returns the results to the interface, improving user operations.

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.