1

I've created a console application that needs to run in the background of my client application and has to invoke a method after a certain period of time. So far I've tried the below with little success but it doesn't seem to be working.

Furthermore, in a situation where I wanted to use the same timer to invoke more than one method, then what would I do?

class Program
{
    static void Main(string[] args)
    {
        Console.ReadLine();
        const bool generate = true;
        NewBookingTimer(generate);
    }

    public static void NewBookingTimer(bool generate)
    {
        if (!generate) return;
        var newBookingTimer = new Timer();
        newBookingTimer.Elapsed += (sender, e) => NewBooking();
        var random = new Random();
        int randomNumber = random.Next(0, 500);
        newBookingTimer.Interval = randomNumber;
        newBookingTimer.Enabled = true;
    }

    public static void NewBooking()
    {
        var dbObject = new DbConnect();
        dbObject.OpenConnection();
        var bookingObject = new GenerateBooking();
        bookingObject.NewBooking();
    }
}
Noctis
  • 11,507
  • 3
  • 43
  • 82
methuselah
  • 12,766
  • 47
  • 165
  • 315
  • sounds like you will need to have some more variables to setup as boolean flags.. also you will need a `timer.Start` and a timer.Stop` call stepping thru your code on your end would tell you the when and where in that regard.. also can you elaborate more on what you mean when you are speaking about the same timer to invoke more than one method..? It almost sounds like you would want to implement some sort of while not true loop along with some Thread.Sleep() method calls – MethodMan Jan 30 '15 at 22:41
  • Which timer it? System.Timers.Timer, System.Threading.Timer or System.Windows.Forms.Timer? I think you are using the first one, but just to check. – Sjips Jan 30 '15 at 22:49
  • @Sjips - using System.Timers; – methuselah Jan 30 '15 at 22:51

2 Answers2

1

He he ... Try this:

static void Main(string[] args)
{
    const bool generate = true;
    NewBookingTimer(generate);
    Console.ReadLine();
}

The way you do it, it's waiting for you to enter a line , and then it closes ... so obviously, it won't do anything.

Turning them around, it'll fire your events, and run until you hit Enter :)


Regarding the multiple calls, Do you want it to call Method_A and Methdod_B every time it fires ? You can just have both of them in the event that you raise. Otherwise, do explain better what you want.


Here's your code slightly modified:

static void Main(string[] args)
{
    const bool generate = true;
    NewBookingTimer(generate);
    Console.ReadLine();
}

public static void NewBookingTimer(bool generate)
{
    if (!generate) return;
    var newBookingTimer = new Timer();
    newBookingTimer.Elapsed += (sender, e) => NewBooking();
    newBookingTimer.Elapsed += (sender, e) => OldBooking();
    var random = new Random();
    int randomNumber = random.Next(0, 500);
    Console.Out.WriteLine("Random = " + randomNumber);
    newBookingTimer.Interval = randomNumber;
    newBookingTimer.Enabled = true;
}

public static void NewBooking()
{
    Console.Out.WriteLine("this is NEW booking");
}

public static void OldBooking()
{
    Console.Out.WriteLine("this is OLD booking");
}

and here's the result:
enter image description here

Noctis
  • 11,507
  • 3
  • 43
  • 82
  • thanks, but do you think that refactoring my solution in this manner makes it more elegant? - http://pastebin.com/NwsiSNwL – methuselah Jan 30 '15 at 22:50
  • I'd like to call `Method_A` and `Method_B` each time it fires – methuselah Jan 30 '15 at 22:50
  • Just tried the above and it loads up the console for one second and then it closes it immediately – methuselah Jan 30 '15 at 22:52
  • I don't know what your constraints are, but if you'll move the `ReadLine` to the end of the `Main`, your code will work. Having said that, have a look at [this question](http://stackoverflow.com/q/1416803/1698987) , they suggest using the thread timer, not the timer timer, and explain a bit more about the differences between them. – Noctis Jan 30 '15 at 22:53
1

This code executes the 2 methods on the same timer. The only trick was the multi-line lambda timer handler.

class Program
{
    static void Main(string[] args)
    {
        Console.ReadLine();
        const bool generate = true;
        NewBookingTimer(generate);
        Console.WriteLine("Running...");
        Console.ReadLine();
    }

    public static void NewBookingTimer(bool generate)
    {
        if (!generate) return;
        var newBookingTimer = new Timer();
        newBookingTimer.Elapsed += (sender, e) =>
        {
            MethodA();
            MethodB();
        };
        var random = new Random();
        int randomNumber = random.Next(0, 500);
        newBookingTimer.Interval = randomNumber;
        newBookingTimer.Enabled = true;
    }

    public static void MethodA()
    {
        Console.WriteLine("in method A");
    }

    public static void MethodB()
    {
        Console.WriteLine("in method B");
    }
}
djv
  • 15,168
  • 7
  • 48
  • 72