首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >每1秒自动发送消息

每1秒自动发送消息
EN

Stack Overflow用户
提问于 2022-08-17 08:53:26
回答 2查看 106关注 0票数 2
代码语言:javascript
复制
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,
            ProcessorId = opts.ProcessorId,
            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;
    }
}

在这里,我发送了一个带有各种选项的syslog大容量消息。

我想知道的是,它是否有可能每1秒自动发送一次。如果是的话,我怎么可能做到呢?

这是密码,以防有任何帮助。

EN

回答 2

Stack Overflow用户

发布于 2022-08-17 09:04:02

请参阅Timer对象的Interval属性。

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

只需设置您的间隔,并将回调分配给经过的事件。

票数 2
EN

Stack Overflow用户

发布于 2022-08-17 09:19:33

代码语言:javascript
复制
//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,
        ProcessorId = opts.ProcessorId,
        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();
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73385554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档