2017-02-07 88 views
-1

其實我在stackoverflow中發現了一些類似的東西,但它並不適合我。他們希望在特定時間(23:00)退出申請。我想在3000秒後退出。對我來說,答案是有幫助的,現在它的工作,主要的一點是,這是很重要的,你把這段代碼放在你的程序中。如何在C#之後第二秒退出或關閉應用程序?

此代碼正在管理多波束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(); 
     } 

    } 


} 
+1

嘛'到Console.ReadLine();'是什麼保持程序打開,因爲它預計輸入,爲什麼它在那裏,如果你想要關閉? – Equalsk

+0

推薦發佈最少完整的可驗證示例。 http://stackoverflow.com/help/mcve – chadnt

+0

[定時器關閉應用程序]可能的重複(http://stackoverflow.com/questions/13626454/timer-to-close-the-application) –

回答

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(); 
    } 
+0

謝謝,它現在的工作! – tiby007

+0

但是,我應該使用'Enviroment.Exit'而不是'Application.Exit' – tiby007

1

我覺得你的問題是如何結束的計時器事件處理程序的控制檯應用程序。如何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); 
} 
+0

謝謝你的回答,但是使用這些代碼,它沒有做任何事情,只需打開控制檯,等待10秒鐘並退出。然後我改變了其他解決方案(可以在下面找到)。它的工作.... – tiby007

相關問題