Home > database >  Automatically send messages every 1 second
Automatically send messages every 1 second

Time:08-17

internal class Program2
{
    public static async Task<int> Main(string[] args)
    {
        Console.WriteLine("String console app");
        var senderObject = MessageActivator.GetSenderObject();
        MessageOptions opts = null;
        Parser.Default.ParseArguments<MessageOptions>(args).WithParsed(opts1 =>
         {
             opts = (MessageOptions)opts1;
          
         }).WithNotParsed(optsx =>
         {
             Environment.Exit(0);
         });
        await Task.Delay(100);
        senderObject.SendMessage(new SyslogMessageOptions()
        {
            SyslogServerHost = opts.SyslogServerHost,
            SyslogServerPort = opts.SyslogServerPort,
            ApplicationName = opts.ApplicationName,
            ProccessorId = opts.ProccessorId,
            MessageId = opts.MessageId,
            Facility = opts.Facility,
            Severity = opts.Severity,
            NetworkProtocols = (SyslogTesterLib.NetworkProtocol)opts.NetworkProtocols,
            MessageFormat = (SyslogTesterLib.SyslogMessageFormat)opts.MessageFormat,
            Message = opts.Message,
            ShouldPrintMessage = opts.ShouldPrintMessage,
            LocalHostName = opts.LocalHostName,
            StructuredData = opts.StructuredData,
            Date = opts.Date,


        }, new BulkMessageOptions()
        {
            TotalNMessageOptions = new TotalNMessageOptions() { TotalMessages=opts.TotalNMessageCount},
            BulkMessageSendType = BulkMessageSendType.OncePerSecond,

        });
        Console.WriteLine("Press any key to close.");
        Console.ReadKey();

        return 0;
    }
}

}

Here I am sending a syslog bulk message with various options.

What I wanted to know is, is it possible for it to automatically send every 1 second. If yes, how can I possibly do that?

There's the codes, in case if it brings of any help.

CodePudding user response:

See the Interval property of Timer object.

https://docs.microsoft.com/en-us/dotnet/api/system.timers.timer.interval?view=net-6.0

Just set your interval and assign your callback to the elapsed event

CodePudding user response:

//send a message every 1 second
var timer = new Timer(1000);
var senderObject = MessageActivator.GetSenderObject();
MessageOptions opts = null;

Parser.Default.ParseArguments<MessageOptions>(args).WithParsed(opts1 =>
{
    opts = (MessageOptions)opts1;
});

timer.Elapsed  = (sender, e) =>
{
    senderObject.SendMessage(new SyslogMessageOptions()
    {
        SyslogServerHost = opts.SyslogServerHost,
        SyslogServerPort = opts.SyslogServerPort,
        ApplicationName = opts.ApplicationName,
        ProccessorId = opts.ProccessorId,
        MessageId = opts.MessageId,
        Facility = opts.Facility,
        Severity = opts.Severity,
        NetworkProtocols = (SyslogTesterLib.NetworkProtocol)opts.NetworkProtocols,
        MessageFormat = (SyslogTesterLib.SyslogMessageFormat)opts.MessageFormat,
        Message = opts.Message,
        ShouldPrintMessage = opts.ShouldPrintMessage,
        LocalHostName = opts.LocalHostName,
        StructuredData = opts.StructuredData,
        Date = opts.Date,
    });
};

timer.Start();
Console.ReadLine();
  •  Tags:  
  • c#
  • Related