More testing of threading changes.

This commit is contained in:
Travis Nickles 2017-03-22 23:39:31 -07:00
parent ecd36237e7
commit bd73978204

View File

@ -137,6 +137,7 @@ namespace DS4Windows
public DateTime lastActive = DateTime.UtcNow; public DateTime lastActive = DateTime.UtcNow;
public DateTime firstActive = DateTime.UtcNow; public DateTime firstActive = DateTime.UtcNow;
private bool charging; private bool charging;
private bool outputRumble = false;
public event EventHandler<EventArgs> Report = null; public event EventHandler<EventArgs> Report = null;
public event EventHandler<EventArgs> Removal = null; public event EventHandler<EventArgs> Removal = null;
@ -266,15 +267,11 @@ namespace DS4Windows
public void StopUpdate() public void StopUpdate()
{ {
if (ds4Output.ThreadState != System.Threading.ThreadState.Unstarted && ds4Input.ThreadState != System.Threading.ThreadState.Stopped) if (ds4Input.ThreadState != System.Threading.ThreadState.Unstarted && ds4Input.ThreadState != System.Threading.ThreadState.Stopped)
{ {
try try
{
if (ds4Output.ThreadState != System.Threading.ThreadState.Aborted && ds4Output.ThreadState != System.Threading.ThreadState.AbortRequested)
{ {
ds4Input.Abort(); ds4Input.Abort();
}
ds4Input.Join(); ds4Input.Join();
} }
catch (Exception e) catch (Exception e)
@ -290,12 +287,8 @@ namespace DS4Windows
if (ds4Output.ThreadState != System.Threading.ThreadState.Unstarted && ds4Output.ThreadState != System.Threading.ThreadState.Stopped) if (ds4Output.ThreadState != System.Threading.ThreadState.Unstarted && ds4Output.ThreadState != System.Threading.ThreadState.Stopped)
{ {
try try
{
if (ds4Output.ThreadState != System.Threading.ThreadState.Aborted && ds4Output.ThreadState != System.Threading.ThreadState.AbortRequested)
{ {
ds4Output.Abort(); ds4Output.Abort();
}
ds4Output.Join(); ds4Output.Join();
} }
catch (Exception e) catch (Exception e)
@ -321,20 +314,15 @@ namespace DS4Windows
{ {
lock (outputReport) lock (outputReport)
{ {
//int lastError = 0; int lastError = 0;
while (true) while (true)
{ {
Monitor.Wait(outputReport); bool result = false;
writeOutput(); if (outputRumble)
/*if (writeOutput())
{ {
lastError = 0; result = writeOutput();
if (testRumble.IsRumbleSet()) // repeat test rumbles periodically; rumble has auto-shut-off in the DS4 firmware
Monitor.Wait(outputReport, 10000); // DS4 firmware stops it after 5 seconds, so let the motors rest for that long, too. if (!result)
else
Monitor.Wait(outputReport);
}
else
{ {
int thisError = Marshal.GetLastWin32Error(); int thisError = Marshal.GetLastWin32Error();
if (lastError != thisError) if (lastError != thisError)
@ -343,10 +331,25 @@ namespace DS4Windows
lastError = thisError; lastError = thisError;
} }
} }
else
{
outputRumble = false;
}
}
if (!outputRumble)
{
lastError = 0;
Monitor.Wait(outputReport);
/*if (testRumble.IsRumbleSet()) // repeat test rumbles periodically; rumble has auto-shut-off in the DS4 firmware
Monitor.Wait(outputReport, 10000); // DS4 firmware stops it after 5 seconds, so let the motors rest for that long, too.
else
Monitor.Wait(outputReport);
*/ */
} }
} }
} }
}
/** Is the device alive and receiving valid sensor input reports? */ /** Is the device alive and receiving valid sensor input reports? */
public bool IsAlive() public bool IsAlive()
@ -595,6 +598,7 @@ namespace DS4Windows
{ {
if (synchronous) if (synchronous)
{ {
outputRumble = false;
outputReportBuffer.CopyTo(outputReport, 0); outputReportBuffer.CopyTo(outputReport, 0);
try try
{ {
@ -617,6 +621,7 @@ namespace DS4Windows
output = outputReport[i] != outputReportBuffer[i]; output = outputReport[i] != outputReportBuffer[i];
if (output) if (output)
{ {
outputRumble = true;
outputReportBuffer.CopyTo(outputReport, 0); outputReportBuffer.CopyTo(outputReport, 0);
Monitor.Pulse(outputReport); Monitor.Pulse(outputReport);
} }