同样 UI 个线程和一个计时器
Again UI thread and a timer
我有一个代码部分,我从服务器调用一些 XML 数据。有时网络或服务器本身有错误。所以我需要一些错误处理。
为此,我安装了 AwesomeFonts.WPF 并创建了一个简单的
<StackPanel Name="ErrorPanel" Height="200" Width="300" Visibility="Hidden" Canvas.Top="440" Canvas.Left="810" Panel.ZIndex="9999">
<fa:ImageAwesome Icon="Spinner" Spin="True" Height="100" Width="100" />
<TextBlock Text="data loading..." />
</StackPanel>
那应该显示旋转图标的加载动画。现在我想做一些像
ErrorPanel.Visibility = Visibility.Visible;
mainPznItem.SubPzns = Communication.GetProductList(tempPznList);
ErrorPanel.Visibility = Visibility.Hidden;
现在我知道了 WPF 的调度问题并且已经尝试过类似的东西
Application.Current.Dispatcher.Invoke(() => { ErrorPanel.Visibility = Visibility.Visible; });
或类似这样的东西
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new
ThreadStart(delegate
{
ErrorPanel.Visibility = Visibility.Visible;
}));
在这个上我真的看到了面板,但是动画停止了。
而且我想让一个计时器运行,这样我就可以缩短服务器的超时时间。我想做的更像是:
1。显示动画
2.启动计时器
3。调用服务器
4。数到 10
5。如果我直到 10 点才得到答案,则显示错误消息,否则显示结果
6。隐藏等待动画。
有人知道如何实现吗?
您应该在后台线程上执行 GetProductList
方法。
async void Click(object sender, RoutedEventArgs e)
{
ErrorPanel.Visibility = Visibility.Visible;
var products = await Task.Run(() => Communication.GetProductList(tempPznList));
mainPznItem.SubPzns = products;
ErrorPanel.Visibility = Visibility.Hidden;
}
这是重要的部分,因为 UI 线程不能同时处理输入事件和执行您的方法。
如果需要,您也可以启动计时器。像这样:
DispatcherTimer _timer;
bool cancelled;
async void Click()
{
if (_timer != null)
{
_timer.Stop();
_timer.Dispose();
}
ErrorPanel.Visibility = Visibility.Visible;
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromSeconds(10);
_timer.Tick += _timer_Tick;
_timer.Start();
var products = await Task.Run(() => Communication.GetProductList(tempPznList));
_timer.Stop();
if (!cancelled)
{
mainPznItem.SubPzns = products;
ErrorPanel.Visibility = Visibility.Hidden;
}
}
private void _timer_Tick(object sender, EventArgs e)
{
MessageBox.Show("error...");
_timer.Tick -= _timer_Tick;
_timer.Stop();
cancelled = true;
ErrorPanel.Visibility = Visibility.Hidden;
}
我有一个代码部分,我从服务器调用一些 XML 数据。有时网络或服务器本身有错误。所以我需要一些错误处理。 为此,我安装了 AwesomeFonts.WPF 并创建了一个简单的
<StackPanel Name="ErrorPanel" Height="200" Width="300" Visibility="Hidden" Canvas.Top="440" Canvas.Left="810" Panel.ZIndex="9999">
<fa:ImageAwesome Icon="Spinner" Spin="True" Height="100" Width="100" />
<TextBlock Text="data loading..." />
</StackPanel>
那应该显示旋转图标的加载动画。现在我想做一些像
ErrorPanel.Visibility = Visibility.Visible;
mainPznItem.SubPzns = Communication.GetProductList(tempPznList);
ErrorPanel.Visibility = Visibility.Hidden;
现在我知道了 WPF 的调度问题并且已经尝试过类似的东西
Application.Current.Dispatcher.Invoke(() => { ErrorPanel.Visibility = Visibility.Visible; });
或类似这样的东西
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new
ThreadStart(delegate
{
ErrorPanel.Visibility = Visibility.Visible;
}));
在这个上我真的看到了面板,但是动画停止了。
而且我想让一个计时器运行,这样我就可以缩短服务器的超时时间。我想做的更像是:
1。显示动画
2.启动计时器
3。调用服务器
4。数到 10
5。如果我直到 10 点才得到答案,则显示错误消息,否则显示结果
6。隐藏等待动画。
有人知道如何实现吗?
您应该在后台线程上执行 GetProductList
方法。
async void Click(object sender, RoutedEventArgs e)
{
ErrorPanel.Visibility = Visibility.Visible;
var products = await Task.Run(() => Communication.GetProductList(tempPznList));
mainPznItem.SubPzns = products;
ErrorPanel.Visibility = Visibility.Hidden;
}
这是重要的部分,因为 UI 线程不能同时处理输入事件和执行您的方法。
如果需要,您也可以启动计时器。像这样:
DispatcherTimer _timer;
bool cancelled;
async void Click()
{
if (_timer != null)
{
_timer.Stop();
_timer.Dispose();
}
ErrorPanel.Visibility = Visibility.Visible;
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromSeconds(10);
_timer.Tick += _timer_Tick;
_timer.Start();
var products = await Task.Run(() => Communication.GetProductList(tempPznList));
_timer.Stop();
if (!cancelled)
{
mainPznItem.SubPzns = products;
ErrorPanel.Visibility = Visibility.Hidden;
}
}
private void _timer_Tick(object sender, EventArgs e)
{
MessageBox.Show("error...");
_timer.Tick -= _timer_Tick;
_timer.Stop();
cancelled = true;
ErrorPanel.Visibility = Visibility.Hidden;
}