列车时刻表在这里指前几日开发的 PPC版列车时刻表 和 SmartPhone版列车时刻表
在移动设备上,由于设备性能的关系,往往在PC上的一个操作在设备上所需要花费的时间将会大很多。例如,同样的数据量(大约4.8万条记录),在PC上,做一个Select使用SQL Server所花时间不到1秒钟,而在设备上就会慢的很多。
在前一版本的列车时刻表中,都是把结果查询出来之后,然后在根据结果来更新界面。结果导致界面响应太慢,甚至像是死机了一样。
后来,在MSDN上看到这样一篇文章,Microsoft .NET Framework 精简版多线程提示 ,受益匪浅。遂在新版本的列车时刻表将查询都更改为使用后台数据查询然后通过界面上的控件Invoke的方式来处理,结果速度得到了大幅度的提高。
代码示例如下:
代码片断1(后台用于按照列车车次名称查询的代码):
public sealed class AsyncSearchByTrain:IAsyncSearch
{
private string _TrainName;
public string TrainName
{
get
{
return _TrainName;
}
set
{
_TrainName = value;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public event PopupSearchByTrainEventHandler PopupResultOfTrain;
public event SearchOverEventHandler SearchOver;
![](/Images/OutliningIndicators/InBlock.gif)
#region IAsyncSearch 成员
![](/Images/OutliningIndicators/InBlock.gif)
public void Search()
{
int i=0;
![](/Images/OutliningIndicators/InBlock.gif)
if(this.PopupResultOfTrain!=null)
{
string text1 = "select
. where TrainName='" + TrainName + "';";
SQLiteCommand command1 = new SQLiteCommand(text1, Database.conn);
IDataReader reader1 = command1.ExecuteReader();
![](/Images/OutliningIndicators/InBlock.gif)
while (reader1.Read())
{
//组织查询到的某一条记录然后将其抛出
this.PopupResultOfTrain(sbtra);
i++;
}
reader1.Close();
![](/Images/OutliningIndicators/InBlock.gif)
}
if(this.SearchOver!=null)
this.SearchOver(i);
}
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
}
我们可以看到,在里面我放了两个事件,一个用于抛出查到的某一条记录,另外一个用于通知界面上已经查询完毕。
代码片断二(用于提交查询的地方):
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();
![](/Images/OutliningIndicators/None.gif)
在查询的时候,使用一个线程来查询。
代码片断三(事件处理):
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() }));
//将本地变量中的数据显示到列表中去
this.TrainList.Update();
}
private void PopupResultOfTrain(SearchByTrainResultArgs searchByTrainResultArgs)
{
this._ts = searchByTrainResultArgs.TrainStation;
//将事件抛出的参数存放到一个本地变量中
this.TrainList.Invoke(new EventHandler(UpdateList));
//然后调用本地控件的Invoke方法来更新数据
}
private void SearchOver(int count)
{
if (count == 0)
{
//如果没有结果的话。。。。
}
else
{
//如果有结果的话
..
}
}
通过这样处理,我们的查询线程在后台查找数据将结果反馈到界面上来,改善了用户的操作。
在移动设备上,由于设备性能的关系,往往在PC上的一个操作在设备上所需要花费的时间将会大很多。例如,同样的数据量(大约4.8万条记录),在PC上,做一个Select使用SQL Server所花时间不到1秒钟,而在设备上就会慢的很多。
在前一版本的列车时刻表中,都是把结果查询出来之后,然后在根据结果来更新界面。结果导致界面响应太慢,甚至像是死机了一样。
后来,在MSDN上看到这样一篇文章,Microsoft .NET Framework 精简版多线程提示 ,受益匪浅。遂在新版本的列车时刻表将查询都更改为使用后台数据查询然后通过界面上的控件Invoke的方式来处理,结果速度得到了大幅度的提高。
代码示例如下:
代码片断1(后台用于按照列车车次名称查询的代码):
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
我们可以看到,在里面我放了两个事件,一个用于抛出查到的某一条记录,另外一个用于通知界面上已经查询完毕。
代码片断二(用于提交查询的地方):
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
代码片断三(事件处理):
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/dot.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
通过这样处理,我们的查询线程在后台查找数据将结果反馈到界面上来,改善了用户的操作。