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秒自动发送一次。如果是的话,我怎么可能做到呢?
这是密码,以防有任何帮助。
发布于 2022-08-17 09:04:02
请参阅Timer对象的Interval属性。
https://learn.microsoft.com/en-us/dotnet/api/system.timers.timer.interval?view=net-6.0
只需设置您的间隔,并将回调分配给经过的事件。
发布于 2022-08-17 09:19:33
//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();https://stackoverflow.com/questions/73385554
复制相似问题