我有一些代码:
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 线程时的延迟究竟是什么。可能是什么问题?