How to properly implement and close sleeping thread in a Windows DLL
Date : March 29 2020, 07:55 AM
should help you out I have a native Windows DLL written in C. This DLL is designed to be loaded (injected) in different Windos processes. This DLL creates some working threads that look like this: , Create an event: HANDLE threadTerminationEvent = CreateEvent(.....);
while (1)
{
...........
if (WaitForSingleObject(threadTerminationEvent, few secs) != WAIT_TIMEOUT)
break;
}
// release resources and end thread
SetEvent(threadTerminationEvent); // “please, die”
WaitForSingleObject(hThread, INFINITE);
|
How to know if my SensorManager has a registered Sensor
Date : March 29 2020, 07:55 AM
I wish this helpful for you As far as I know, there is no native way to check if you have already registered listener. I would not worry too much about this check since this check is already done by Android, so if you have already registered listener, Android is not gonna add the same listener twice: @SuppressWarnings("deprecation")
private boolean registerLegacyListener(int legacyType, int type,
SensorListener listener, int sensors, int rate)
{
if (listener == null) {
return false;
}
boolean result = false;
// Are we activating this legacy sensor?
if ((sensors & legacyType) != 0) {
// if so, find a suitable Sensor
Sensor sensor = getDefaultSensor(type);
if (sensor != null) {
// If we don't already have one, create a LegacyListener
// to wrap this listener and process the events as
// they are expected by legacy apps.
LegacyListener legacyListener = null;
synchronized (mLegacyListenersMap) {
legacyListener = mLegacyListenersMap.get(listener);
if (legacyListener == null) {
// we didn't find a LegacyListener for this client,
// create one, and put it in our list.
legacyListener = new LegacyListener(listener);
mLegacyListenersMap.put(listener, legacyListener);
}
}
// register this legacy sensor with this legacy listener
legacyListener.registerSensor(legacyType);
// and finally, register the legacy listener with the new apis
result = registerListener(legacyListener, sensor, rate);
}
}
return result;
}
|
Android: implement sensormanager in a class
Date : March 29 2020, 07:55 AM
help you fix your problem You have named your activity as SensorManager which is wrong because there is already a SensorManager Class in SDK Try re-naming your activity.
|
Android: Algorithms for SensorManager.getRotationMatrix and SensorManager.getOrientation()
Date : March 29 2020, 07:55 AM
I wish this helpful for you Let me try to explain: getRotationMatrix compose rotation matrix on the basis of gravity and megnetic vector. Our main goal here is to construct NED frame float Hx = Ey*Az - Ez*Ay;
float Hy = Ez*Ax - Ex*Az;
float Hz = Ex*Ay - Ey*Ax;
final float normH = (float)Math.sqrt(Hx*Hx + Hy*Hy + Hz*Hz);
final float invH = 1.0f / normH;
Hx *= invH;
Hy *= invH;
Hz *= invH;
final float invA = 1.0f / (float)Math.sqrt(Ax*Ax + Ay*Ay + Az*Az);
Ax *= invA;
Ay *= invA;
Az *= invA;
double Mx = Ay * Hz - Az * Hy;
double My = Az * Hx - Ax * Hz;
double Mz = Ax * Hy - Ay * Hx;
R[0] = Hx; R[1] = Hy; R[2] = Hz;
R[3] = Mx; R[4] = My; R[5] = Mz;
R[6] = Ax; R[7] = Ay; R[8] = Az;
azimuth = (float)Math.atan2(R[1], R[4]);
pitch = (float)Math.asin(-R[7]);
roll = (float)Math.atan2(-R[6], R[8]);
|
How do I properly implement a cancellable, long-running thread in C#?
Tag : chash , By : Moe Skeeto
Date : March 29 2020, 07:55 AM
Any of those help Tasks aren't necessarily for I/O bound operations. In fact, this is a good use-case for Task.Delay (which internally wraps a timer): public static async Task ProcessAsync(CancellationToken cancellationToken)
{
try
{
while (true)
{
await Task.Delay(TimeSpan.FromMinutes(5), cancellationToken).ConfigureAwait(false);
Process();
}
}
catch (TaskCanceledException)
{
// Cancellation requested, do whatever cleanup you need then exit gracefully
}
}
var cancellationTokenSource = new CancellationTokenSource();
var task = ProcessAsync(cancellationTokenSource.Token);
string response;
do
{
Console.WriteLine("Press 'q' to quit");
response = Console.ReadLine();
} while (response != "q");
cancellationTokenSource.Cancel();
task.Wait(); // Wait for the task to finish
var timer = new Timer(_ => Process(), null, TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(5));
string response;
do
{
Console.WriteLine("Press 'q' to quit");
response = Console.ReadLine();
} while (response != "q");
timer.Dispose(); // Stop the timer
|