我们在WPF中有一个应用程序,它通过ObservableCollection显示数据.5分钟后,我想刷新数据.
我以为我可以将System.Timers.Timer对象用于其Elapsed事件,然后调用a BackgroundWorker来调用启动作业的方法.该方法位于ViewModel类上.
但似乎线程存在问题.
所以我尝试了Dispatcher,但同样的事情.
这是我的(简化和未优化)代码:
/// <summary>
/// Initializes a new instance of the <see cref="ApplicationController"/> class.
/// </summary>
public ApplicationController()
{
CreateDefaultTabs();
Timer timer = new Timer(20000); //20 secs for testing purpose.
timer.AutoReset = true;
timer.Enabled = true;
timer.Elapsed += new ElapsedEventHandler(OnTimeBeforeRefreshElapsed);
timer.Start();
}
private void OnTimeBeforeRefreshElapsed(object sender, ElapsedEventArgs e)
{
Dispatcher.CurrentDispatcher.Invoke(new Action(() => { RefreshData(); }));
Dispatcher.CurrentDispatcher.Invoke(new Action(() => { UpdateLayout(); }));
}
private void RefreshData()
{
foreach (object tab in _tabItems) …Run Code Online (Sandbox Code Playgroud) 问题:
每当我尝试在调试器中断开或设置跟踪点时,我们的应用程序和visual studio都会完全冻结.分离调试器后,应用程序继续.
这个问题可能与WPF有关.我们已将WinForm应用程序迁移到WPF.从那以后出现这个问题.但我无法找到导致问题的代码的特定部分.我已经回滚了数百次提交但没有成功.
它也可能与UI线程有关.如果断点设置在远离UI逻辑的某个地方,则应用程序不会冻结,也不会像在UI线程中那样经常冻结.
[ 编辑: ]我在Visual Studio 2010中使用Windows 7. 64位
[ 更新: ]
当Visual Studio挂起,并且我在断点显示之前尝试分离时,消息"无法从一个或多个进程中分离.所有未完成的功能都未完成".但是我已经在调试选项中禁用了所有func评估.我认为我的问题是由无法完成的func_evaluation或者超时的func_evaluation引起的.
有没有办法看到哪个func_evaluation视觉工作室挂起?
例:
class SomeUiViewPresenterExample
{
private Timer m_Timer;
public void Init()
{
m_Timer = new Timer();
m_Timer.Elapsed += ElapsedFoo();
m_Timer.AutoReset = false;
m_Timer.Interval = 200;
}
private void ElapsedFoo(object sender, ElapsedEventArgs elapsedEventArgs)
{
// there is no code inside this method
// On the next line a trace point with "---> ElapsedFoo called" will freeze the debugger
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试过:(没有成功)
我DispatcherTimer在我的代码中运行,每30秒触发一次,从服务器更新系统状态.即使我正在调试我的服务器代码,计时器也会在客户端中触发,因此如果我已经调试了5分钟,我可能会在客户端中进行十几次超时.最后决定我需要修复这个问题从而寻求使一个更加async/ await友好DispatcherTimer.
DispatcherTimer必须是可配置的,无论它是否是可重入的(即如果任务已经运行,则不应该再次运行它)await完成任务IsRunning为UI提供可绑定属性