C# WP7 单例定时器

发布于 2024-09-11 19:02:33 字数 579 浏览 0 评论 0原文

我的 WP7 应用程序中有一个单例计时器,但是我不知道如何让它在每次计时器滴答时更新文本块...有没有办法获取计时器滴答的事件处理程序,然后用正确的值更新文本框时间?

这是我尝试使用但不起作用的内容:

public _1()
    {
        InitializeComponent();
        Singleton.TimerSingleton.Timer.Tick += new EventHandler(SingleTimer_Tick);
    }

    void SingleTimer_Tick(object sender)
    {
        textBlock1.Text = Singleton.TimerSingleton.TimeElapsed.TotalSeconds.ToString();
    }

这是我的 Timer.cs SingleTon:

http://tech-fyi.net/code/timer.cs.txt

I have a singleton timer in my WP7 application however I have no idea how to get it to update a textblock everytime the timer ticks... Is there a way to get the event handler of the timer ticking and then update the textbox with the correct time?

Here is what I tried to use but wouldn't work:

public _1()
    {
        InitializeComponent();
        Singleton.TimerSingleton.Timer.Tick += new EventHandler(SingleTimer_Tick);
    }

    void SingleTimer_Tick(object sender)
    {
        textBlock1.Text = Singleton.TimerSingleton.TimeElapsed.TotalSeconds.ToString();
    }

Here is my Timer.cs SingleTon:

http://tech-fyi.net/code/timer.cs.txt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花伊自在美 2024-09-18 19:02:33
void SingleTimer_Tick(object sender)

上面的方法应该类似于

void SingleTimer_Tick(object sender, EventArgs e)

当您提出问题时,请正确使用您的术语并提供更多详细信息。它将帮助您更快地获得正确答案。例如,当您说“应用程序不会让我调用...”时,您实际上的意思是编译器给您一个错误。

void SingleTimer_Tick(object sender)

The method above should be something like

void SingleTimer_Tick(object sender, EventArgs e)

And when you ask a question please get your terminology right and give more details. It'll help you get the right answer faster. For example when you say "the application won't let me call ..." what you actually mean is the compiler gives you an error.

浅笑依然 2024-09-18 19:02:33

SingleTimer_Tick 方法在非 GUI 线程上执行。调用

textBlock1.Invoke(() => textBlock1.Text = Singleton.TimerSingleton.TimeElapsed.TotalSeconds.ToString());

The method SingleTimer_Tick gets executed on a non GUI thread. Call

textBlock1.Invoke(() => textBlock1.Text = Singleton.TimerSingleton.TimeElapsed.TotalSeconds.ToString());

枕头说它不想醒 2024-09-18 19:02:33

我不确定你的计时器是什么类型。有些用于在线程池线程上引发事件,其他则用于在 GUI 线程上引发事件。如果您可以告诉我们您的计时器的类型(System.Timers.Timer、System.Threading.Timer 等),那么我可以告诉您它的用途。但是,如果我没记错的话,Tick 用于 GUI 计时器,而 Elapsed 用于其他计时器 - 我可能是错的。

冒着光顾你的风险,我会问你是否已经启动了计时器:)通常调用 Start() 或将 Enabled 设置为 true 将启动计时器对象。如果您希望计时器已经在运行,您应该能够在连接事件之前检查诸如“Enabled”之类的属性来断言它正在运行。

除此之外,我会进行一些 printf 样式的调试来检查事件是否确实被引发。

I'm not sure which type your timer is. Some are used to raise events on a thread pool thread, others on the GUI thread. If you could tell us the type of your timer (System.Timers.Timer, System.Threading.Timer etc.) then I can tell you what it's used for. However, if I remember correctly, Tick is used for the GUI timer whereas Elapsed is used for other timers - I might be wrong.

At the risk of patronizing you, I'm going to ask if you've started your timer or not :) Usually a call to Start() or setting Enabled to true will kick off a timer object. If you expect the timer to already be running you should be able to check a property such as Enabled to assert that it's running before you hook up the event.

Apart from that I'd do some printf style debugging to check that the event's actually being raised.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文