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)
@ -291,11 +288,7 @@ namespace DS4Windows
{ {
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,29 +314,39 @@ 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()) {
result = writeOutput();
if (!result)
{
int thisError = Marshal.GetLastWin32Error();
if (lastError != thisError)
{
Console.WriteLine(MacAddress.ToString() + " " + System.DateTime.UtcNow.ToString("o") + "> encountered write failure: " + thisError);
lastError = thisError;
}
}
else
{
outputRumble = false;
}
}
if (!outputRumble)
{ {
lastError = 0; lastError = 0;
if (testRumble.IsRumbleSet()) // repeat test rumbles periodically; rumble has auto-shut-off in the DS4 firmware 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. Monitor.Wait(outputReport, 10000); // DS4 firmware stops it after 5 seconds, so let the motors rest for that long, too.
else else
Monitor.Wait(outputReport); Monitor.Wait(outputReport);
*/
} }
else
{
int thisError = Marshal.GetLastWin32Error();
if (lastError != thisError)
{
Console.WriteLine(MacAddress.ToString() + " " + System.DateTime.UtcNow.ToString("o") + "> encountered write failure: " + thisError);
lastError = thisError;
}
}
*/
} }
} }
} }
@ -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);
} }