0
votes

i've coded a code next: private async void WriteData() { var removableDevices = KnownFolders.RemovableDevices; var externalDrives = await removableDevices.GetFoldersAsync(); var drive0 = externalDrives[0];

            var testFolder = await drive0.CreateFolderAsync("test");
            var testFile = await testFolder.CreateFileAsync("test.jpg");

            var byteArray = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
            using (var sourceStream = new MemoryStream(byteArray).AsRandomAccessStream())
            {
                using (var destinationStream = (await testFile.OpenAsync(FileAccessMode.ReadWrite)).GetOutputStreamAt(0))
                {
                    await RandomAccessStream.CopyAndCloseAsync(sourceStream, destinationStream);
                }

            }

        }

        private async void _timer_Tick(object sender, object e)
        {
            DhtReading reading = new DhtReading();
            int val = this.TotalAttempts;
            this.TotalAttempts++;

            reading = await _dht.GetReadingAsync().AsTask();

            _retryCount.Add(reading.RetryCount);
            this.OnPropertyChanged(nameof(AverageRetriesDisplay));
            this.OnPropertyChanged(nameof(TotalAttempts));
            this.OnPropertyChanged(nameof(PercentSuccess));
        if (reading.IsValid)
        {
            this.TotalSuccess++;
            this.Temperature = Convert.ToSingle(reading.Temperature);
            this.Humidity = Convert.ToSingle(reading.Humidity);
            this.LastUpdated = DateTimeOffset.Now;
            this.OnPropertyChanged(nameof(SuccessRate));

            this.WriteData();


        }

        this.OnPropertyChanged(nameof(LastUpdatedDisplay));

    }

My code freeze , and i wanna know how could I save the humidity and temperature values in that archive, It is the best way to save this app ?

Someone Could Help me ?

2
I checked WriteData() on Raspberry Pi 2. I wrote to test.jpg successfully. Problem may be in _timer_Tick(). Which line cause your code freeze? What device do you use?Rita Han
i'm using Raspberry Pi 3 , with Windows Iot Core OS version : 10.0.14393.0 , did you saw any mistake in my code?Felipe Ferreira Mendes
The error is in #endif #if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION UnhandledException += (sender, e) => { if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break(); //THIS LINE }; #endif } } }Felipe Ferreira Mendes

2 Answers

0
votes

Because after you ran the code for the first time, the "test" folder and "test.jpg" is already created, so when you run the code again you may receive this exception. You can try this:

var testFolder = await drive0.GetFolderAsync("test");
var testFile = await testFolder.GetFileAsync("test.jpg");

Instead of

var testFolder = await drive0.CreateFolderAsync("test");
var testFile = await testFolder.CreateFileAsync("test.jpg");

Note: Here I saved the test.jpg on an external SD card. If there is no SD card in your device, the code also run into this exception.

0
votes

Try this in top of your class, MyClass is the name of the container class

StorageFolder testFolder;
public MyClass()
{
    var removableDevices = KnownFolders.RemovableDevices; 
    var externalDrives = await removableDevices.GetFoldersAsync(); 
    var drive0 = externalDrives[0];
    testFolder = await drive0.CreateFolderAsync("test");
    await testFolder.CreateFileAsync("test.jpg");
}

And, in your WriteData method, you say only

var testFile = await testFolder.GetFolderAsync("test.jpg");

Observations

  • WriteData get slower at removableDevices.GetFoldersAsync()
  • As you use a timer you need to keep the Tick event as fast and resources free as possible