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.