【问题标题】:.NET Core Windows Forms app: can't find System.Timers.Timer in visual studio 2019 toolbox.NET Core Windows Forms 应用程序:在 Visual Studio 2019 工具箱中找不到 System.Timers.Timer
【发布时间】:2021-09-02 16:01:42
【问题描述】:

在 Visual Studio 2019 中启动应用。它是使用 .NET Core 5 的 Windows 窗体应用。 我以前使用过 System.Timers.Timer,发现它的抖动很低。 但是,我无法在 Visual Studio 工具箱中找到它。 All Windows Forms 下有一个计时器:System.Windows.Forms.Timer Components下还有一个定时器,也是System.Windows.Forms.Timer。 这个计时器不可靠。刻度的数量与指定的数量相差很大。 Fallback 是手动构造一个 System.Timers.Timer。 查看我以前的应用程序,虽然它来自工具箱。 任何建议表示赞赏!

【问题讨论】:

  • 您是否正在尝试运行速度测试?你用过秒表吗?速度测试很方便。
  • 我认为System.Timers.Timer 从未出现在 WinForm 的设计器工具箱中。
  • Andrew Reese:在滴答处理程序中有一个状态机。我一分钟需要大约 20 个滴答声。 LarsTech:这是上一个应用程序的代码部分:(在 Designer.cs 中)((System.ComponentModel.ISupportInitialize)(this.timerSys)).BeginInit();在:#region Windows 窗体设计器生成的代码 /// /// 设计器支持所需的方法 - 请勿使用代码编辑器修改 /// 此方法的内容。 /// 恐怕构建会踢出我的手动插入。
  • 您始终可以在设计器代码后面的区域InitializeComponent 方法之外添加一个System.Timers.Timer 对象。
  • @LarsTech docs.microsoft.com/en-us/archive/msdn-magazine/2004/february/… 说它曾经是(我也不记得了)

标签: c# visual-studio-2019 toolbox system.timers.timer


【解决方案1】:

正如其他人所说,在工具箱中找不到System.Timers.Timer

因为它不继承自 System.Windows.Forms.Control 类。它不是窗体控件。

您可以参考以下代码了解如何在winforms中使用。

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }
        System.Timers.Timer timer;
        private void button1_Click(object sender, EventArgs e)
        {
            timer.Start();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer = new System.Timers.Timer(1000);
            timer.Elapsed += Timer_Elapsed;

        }

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            richTextBox1.AppendText("Hello, world"+Environment.NewLine);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer.Stop();
        }
    }

结果:

【讨论】:

  • 请提供一些理由来关闭跨线程检查,而不是采用 Microsft 提倡的在非线程创建的场景中访问控件的途径之一(例如调用或设置 SyncronizingObject属性)
  • 谢谢。我能够将相关代码放在 te form1 构造函数中。它工作正常。我仍然认为/记得在某些时候可以将其从工具箱/组件中拉出来。
  • @HBC531,我已经解释了为什么在工具箱中找不到它。请再次检查。我认为如果你在使用时可以将计时器从工具箱中拉出,那是没有影响的。
猜你喜欢
  • 2021-12-06
  • 1970-01-01
  • 1970-01-01
  • 2020-08-27
  • 1970-01-01
  • 2016-05-19
  • 2021-07-28
  • 2017-04-07
  • 2021-06-25
相关资源
最近更新 更多