8.2 C
New York
Thursday, March 20, 2025

Issues getting app to run asyncronously


Been making an attempt to create an app which is ready to preserve observe of steps, and gyro + accelometer on the identical time. At this present stage I’m able to run both the gyro or the stepcounter independently, however not collectively. It simply crashes when launching the app atm.

GyroSensor.cs class


utilizing System;
utilizing System.Threading.Duties;
utilizing Microsoft.Maui.Units.Sensors;
utilizing Microsoft.Maui.Dispatching;

namespace WodiaAPP.Utilities
{
    public class GyroSensor
    {
        personal bool isRunning = false;
        personal GyroscopeData lastReading;
        public occasion Motion<string>? OnStatusUpdated;

        public GyroSensor()
        {
            _ = StartGyroscope(); // Fireplace and neglect
        }

        // Begins the gyroscope
        personal async Activity StartGyroscope()
        {
            if (Gyroscope.IsSupported)
            {
                Gyroscope.ReadingChanged += Gyroscope_ReadingChanged;
                Gyroscope.Begin(SensorSpeed.UI);

                isRunning = true;

                OnStatusUpdated?.Invoke("Gyroscope Began");

                whereas (isRunning)
                {
                    await Activity.Delay(10000); // Replaces System.Timers.Timer
                    await OnGyroTimerElapsed();
                }
            }
            else
            {
                OnStatusUpdated?.Invoke("Gyroscope Not Supported");
            }
        }

        // Timer occasion handler to examine if the telephone is transferring
        personal async Activity OnGyroTimerElapsed()
        {
            var knowledge = lastReading;
            double threshold = 0.1;

            string standing = (Math.Abs(knowledge.AngularVelocity.X) > threshold ||
                             Math.Abs(knowledge.AngularVelocity.Y) > threshold ||
                             Math.Abs(knowledge.AngularVelocity.Z) > threshold)
                            ? "Cellphone is transferring!"
                            : "Cellphone continues to be.";

            await MainThread.InvokeOnMainThreadAsync(() =>
            {
                OnStatusUpdated?.Invoke(standing);
            });
        }

        // Occasion handler for gyroscope studying adjustments
        personal void Gyroscope_ReadingChanged(object? sender, GyroscopeChangedEventArgs e)
        {
            lastReading = e.Studying;
        }

        //stops the gyroscope
        public void StopGyroscope()
        {
            isRunning = false;
            Gyroscope.Cease();
            OnStatusUpdated?.Invoke("Gyroscope Stopped");
        }
    }
}

StepCounter.cs class

utilizing Plugin.Maui.Pedometer;
utilizing System;
utilizing System.Threading.Duties;

namespace WodiaAPP.Utilities
{
    public class StepCounter
    {
        personal readonly IPedometer _pedometer;

        // Occasion for step depend updates
        public occasion Motion<string> OnStepCountUpdated;

        public StepCounter(IPedometer pedometer)
        {
            _pedometer = pedometer;
            _pedometer.ReadingChanged += OnPedometerReadingChanged;
        }

        // Async technique to begin pedometer monitoring
        public async Activity StartAsync()
        {
            if (_pedometer.IsSupported)
            {
                await Activity.Run(() => _pedometer.Begin());
            }
        }

        // Deal with studying adjustments from pedometer
        personal void OnPedometerReadingChanged(object sender, PedometerData e)
        {
            OnStepCountUpdated?.Invoke(e.NumberOfSteps.ToString());
        }

        // Properties to examine if pedometer is supported or monitoring
        public bool IsSupported => _pedometer.IsSupported;
        public bool IsMonitoring => _pedometer.IsMonitoring;
    }
}

Right here is my try at operating them collectively on the MainPage:

utilizing System;
utilizing Microsoft.Maui.Controls;
utilizing WodiaAPP.Utilities;
utilizing Plugin.Maui.Pedometer;
utilizing Microsoft.Maui.Dispatching;
utilizing System.Threading.Duties;

namespace WodiaAPP
{
    public partial class MainPage : ContentPage
    {
        personal readonly GyroSensor gyroSensor;
        personal readonly StepCounter stepCounter;

        public MainPage()
        {
            InitializeComponent();

            // Initialize GyroSensor
            gyroSensor = new GyroSensor();
            gyroSensor.OnStatusUpdated += standing => MainThread.BeginInvokeOnMainThread(() =>
            {
                GyroStatusLabel.Textual content = standing;
            });

            // Initialize StepCounter with Pedometer from the Plugin
            var pedometer = DependencyService.Get<IPedometer>(); // Assuming dependency injection for Pedometer
            stepCounter = new StepCounter(pedometer);

            // Initialize and begin StepCounter asynchronously
            InitializeStepCounterAsync().ConfigureAwait(false);

            // Subscribe to step depend updates
            stepCounter.OnStepCountUpdated += standing => MainThread.InvokeOnMainThreadAsync(() =>
            {
                StepCount.Textual content = standing;
            });
        }

        // New technique to correctly await StartAsync for the StepCounter
        personal async Activity InitializeStepCounterAsync()
        {
            if (stepCounter.IsSupported)
            {
                await stepCounter.StartAsync();
            }
            else
            {
                // Deal with the case the place the pedometer is just not supported
                MainThread.BeginInvokeOnMainThread(() =>
                {
                    StepCount.Textual content = "Pedometer Not Supported";
                });
            }
        }
    }
}


Right here is the error which reveals up when launching:

Loaded meeting: /personal/var/cellular/Containers/Knowledge/Software/E6CFA9BA-F86C-4B59-9103-E092CA4F7B07/Paperwork/WodiaAPP.content material/System.Web.Http.Json.dll [External]
2025-03-20 12:07:52.574 Xamarin.PreBuilt.iOS[8810:1610594] Couldn't resolve meeting System.Runtime.InteropServices.JavaScript.sources, Model=9.0.0.0, Tradition=da, PublicKeyToken=null. Particulars: Couldn't load file or meeting '/var/cellular/Containers/Knowledge/Software/E6CFA9BA-F86C-4B59-9103-E092CA4F7B07/Paperwork/WodiaAPP.content material/System.Runtime.InteropServices.JavaScript.sources.dll' or considered one of its dependencies.
Loaded meeting: /personal/var/cellular/Containers/Knowledge/Software/E6CFA9BA-F86C-4B59-9103-E092CA4F7B07/Paperwork/WodiaAPP.content material/System.Safety.Cryptography.OpenSsl.dll [External]
Loaded meeting: /personal/var/cellular/Containers/Knowledge/Software/E6CFA9BA-F86C-4B59-9103-E092CA4F7B07/Paperwork/WodiaAPP.content material/System.Runtime.InteropServices.JavaScript.dll [External]
[0:] Didn't power load meeting /var/cellular/Containers/Knowledge/Software/E6CFA9BA-F86C-4B59-9103-E092CA4F7B07/Paperwork/WodiaAPP.content material/System.Runtime.InteropServices.JavaScript.dll. Sort:System.Runtime.InteropServices.JavaScript.JSExportAttribute. Exception: 'System.Runtime.InteropServices.JavaScript is just not supported on this platform.'. Callstack: '   at System.Runtime.InteropServices.JavaScript.JSExportAttribute..ctor()
   at System.Object.InvokeStub_JSExportAttribute..ctor(Object , Object , IntPtr* )
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)'
Loaded meeting: /personal/var/cellular/Containers/Knowledge/Software/E6CFA9BA-F86C-4B59-9103-E092CA4F7B07/Paperwork/WodiaAPP.content material/Microsoft.Extensions.DependencyInjection.dll [External]
Loaded meeting: /personal/var/cellular/Containers/Knowledge/Software/E6CFA9BA-F86C-4B59-9103-E092CA4F7B07/Paperwork/WodiaAPP.content material/System.Textual content.Encoding.CodePages.dll [External]
Loaded meeting: /personal/var/cellular/Containers/Knowledge/Software/E6CFA9BA-F86C-4B59-9103-E092CA4F7B07/Paperwork/WodiaAPP.content material/Plugin.Maui.Pedometer.dll [External]
Loaded meeting: /personal/var/cellular/Containers/Knowledge/Software/E6CFA9BA-F86C-4B59-9103-E092CA4F7B07/Paperwork/WodiaAPP.content material/Microsoft.VisualBasic.dll [External]
Loaded meeting: /personal/var/cellular/Containers/Knowledge/Software/E6CFA9BA-F86C-4B59-9103-E092CA4F7B07/Paperwork/WodiaAPP.content material/System.dll [External]
Loaded meeting: /personal/var/cellular/Containers/Knowledge/Software/E6CFA9BA-F86C-4B59-9103-E092CA4F7B07/Paperwork/WodiaAPP.content material/System.Drawing.dll [External]
Loaded meeting: /personal/var/cellular/Containers/Knowledge/Software/E6CFA9BA-F86C-4B59-9103-E092CA4F7B07/Paperwork/WodiaAPP.content material/System.Assets.Reader.dll [External]

Any and all suggestions is welcome in order that I can enhance!

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles