不稳定/奇怪的程序行为,入口点击中两次

时间:2018-01-27 17:15:36

标签: c# windows-10-iot-core

在Rasberry pi 3上运行的Windows 10 IoT。这是我的代码:

    using System;
    using Windows.ApplicationModel.Background;
    using Windows.Devices.Gpio;
    using System.IO;


namespace CatFeeders
{
public sealed class StartupTask : IBackgroundTask
{
    private BackgroundTaskDeferral deferral;
    GpioController gpio = GpioController.GetDefault();
    int[] pinNumbers = new int[] { 17, 27, 22 };

    public void Run(IBackgroundTaskInstance taskInstance)
    {
        deferral = taskInstance.GetDeferral();
        WriteLog("Starting");
        foreach (int i in pinNumbers)
        {
            using (GpioPin p = gpio.OpenPin(i))
            {
                p.Write(GpioPinValue.Low);
                p.SetDriveMode(GpioPinDriveMode.Output);
                WriteLog("Ran pin " + p.PinNumber.ToString());
                System.Threading.Tasks.Task.Delay(2000).Wait();
            }
        }
        WriteLog("Finished");
        System.Threading.Tasks.Task.Delay(2000).Wait();
        deferral.Complete();
    }
    private static string GetNow()
    {
        string datePatt = @"M/d/yyyy hh:mm:ss tt";
        DateTime currtime = DateTime.Now;
        string now = currtime.ToString(datePatt);
        return now;
    }
    private void WriteLog(string message)
    {
        File.AppendAllText("C:\\logs\\feeder.log", GetNow()+" : "+message + Environment.NewLine);
    }
}

}

这是生成的日志文件:

1/27/2018 10:55:31 AM : Starting
1/27/2018 10:55:32 AM : Ran pin 17
1/27/2018 10:55:32 AM : Starting
1/27/2018 10:55:35 AM : Ran pin 27
1/27/2018 10:55:37 AM : Ran pin 22
1/27/2018 10:55:39 AM : Finished

"开始"记录线被第二次击中?每次我运行程序时它都不会这样做......

有什么想法吗?

谢谢! 格雷格

1 个答案:

答案 0 :(得分:0)

我认为这是因为我停止后很快就运行它。我猜我会留意它。

感谢您的帮助!