简体   繁体   English

在计时器启动之前执行功能

[英]Execute a Function before a Timer start

I am a bit new Here .I have window service in which I have a timer which executes a function after 1 Min interval..I want to Execute function first time before Timer start and then after each Timer Interval... 我在这里有点新。我有窗口服务,其中有一个计时器,该计时器在1分钟间隔后执行一个功能。.我想在计时器启动之前第一次执行功能,然后在每个计时器间隔之后执行...

here is My Code : 这是我的代码:

public partial class ASMSFetchService : ServiceBase
{
    System.Timers.Timer updateAutoSMSTimer;

    public ASMSFetchService()
    {
        InitializeComponent();

    }

    protected override void OnStart(string[] args)
    {
        updateAutoSMSTimer = new System.Timers.Timer(1 * 60 * 1000);
        updateAutoSMSTimer.Elapsed += new System.Timers.ElapsedEventHandler(Slots);
        updateAutoSMSTimer.Enabled = true;
        updateAutoSMSTimer.AutoReset = true;
        updateAutoSMSTimer.Start();
     }

    private void Slots(object sender, ElapsedEventArgs e)
    {method1();}

    private void method1()
    { //SomeOpeartion }
}

The function is executing when the 1 timer interval complete ...I want to call the method1() before timer start and then after every Timer interval .... 该函数在1个计时器间隔完成时正在执行...我想在计时器启动之前然后在每个计时器间隔之后调用method1()....

I have try to put method in the Start() and in the constructor() ..But it is not working ...I am not sure ..But Can it be possible ??? 我试图将方法放在Start()和Constructor()中..但是它不起作用...我不确定..但是有可能吗?

Any suggestion will be Helpful 任何建议都会有帮助

I want to call the method1() before timer start and then after every Timer interval 我想在计时器启动之前然后在每个计时器间隔之后调用method1()

Then call it before you start the timer 在启动计时器之前先调用它

method1();
updateAutoSMSTimer.Start();

try to call if before start then : 尝试在开始之前致电,然后:

 protected override void OnStart(string[] args)
        {
            updateAutoSMSTimer = new System.Timers.Timer(1 * 60 * 1000);
            updateAutoSMSTimer.Elapsed += new System.Timers.ElapsedEventHandler(Slots);
            updateAutoSMSTimer.Enabled = true;
            updateAutoSMSTimer.AutoReset = true;
            method1();
            updateAutoSMSTimer.Start();
         }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何开始在WPF应用中执行计时器功能 - How to start performing timer function in WPF app 在Unity 5中如何在多人游戏开始之前设置计时器? - How to make timer before multiplayer game start in unity 5.? function 'Function1' 的侦听器在运行计时器时无法启动 function - The listener for function 'Function1' was unable to start when running a timer function 是否可以通过顺序执行来执行 Azure 函数定时器触发器? - Is it possible to execute Azure function Timer trigger with Sequential Execution? 在执行被调用函数之前执行代码 - Execute code before the called function is executed WPF在特定事件发生之前执行功能吗? - WPF execute a function before a particular event occurs? 在使用计时器执行功能之前等待x小时不起作用 - waiting for x hours before executing a function using timer does not work 计时器(线程)在等待任务重新执行之前会完成任务吗? - Would a Timer (Threading) finish execute his task before the waiting period for the task re-execution? 在执行下一行代码之前,如何在不使线程进入睡眠状态的情况下等待计时器完成? - How can I wait for a timer to finished before I execute next line of code without putting the thread to sleep? C# 中的计时器用于启动警报 - 防止其在上午 8 点之前启动的条件 - Timer in C# to start an alarm - condition to prevent it from starting before 8am
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM