gpt4 book ai didi

wpf - DispatcherTimer 在 WPF 应用程序中未触发

转载 作者:行者123 更新时间:2023-12-03 01:53:40 24 4
gpt4 key购买 nike

我试图理解为什么 SingletonWithTimer 中包含的 DispatcherTimer 在以下 WPF 应用程序中没有触发。我已经研究了几天,但似乎无法弄清楚它的真相。该应用程序是我正在尝试修复的现有应用程序的减少的基本部分。该项目的 Startup 对象是 WPFApplication5TimerTest.Program

控制台的输出如下,问题很明显,因为输出中没有显示“TimerTick”一词:

Timer is initialized
'WpfApplication5TimerTest.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework.Aero\v4.0_4.0.0.0__31bf3856ad364e35\PresentationFramework.Aero.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Sample thread
Sample thread
Sample thread
Sample thread
Sample thread
Sample thread
The thread '<No Name>' (0x10b0) has exited with code 0 (0x0).
Sample thread exiting!

这是 Program.cs:

using System;

namespace WpfApplication5TimerTest
{
static class Program
{
[STAThread]
static void Main(string[] args)
{
AppObject = new App();
AppObject.Run();
}

public static App AppObject
{
get;
private set;
}
}
}

这是App.xaml.cs:

using System;
using System.Threading;
using System.Windows;

namespace WpfApplication5TimerTest
{
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
var sampleThread = new Thread(new ThreadStart(SampleThreadEntryPoint));
sampleThread.Start();
new MainWindow().Show();
}

private void SampleThreadEntryPoint()
{
SingletonWithTimer.Initialize();
while (!_shutdownEvent.WaitOne(1000))
Console.WriteLine("Sample thread");
Console.WriteLine("Sample thread exiting!");
}

protected override void OnExit(ExitEventArgs e)
{
_shutdownEvent.Set();
}

private ManualResetEvent _shutdownEvent = new ManualResetEvent(false);
}
}

这是MainWindow.xaml.cs:

using System;
using System.Windows;

namespace WpfApplication5TimerTest
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void Window_Closed(object sender, EventArgs e)
{
Program.AppObject.Shutdown();
}
}
}

这是 SingletonWithTimer.cs:

using System;
using System.Windows.Threading;

namespace WpfApplication5TimerTest
{
public class SingletonWithTimer
{
private static SingletonWithTimer Instance
{
get
{
if (_instance == null)
{
_instance = new SingletonWithTimer();
}
return _instance;
}
}

public static void Initialize()
{
SingletonWithTimer.Instance._timer = new DispatcherTimer();
SingletonWithTimer.Instance._timer.Interval = TimeSpan.FromSeconds(2);
SingletonWithTimer.Instance._timer.Tick += new EventHandler(SingletonWithTimer.Instance.OnTimerTick);
SingletonWithTimer.Instance._timer.Start();
Console.WriteLine("Timer is initialized");
}

private void OnTimerTick(object sender, EventArgs e)
{
Console.WriteLine("TimerTick");
}

private static SingletonWithTimer _instance;
private DispatcherTimer _timer = null;
}
}

最佳答案

这是因为您在另一个(非 UI)线程上创建了 DispatcherTimer。因此,它将绑定(bind)到该线程上的调度程序,而不是 UI 线程上的调度程序。由于该线程上没有任何东西正在运行 Dispatcher,因此它永远不会触发。

在 UI 线程上创建 DispatcherTimer,或者使用 constructor overload允许您传入特定的 Dispatcher 来使用。

关于wpf - DispatcherTimer 在 WPF 应用程序中未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8423811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com