I have a Problem with my timer in C#. The Problem is following:
The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
Can you help me and solve this? I have no plan.
Thank you. Here is my Code:
using System;
using Windows.ApplicationModel.Background;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.Devices.Gpio;
using System.Diagnostics;
namespace StepperBackgroundV3
{
public sealed class StartupTask : IBackgroundTask
{
private const int Stepper_PIN_A = 18;
private const int Stepper_DIR_A = 27;
private const int Sleep_PIN_A = 13;
private const int Enable_PIN_A = 16;
private const int Fault_PIN_A = 24;
private GpioPin StepperDirA;
private GpioPin StepperPinA;
private GpioPin FaultPinA;
private GpioPin SleepPinA;
private GpioPin EnablePinA;
private DispatcherTimer timer;
private double BEAT_PACE = 1000;
private double CounterClockwiseDanceMove = 1;
private double ClockwiseDanceMove = 2;
private double currentDirection;
private double PulseFrequency = 20;
Stopwatch stopwatch;
public void Run(IBackgroundTaskInstance taskInstance)
{
//InitializeComponent();
//
// !!! Wieder einbauen zum Starten der Motoren !!!
//
this.InitDancing();
//
//
//pausiereMotoren();
//erwarteAnweisungen();
}
private void InitDancing()
{
try
{
var gpio = GpioController.GetDefault();
if (gpio == null)
{
StepperPinA = null;
return;
}
StepperPinA = gpio.OpenPin(Stepper_PIN_A);
StepperPinA.SetDriveMode(GpioPinDriveMode.Output);
StepperDirA = gpio.OpenPin(Stepper_DIR_A);
StepperDirA.SetDriveMode(GpioPinDriveMode.Output);
FaultPinA = gpio.OpenPin(Fault_PIN_A);
FaultPinA.SetDriveMode(GpioPinDriveMode.Input);
SleepPinA = gpio.OpenPin(Sleep_PIN_A);
SleepPinA.SetDriveMode(GpioPinDriveMode.Output);
EnablePinA = gpio.OpenPin(Enable_PIN_A);
EnablePinA.SetDriveMode(GpioPinDriveMode.Output);
stopwatch = Stopwatch.StartNew();
currentDirection = 0; // Initially we aren't dancing at all.
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(BEAT_PACE);
timer.Tick += Beat;
if (StepperPinA != null)
{
timer.Start();
Windows.System.Threading.ThreadPool.RunAsync(this.MotorThread, Windows.System.Threading.WorkItemPriority.High);
}
}catch(Exception e)
{
}
}
private void Beat(object sender, object e)
{
if (currentDirection != ClockwiseDanceMove)
{
currentDirection = ClockwiseDanceMove;
//GpioStatus.Text = "Yay!";
}
else
{
currentDirection = CounterClockwiseDanceMove;
//GpioStatus.Text = "Windows 10!";
}
}
private void MotorThread(IAsyncAction action)
{
while (true)
{
if (currentDirection != 0)
{
StepperPinA.Write(GpioPinValue.High);
}
// Könnten es noch heruntersetzen auf 0.1 ms
Wait(currentDirection);
StepperPinA.Write(GpioPinValue.Low);
Wait(PulseFrequency - currentDirection);
}
}
private void Wait(double milliseconds)
{
long initialTick = stopwatch.ElapsedTicks;
long initialElapsed = stopwatch.ElapsedMilliseconds;
double desiredTicks = milliseconds / 1000.0 * Stopwatch.Frequency;
double finalTick = initialTick + desiredTicks;
while (stopwatch.ElapsedTicks < finalTick)
{
}
}
//Standbymode
private void pausiereMotoren()
{
//Sleep setzen....
}
//Communicationssocket
private void erwarteAnweisungen()
{
//Clientsocket Coden
//Irgendwie den Start der Motoren aufrufen
//this.InitDancing();
}
//Fehlererkennung
private void Fault()
{
if(FaultPinA.Read() == GpioPinValue.Low)
{
//LED und Summer ansteuern
//String an Communicationlogger MotorA-Fehler
pausiereMotoren();
}
//if (FaultPinB.Read() == GpioPinValue.Low)
//{
// //LED und Summer ansteuern
// //String an Communicationlogger MotorB-Fehler
// pausiereMotoren();
//}
}
}
}
User contributions licensed under CC BY-SA 3.0