如何在 C# 中的 x 秒后退出或关闭应用程序?
How to exit or close the application after x second in C#?
实际上我在 Whosebug 中找到了一些类似的东西,但它对我不起作用。他们想在给定时间 (23:00) 退出他们的应用程序。我想在 3000 秒后退出它。对我来说,答案很有帮助,现在它正在运行,重点是,重要的是,将这段代码放在程序中的什么位置。
此代码正在管理多波束 RFID reader 以定位标签。它将为您提供 x 和 y 坐标(以及一些其他信息 - EPC、时间戳、报告类型等)。实际上该程序运行正常,但我只想 运行 它 5 分钟。我正在使用不同的设置测量 Reader 的准确性,如果我的所有测量都是 3000 秒(或其他),它会更具可比性。现在,如果我按下回车键,程序就会退出。
我一直在搜索和尝试很多,但它不想为我工作。我希望有人能在这里提供帮助。
代码如下:
using System;
using Impinj.OctaneSdk;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace OctaneSdkExamples
{
class Program
{
// Create an instance of the ImpinjReader class.
static ImpinjReader reader = new ImpinjReader();
static void Main(string[] args)
{
try
{
// Connect to the reader.
// Change the ReaderHostname constant in SolutionConstants.cs
// to the IP address or hostname of your reader.
reader.Connect(SolutionConstants.ReaderHostname);
// Assign the LocationReported event handler.
// This specifies which method to call
// when a location report is available.
reader.LocationReported += OnLocationReported;
// Get the default settings
// We'll use these as a starting point
// and then modify the settings we're
// interested in.
Settings settings = reader.QueryDefaultSettings();
// Put the xArray into location mode
settings.SpatialConfig.Mode = SpatialMode.Location;
// Enable all three report types
settings.SpatialConfig.Location.EntryReportEnabled = true;
settings.SpatialConfig.Location.UpdateReportEnabled = true;
settings.SpatialConfig.Location.ExitReportEnabled = true;
// Set xArray placement parameters
// The mounting height of the xArray, in centimeters
settings.SpatialConfig.Placement.HeightCm = 100
;
// These settings aren't required in a single xArray environment
// They can be set to zero (which is the default)
settings.SpatialConfig.Placement.FacilityXLocationCm = 0;
settings.SpatialConfig.Placement.FacilityYLocationCm = 0;
settings.SpatialConfig.Placement.OrientationDegrees = 0;
// Set xArray location parameters
settings.SpatialConfig.Location.ComputeWindowSeconds = 10;
settings.ReaderMode = ReaderMode.AutoSetDenseReader;
settings.Session = 2;
settings.SpatialConfig.Location.TagAgeIntervalSeconds = 20;
// Specify how often we want to receive location reports
settings.SpatialConfig.Location.UpdateIntervalSeconds = 5;
// Set this to true if the maximum transmit power is desired, false if a custom value is desired
settings.SpatialConfig.Location.MaxTxPower = false;
// If MaxTxPower is set to false, then a custom power can be used. Provide a power in .25 dBm increments
settings.SpatialConfig.Location.TxPowerInDbm = 23.00;
// Disable antennas targeting areas from which we may not want location reports,
// in this case we're disabling antennas 10 and 15
List<ushort> disabledAntennas = new List<ushort> { };
settings.SpatialConfig.Location.DisabledAntennaList = disabledAntennas;
// Uncomment this is you want to filter tags
/*
// Setup a tag filter.
// Only the tags that match this filter will respond.
// We want to apply the filter to the EPC memory bank.
settings.Filters.TagFilter1.MemoryBank = MemoryBank.Epc;
// Start matching at the third word (bit 32), since the
// first two words of the EPC memory bank are the
// CRC and control bits. BitPointers.Epc is a helper
// enumeration you can use, so you don't have to remember this.
settings.Filters.TagFilter1.BitPointer = BitPointers.Epc;
// Only match tags with EPCs that start with "3008"
settings.Filters.TagFilter1.TagMask = "3008";
// This filter is 16 bits long (one word).
settings.Filters.TagFilter1.BitCount = 16;
// Set the filter mode. Use only the first filter
settings.Filters.Mode = TagFilterMode.OnlyFilter1;
*/
// Apply the newly modified settings.
reader.ApplySettings(settings);
// Start the reader
reader.Start();
timer1_Tick(3000);
// Wait for the user to press enter.
Console.WriteLine("Press enter to exit");
Console.ReadLine();
// Apply the default settings before exiting.
reader.ApplyDefaultSettings();
// Disconnect from the reader.
reader.Disconnect();
}
catch (OctaneSdkException e)
{
// Handle Octane SDK errors.
Console.WriteLine("Octane SDK exception: {0}", e.Message);
}
catch (Exception e)
{
// Handle other .NET errors.
Console.WriteLine("Exception : {0}", e.Message);
}
}
// This event handler will be called when a location report is ready.
static void OnLocationReported(ImpinjReader reader, LocationReport report)
{
// Print out the report details
Console.WriteLine("Location report");
Console.WriteLine(" Type = {0}", report.ReportType);
Console.WriteLine(" EPC = {0}", report.Epc);
Console.WriteLine(" X = {0} cm", report.LocationXCm);
Console.WriteLine(" Y = {0} cm", report.LocationYCm);
Console.WriteLine(" Timestamp = {0} ({1})", report.Timestamp, report.Timestamp.LocalDateTime);
Console.WriteLine(" Read count = {0}", report.ConfidenceFactors.ReadCount);
// Saving data
string path = @"b:\Master Thesis\xArray\Tests\Test 3 - Height-100cm, Disabled Antennas - , TxPowerInDbm-23.25, UpdateIntervalSeconds-5, ComputeWindowSeconds-10, ReaderMode_AutoSetDenseReader, TagAgeIntervalSeconds-20, tags 40 cm from origo .txt";
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Type, Epc, X Localization, Y localization, Timestamp, Local Date Time, Read Count ");
// This text is added only once to the file.
}
}
// This text is always added, making the file longer over time
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine(" {0} , {1} , {2} , {3} , {4} , {5} , {6} ", report.ReportType, report.Epc, report.LocationXCm, report.LocationYCm, report.Timestamp, report.Timestamp.LocalDateTime, report.ConfidenceFactors.ReadCount);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
reader.Disconnect();
}
}
}
我认为您的问题是如何在计时器事件处理程序中结束控制台应用程序。 Environment.Exit(0)
.
怎么样
public static void Main(String[] args)
{
Timer timer = new Timer();
timer.Interval = 10000;
timer.Elapsed += Timer_Elapsed;timer.Start();
Console.ReadKey();
}
private static void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
Environment.Exit(0);
}
如果您只想让您的应用程序在 5 分钟后关闭,您可以这样做:
static void Main()
{
System.Timers.Timer timer = new System.Timers.Timer(300000);
timer.Elapsed += Timer_Elapsed;
timer.Start();
//DO WHATEVER YOU WANT HERE
}
private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Application.Exit();
}
实际上我在 Whosebug 中找到了一些类似的东西,但它对我不起作用。他们想在给定时间 (23:00) 退出他们的应用程序。我想在 3000 秒后退出它。对我来说,答案很有帮助,现在它正在运行,重点是,重要的是,将这段代码放在程序中的什么位置。
此代码正在管理多波束 RFID reader 以定位标签。它将为您提供 x 和 y 坐标(以及一些其他信息 - EPC、时间戳、报告类型等)。实际上该程序运行正常,但我只想 运行 它 5 分钟。我正在使用不同的设置测量 Reader 的准确性,如果我的所有测量都是 3000 秒(或其他),它会更具可比性。现在,如果我按下回车键,程序就会退出。
我一直在搜索和尝试很多,但它不想为我工作。我希望有人能在这里提供帮助。
代码如下:
using System;
using Impinj.OctaneSdk;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace OctaneSdkExamples
{
class Program
{
// Create an instance of the ImpinjReader class.
static ImpinjReader reader = new ImpinjReader();
static void Main(string[] args)
{
try
{
// Connect to the reader.
// Change the ReaderHostname constant in SolutionConstants.cs
// to the IP address or hostname of your reader.
reader.Connect(SolutionConstants.ReaderHostname);
// Assign the LocationReported event handler.
// This specifies which method to call
// when a location report is available.
reader.LocationReported += OnLocationReported;
// Get the default settings
// We'll use these as a starting point
// and then modify the settings we're
// interested in.
Settings settings = reader.QueryDefaultSettings();
// Put the xArray into location mode
settings.SpatialConfig.Mode = SpatialMode.Location;
// Enable all three report types
settings.SpatialConfig.Location.EntryReportEnabled = true;
settings.SpatialConfig.Location.UpdateReportEnabled = true;
settings.SpatialConfig.Location.ExitReportEnabled = true;
// Set xArray placement parameters
// The mounting height of the xArray, in centimeters
settings.SpatialConfig.Placement.HeightCm = 100
;
// These settings aren't required in a single xArray environment
// They can be set to zero (which is the default)
settings.SpatialConfig.Placement.FacilityXLocationCm = 0;
settings.SpatialConfig.Placement.FacilityYLocationCm = 0;
settings.SpatialConfig.Placement.OrientationDegrees = 0;
// Set xArray location parameters
settings.SpatialConfig.Location.ComputeWindowSeconds = 10;
settings.ReaderMode = ReaderMode.AutoSetDenseReader;
settings.Session = 2;
settings.SpatialConfig.Location.TagAgeIntervalSeconds = 20;
// Specify how often we want to receive location reports
settings.SpatialConfig.Location.UpdateIntervalSeconds = 5;
// Set this to true if the maximum transmit power is desired, false if a custom value is desired
settings.SpatialConfig.Location.MaxTxPower = false;
// If MaxTxPower is set to false, then a custom power can be used. Provide a power in .25 dBm increments
settings.SpatialConfig.Location.TxPowerInDbm = 23.00;
// Disable antennas targeting areas from which we may not want location reports,
// in this case we're disabling antennas 10 and 15
List<ushort> disabledAntennas = new List<ushort> { };
settings.SpatialConfig.Location.DisabledAntennaList = disabledAntennas;
// Uncomment this is you want to filter tags
/*
// Setup a tag filter.
// Only the tags that match this filter will respond.
// We want to apply the filter to the EPC memory bank.
settings.Filters.TagFilter1.MemoryBank = MemoryBank.Epc;
// Start matching at the third word (bit 32), since the
// first two words of the EPC memory bank are the
// CRC and control bits. BitPointers.Epc is a helper
// enumeration you can use, so you don't have to remember this.
settings.Filters.TagFilter1.BitPointer = BitPointers.Epc;
// Only match tags with EPCs that start with "3008"
settings.Filters.TagFilter1.TagMask = "3008";
// This filter is 16 bits long (one word).
settings.Filters.TagFilter1.BitCount = 16;
// Set the filter mode. Use only the first filter
settings.Filters.Mode = TagFilterMode.OnlyFilter1;
*/
// Apply the newly modified settings.
reader.ApplySettings(settings);
// Start the reader
reader.Start();
timer1_Tick(3000);
// Wait for the user to press enter.
Console.WriteLine("Press enter to exit");
Console.ReadLine();
// Apply the default settings before exiting.
reader.ApplyDefaultSettings();
// Disconnect from the reader.
reader.Disconnect();
}
catch (OctaneSdkException e)
{
// Handle Octane SDK errors.
Console.WriteLine("Octane SDK exception: {0}", e.Message);
}
catch (Exception e)
{
// Handle other .NET errors.
Console.WriteLine("Exception : {0}", e.Message);
}
}
// This event handler will be called when a location report is ready.
static void OnLocationReported(ImpinjReader reader, LocationReport report)
{
// Print out the report details
Console.WriteLine("Location report");
Console.WriteLine(" Type = {0}", report.ReportType);
Console.WriteLine(" EPC = {0}", report.Epc);
Console.WriteLine(" X = {0} cm", report.LocationXCm);
Console.WriteLine(" Y = {0} cm", report.LocationYCm);
Console.WriteLine(" Timestamp = {0} ({1})", report.Timestamp, report.Timestamp.LocalDateTime);
Console.WriteLine(" Read count = {0}", report.ConfidenceFactors.ReadCount);
// Saving data
string path = @"b:\Master Thesis\xArray\Tests\Test 3 - Height-100cm, Disabled Antennas - , TxPowerInDbm-23.25, UpdateIntervalSeconds-5, ComputeWindowSeconds-10, ReaderMode_AutoSetDenseReader, TagAgeIntervalSeconds-20, tags 40 cm from origo .txt";
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Type, Epc, X Localization, Y localization, Timestamp, Local Date Time, Read Count ");
// This text is added only once to the file.
}
}
// This text is always added, making the file longer over time
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine(" {0} , {1} , {2} , {3} , {4} , {5} , {6} ", report.ReportType, report.Epc, report.LocationXCm, report.LocationYCm, report.Timestamp, report.Timestamp.LocalDateTime, report.ConfidenceFactors.ReadCount);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
reader.Disconnect();
}
}
}
我认为您的问题是如何在计时器事件处理程序中结束控制台应用程序。 Environment.Exit(0)
.
public static void Main(String[] args)
{
Timer timer = new Timer();
timer.Interval = 10000;
timer.Elapsed += Timer_Elapsed;timer.Start();
Console.ReadKey();
}
private static void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
Environment.Exit(0);
}
如果您只想让您的应用程序在 5 分钟后关闭,您可以这样做:
static void Main()
{
System.Timers.Timer timer = new System.Timers.Timer(300000);
timer.Elapsed += Timer_Elapsed;
timer.Start();
//DO WHATEVER YOU WANT HERE
}
private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Application.Exit();
}