English 中文(简体)
为什么是我的制度。 Timers. C#没有发射的时间事件
原标题:Why is my System.Timers.Timer event not firing in C#
  • 时间:2012-01-12 09:59:00
  •  标签:
  • c#
  • timer
System.Timers.Timer timer = new System.Timers.Timer();

private void button1_Click(object sender, EventArgs e)
{
    timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
    timer.Interval = 2000;

    timer.Enabled = true;
    timer.Start();
}

private void timer_Elapsed(object myobject, System.Timers.ElapsedEventArgs e)
{
    Say("Time hit" + DateTime.Now.ToString());
}

什么是失踪?

EDIT:

补充:

<代码>时间。 AutoReset = 真实;

For Saye 方法:

private void Say(string s)
        {

            try
            {
                txtSay.AppendText(s + "
");
            }
            catch
            {
            }
        }

这里的口述方法没有问题。 一切照旧。

最佳回答

I think you didint mentioned that you getting cross thread exception.. try to change code like that:

Invoke(new Action(()=>Say("Time hit" + DateTime.Now.ToString())));
问题回答

......

  1. your Timers.Timer does not set a SyncObject
  2. Therefore your event fires on another thread
  3. Setting the Text property raises a cross-threading Exception
  4. 那么,你就把这一例外推上了,没有任何痕迹。

    private void Say(string s)
    {
        try
        {
            txtSay.AppendText(s + "
    ");
        }
        catch  // empty catch-block: the real fundamental problem
        {
        }
    }
    

因此,时事的确发生火灾,但会产生一个仍然隐藏的错误。

短期、实际的解决办法是使用Windows。 全球倡议相关时间。

这可能不是正确的,但根据MSDN的榜样,你不需要打上t.Start(......),应该开始。 既然你开始使用t.Enabled和t.Start,就可能与AutoRest财产发生某种冲突。

I would also make sure the Say method works like it should by adding a break point or something similar. I have no clue what you are calling AppendText on in this case.

一小部分猜测工作。

You needn t set the eventHandler in the Cak Solutions. 比如,在贵阶层的构造中确定,并将工作。

离开起步 点击功能的方法:

private void button1_Click(object sender, EventArgs e)
{
    timer.Start();
}

正如Reniuz所说,你很可能成为一种例外,因为你试图从不同的线上更新国际调查。 <代码>Timer等值互为衔接,以防止其干预您的申请。

这正是为什么<代码>try{ /* , */ } Collection{<>/code>是一个坏的主意——你用书面方式完全掩盖错误。 如果你没有书面的话,你也许能够指出,你需要从例外信息中援引最新信息。

在C++/CLI(使用C++管理)。 NET——实际上已经关闭到C#上,你应当反对在那个时间上同步。

它可以这样看待:+-

timer->SynchronizingObject = txtSay;

可在C#中:>,而不是->

artice





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...