zoukankan      html  css  js  c++  java
  • Windows服务中timer组件无法引发相应的tick事件

    微软对此在这两篇文章那个中给出了相应的解决方案,http://support.microsoft.com/kb/820639/zh-cnhttp://support.microsoft.com/kb/842793/zh-cn
    实际上就是在使用Windows.Forms命名空间中的timer无效时则使用System.Timers中的组件,再不行的话使用Threading中的Timer组件。我遇到的是第一种情况,通过使用System.Timers中的Timer,问题解决了。

    症状

    您添加到 Windows 服务 Microsoft WindowsForms 计时器组件。 启用计时器, 然后您设置计时器以引发事件的间隔。 计算机, 上安装 Windows 服务, 然后您启动服务。 不引发 计时器 事件在 Windows 服务。

    注意 : WindowsForms 计时器组件位于 System.Windows.Forms 命名空间中。

    原因

    WindowsForms 计时器组件用于 WindowsForms 环境,不用于服务器环境。 计时器如果在 Windows 服务使用时, 可能不引发事件。

    解决方案

    要解决此问题, 使用服务器计时器时让命名空间 System.Timers 代替 System.Windows.Forms 命名空间。 要这样做, 请按照下列步骤操作:
    1. 在要删除 Service 1 类命令窗口运行以下命令:
    installutil /u WindowsService1.exe
    注意 WindowsService1.exe 位于您的项目文件夹下 bin 文件夹中。 添加路径是您 WindowsService1.exe。
    2. 切换到 Windows 服务项目。
    3. 在 Service 1 设计器窗口, 单击 Timer 1 控件。
    4. 按 DELETE 键从设计器窗口中删除该 Timer 1 控件。
    5. 在工具栏上, 单击 组件 选项卡。
    6. 从工具栏, 将 Timer 控件拖到设计器窗口。
    7. 双击设计器窗口以查看代码窗口上 Timer 1 控件。
    8. Timer 1 控件的 Tick 事件处理程序中移动到的 Timer 1 控件 Elapsed 事件处理程序代码
    9. 移除 Tick 事件处理程序。
    10. 在 生成 菜单上, 单击 BuildSolution@@@ 。
    11. 在命令窗口运行以下命令:
    installutil WindowsService1.exe
    12. 在 管理工具 菜单上, 单击 服务 。
    13. 右击 Service 1 , 然后单击 开始 。
    14. 等待几秒钟, 右击 Service 1 , 然后单击 停止 。
    15. 打开, C:\sample.txt 文件, 然后注意文本。

    以 开始 和 停止 Tick 文本显示。

     

    状态

    此行为是设计使然。

    更多信息

    再现问题的步骤

    1. 启动 MicrosoftVisualStudio.NET。
    2. 通过使用 MicrosoftVisualBasic.NET 或 MicrosoftVisualC # .NET 打开一个新 Windows 服务项目。

    默认情况下, 创建 Service 1 。
    3. 在工具箱中, 单击 WindowsForms 选项卡。
    4. 将一个 Timer 控件从工具箱拖到 Service 1 设计器窗口。
    5. 在设计器窗口, 双击 Timer 1 控件。

    显示 Service 1 代码窗口。
    6. 如果您使用 VisualC # .NET, 将以下语句添加到开头的代码:
    using System.IO;
    7. 以下代码添加到 Service 1 类的 OnStart 过程。

    VisualBasic.NET 代码
    'Set the interval of the timer to 3 seconds.
                Timer1.Interval = 3000
                Timer1.Enabled = True
                'Open the sample.txt file in append mode.
                FileOpen(1, "C:\sample.txt", OpenMode.Append)
                'Print text to the "C:/sample.txt file.
                Print(1, "Start")
                FileClose()
    VisualC # .NET 代码
    //Set the interval of timer to 3 seconds.
                timer1.Interval =3000;
                //Enable the timer.
                timer1.Enabled =true;
                //Append the text to the sample file.
                StreamWriter writer =File.AppendText(@"C:\sample.txt");
                writer.WriteLine("Start");
                writer.Close();
    8. 以下代码添加到 Service 1 类的 OnStop 过程。

    VisualBasic.NET 代码
    'Open the C:\sample.txt file in append mode.
                FileOpen(1, "C:\sample.txt", OpenMode.Append)
                'Print the text 'Stop' to the C:\sample.txt file.
                Print(1, "Stop")
                FileClose()
    VisualC # .NET 代码
    //Append the text Stop to the C:\sample.txt file.
                StreamWriter writer =File.AppendText(@"C:\sample.txt");
                writer.WriteLine("Stop");
                writer.Close();
    9. 将以下代码添加到 Timer 1 组件的 Tick 事件。

    VisualBasic.NET 代码
     'Set the enabled property to false.
                Timer1.Enabled = False
                'Open the C:\sample.txt file in append mode.
                FileOpen(1, "C:\sample.txt", OpenMode.Append)
                'Print the text 'Tick' to the C:\sample.txt file.
                Print(1, "Tick")
                FileClose()
                'Enable the timer.
                Timer1.Enabled = True
    VisualC # .NET 代码
    //Set the enable property to false.
                timer1.Enabled =false;
                //Append the text Tick to the C:\sample.txt file.
                StreamWriter writer =File.AppendText(@"C:\sample.txt");
                writer.WriteLine("Tick");
                writer.Close();
                timer1.Enabled =true;
    10. 在 视图 菜单上, 单击 设计器 。
    11. 右击 设计器 , 然后单击 添加 Installer 。

    默认情况下, ServiceInstaller 1 和 ServiceProcessInstaller1 创建。
    12. ServiceInstaller 1 , 右击, 然后单击 属性 。
    13. 将 DisplayName 属性设置为 Service 1 。

    注意 最好要将 ServiceName 属性设为 Service 1
    14. 右击 ServiceProcessInstaller1 , 然后单击 属性 。
    15. 在属性窗口, ServiceProcessInstaller1 将以 LocalSystem 帐户 属性。
    16. 在 生成 菜单上, 单击 BuildSolution@@@ 。
    17. 开始 , 依次 运行 。
    18. 在运行窗口, 在 打开 框中, 键入 cmd , 然后按 ENTER 键。
    19. 在命令提示符下, 运行以下命令:
    installutil  WindowsService1.exe
    注意 如果使用任何其他服务具有相同名称不安装您 Service 1 服务。
    20. 完成安装步骤后, AdministrativeTools , 控制面板中单击, 然后单击 服务 。
    21. 在服务窗口, 右击 Service 1 , 依次 开始 。
    22. 等待几秒钟, 右击 Service 1 , 然后单击 停止 。
    23. 打开 C:\sample.txt 文件。

    显示文本 StartStop 。 不引发 Timer 1 Tick 事件, 并且不打印到 C:\sample.txt 文件 Tick 文本。

    以下是本人编写的一个例子:
    Program.cs
    Code

    ProjectInstaller.cs
    Code

    ProjectInstaller.Designer.cs
    Code

    Service1.cs
    Code

    Service1.Designer.cs
    Code

    其中,POPPersistent类及其方法OperationEmail()是要关联的程序。
  • 相关阅读:
    【C#】ArcFace2 视频人脸比对教程
    C#二次封装虹软arc研究
    【Linux】Face Recognition的封装
    人脸识别 ArcFace Demo [Windows]
    Python创建一个简单的区块链
    Mac更新系统后提示xcrun error
    Hyperledger Fabric开发(二):创建网络
    Hyperledger Fabric开发(一):环境配置
    mac OS和win7笔记本实现文件共享
    代码行数统计的Java和Python实现
  • 原文地址:https://www.cnblogs.com/greatandforever/p/1239857.html
Copyright © 2011-2022 走看看