我有一些代码:
public MainPage()
{
InitializeComponent();
MyPostRequest.GetDataFromService((result) =>
{
Dispatcher.BeginInvoke(() => { //Update UI from web service});
});
System.Threading.Thread myThread = new System.Threading.Thread(new System.Threading.ThreadStart(MyThread));
myThread.Start();
}
private void MyThread()
{
//do something
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Dispatcher.BeginInvoke(() =>
{
stopwatch.Stop();
long elapsedMilliseconds = stopwatch.ElapsedMilliseconds;
Debug.WriteLine("time: {0} ms", elapsedMilliseconds);
//update UI
});
}
基本上,测量的时间约为100毫秒,但很少为1000 - 5000毫秒。在升级UI之前,我检查本地数据库中的数据并对数据进行采样。我很困惑,切换到UI线程的延迟到底是什么。可能是什么问题?