如何在C#中的x秒后退出或关闭应用程序?

如何在C#中的x秒后退出或关闭应用程序?,c#,visual-studio,time,timer,exit,C#,Visual Studio,Time,Timer,Exit,事实上,我在stackoverflow中发现了类似的东西,但它对我不起作用。他们希望在给定的时间(23:00)内退出应用程序。我想在3000秒后退出。对我来说,答案是有帮助的,现在它起作用了,主要的一点是,它很重要,你把这些代码放在你的程序中 该代码管理一个多波束RFID阅读器来定位标签。它将为您提供x和y坐标(以及一些其他信息-EPC、时间戳、报告类型等)。实际上这个程序运行正常,但我只想运行5分钟。我正在用不同的设置测量阅读器的准确度,如果我所有的测量值都是3000秒(或任何时间),那么会更

事实上,我在stackoverflow中发现了类似的东西,但它对我不起作用。他们希望在给定的时间(23:00)内退出应用程序。我想在3000秒后退出。对我来说,答案是有帮助的,现在它起作用了,主要的一点是,它很重要,你把这些代码放在你的程序中

该代码管理一个多波束RFID阅读器来定位标签。它将为您提供x和y坐标(以及一些其他信息-EPC、时间戳、报告类型等)。实际上这个程序运行正常,但我只想运行5分钟。我正在用不同的设置测量阅读器的准确度,如果我所有的测量值都是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(); 
        }

    }


}
使用系统;
使用Impinj.OctaneSdk;
使用System.Collections.Generic;
使用System.IO;
使用系统文本;
名称空间Octanesdk示例
{
班级计划
{
//创建ImpinjReader类的实例。
静态IMPINREADER=新IMPINREADER();
静态void Main(字符串[]参数)
{
尝试
{
//连接到读卡器。
//更改SolutionConstants.cs中的ReaderHostname常量
//读卡器的IP地址或主机名。
reader.Connect(SolutionConstants.ReaderHostname);
//分配LocationReported事件处理程序。
//这指定要调用的方法
//当位置报告可用时。
reader.LocationReported+=OnLocationReported;
//获取默认设置
//我们将使用这些作为起点
//然后修改我们正在进行的设置
//对……感兴趣。
Settings=reader.QueryDefaultSettings();
//将xArray置于定位模式
settings.SpatialConfig.Mode=SpatialMode.Location;
//启用所有三种报告类型
settings.SpatialConfig.Location.EntryReportEnabled=true;
settings.SpatialConfig.Location.UpdateReportEnabled=true;
settings.SpatialConfig.Location.ExitReportEnabled=true;
//设置xArray放置参数
//xArray的安装高度,以厘米为单位
settings.SpatialConfig.Placement.HeightCm=100
;
//在单个xArray环境中不需要这些设置
//它们可以设置为零(这是默认值)
settings.SpatialConfig.Placement.FacilityXLocationCm=0;
settings.SpatialConfig.Placement.FacilityLocationCM=0;
settings.SpatialConfig.Placement.orientationgrees=0;
//设置xArray位置参数
settings.SpatialConfig.Location.ComputeWindowsSeconds=10;
settings.ReaderMode=ReaderMode.AutoSetDenseReader;
设置。会话=2;
settings.SpatialConfig.Location.TagAgeIntervalSeconds=20;
//指定我们希望接收位置报告的频率
settings.SpatialConfig.Location.UpdateIntervalSeconds=5;
//如果需要最大发射功率,则将其设置为true;如果需要自定义值,则将其设置为false
settings.SpatialConfig.Location.MaxTxPower=false;
//如果MaxTxPower设置为false,则可以使用自定义电源。以.25 dBm为增量提供电源
settings.SpatialConfig.Location.TxPowerInDbm=23.00;
//禁用针对我们可能不需要位置报告的区域的天线,
//在这种情况下,我们禁用天线10和15
List disabledAntennas=新列表{};
settings.SpatialConfig.Location.DisableDantenList=DisableDantenas;
//取消注释这是您要筛选的标记
/*
//设置标记过滤器。
//只有与此筛选器匹配的标记才会响应。
//我们希望将过滤器应用于EPC内存库。
settings.Filters.TagFilter1.MemoryBank=MemoryBank.Epc;
//从第三个字(位32)开始匹配,因为
//EPC内存库的前两个字是
//CRC和控制位。位指针。Epc是一个助手
//可以使用枚举,因此不必记住这一点。
settings.Filters.TagFilter1.BitPointer=BitPointers.Epc;
//仅与以“3008”开头的EPC匹配标记
settings.Filters.TagFilter1.TagMask=“3008”;
//此过滤器的长度为16位(一个字)。
settings.Filters.TagFilter1.BitCount=16;
//设置过滤器模式。仅使用第一个过滤器
settings.Filters.Mode=TagFilterMode.OnlyFilter1;
*/
//应用新修改的设置。
读卡器。应用程序设置(设置);
//启动读卡器
reader.Start();
计时器1_刻度(3000);
//等待用户按enter键。
控制台写入线(“按回车键退出”);
Console.ReadLine();
//退出前应用默认设置。
reader.ApplyDefaultSettings();
//断开与读卡器的连接。
reader.Disconnect();
}
捕获(Octanesdke)
{
//处理辛烷值SDK错误。
Console.WriteLine(“辛烷值SDK异常:
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);
}
    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();
    }