Skip to content

Commit 16abb86

Browse files
committed
Fixes related to threading and release prep.
1 parent 473d2db commit 16abb86

File tree

9 files changed

+47
-18
lines changed

9 files changed

+47
-18
lines changed

Forms/KeyCaptureConfig.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public partial class KeyCaptureConfig
4646
private readonly IniManager m_zIniManager = new IniManager(Application.ProductName, false, true, false);
4747

4848
private FormWindowState m_ePrevWindowState = FormWindowState.Normal;
49-
private bool m_bRun = true;
5049
private bool m_bShutdownApplication = false;
5150

5251
/// <summary>
@@ -129,7 +128,6 @@ private void newToolStripMenuItem_Click(object sender, EventArgs e)
129128
private void exitMainToolStripMenuItem_Click(object sender, EventArgs e)
130129
{
131130
m_bShutdownApplication = true;
132-
m_bRun = false;
133131
if (WindowState == FormWindowState.Minimized)
134132
{
135133
WindowState = m_ePrevWindowState;

KeyCapLib/KeyCapture.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void InitiallizeEntryContainerListItem(RemapEntryContainerListItem* pKeyItem, Re
153153
pKeyItem->pEntryContainer->pEntry = pEntry;
154154
}
155155

156-
void ShutdownInputThreads()
156+
void ShutdownInputThreads(bool forceShutdown)
157157
{
158158
// signal shutdown for all entries with a thread handle
159159
for (int nIdx = 0; nIdx < WIN_KEY_COUNT; nIdx++)
@@ -174,6 +174,9 @@ void ShutdownInputThreads()
174174
}
175175
}
176176

177+
// a forced shutdown will kill threads (mostly for application exit)
178+
if(!forceShutdown) return;
179+
177180
// monitor and terminate threads for all entries with a thread handle
178181
for (int nIdx = 0; nIdx < WIN_KEY_COUNT; nIdx++)
179182
{
@@ -210,7 +213,7 @@ Shuts down the key capture hook and frees any allocated memory
210213
__declspec(dllexport) void ShutdownCapture()
211214
{
212215
// stop any active threads
213-
ShutdownInputThreads();
216+
ShutdownInputThreads(true);
214217

215218
// disable the hook
216219
if(NULL != g_hookMain)

KeyCapLib/KeyCapture.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "keycaptureutil.h"
3030

3131
// shared functions
32-
void ShutdownInputThreads();
32+
void ShutdownInputThreads(bool forceShutdown);
3333

3434
// === consts and defines
3535
const int THREAD_SHUTDOWN_MAX_ATTEMPTS = 5;

KeyCapLib/KeyCaptureUtil.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@
2525
#include "keycapturestructs.h"
2626
#include "keycaptureutil.h"
2727

28+
void ResetRemapEntryState(RemapEntryState* pRemapEntryState, BYTE bToggled)
29+
{
30+
pRemapEntryState->bToggled = bToggled;
31+
pRemapEntryState->bRepeating = false;
32+
pRemapEntryState->bShutdown = false;
33+
pRemapEntryState->threadHandle = NULL;
34+
}
35+
2836
bool IsButtonDownRequired(RemapEntryState* pRemapEntryState, OutputConfig* pKeyDef)
2937
{
3038
if (pKeyDef->outputFlag.bToggle)

KeyCapLib/KeyCaptureUtil.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
const int DESCRIPTION_BUFFER_SIZE = 256;
3232

33+
void ResetRemapEntryState(RemapEntryState* pRemapEntryState, BYTE bToggled);
3334
bool IsButtonDownRequired(RemapEntryState* pRemapEntryState, OutputConfig* pKeyDef);
3435
bool IsButtonUpRequired(RemapEntryState* pRemapEntryState, OutputConfig* pKeyDef);
3536

KeyCapLib/sendinputthread.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,8 @@ DWORD InitiateSendInput(RemapEntry* pRemapEntry, RemapEntryState* pRemapEntrySta
109109
}
110110
if(pOutputConfig->outputFlag.bCancelActiveOutputs)
111111
{
112-
// HACK alert - would destroy this thread, so disassociate and exit immediately after
113-
pRemapEntryState->bShutdown = true;
114-
pRemapEntryState->threadHandle = NULL;
115-
ShutdownInputThreads();
112+
// NOTE: even this thread might be affected
113+
ShutdownInputThreads(false);
116114
break;
117115
}
118116
else if (pOutputConfig->outputFlag.bMouseOut)
@@ -152,17 +150,34 @@ DWORD InitiateSendInput(RemapEntry* pRemapEntry, RemapEntryState* pRemapEntrySta
152150
firstPassComplete = true;
153151
if (pRemapEntryState->bRepeating)
154152
{
155-
Sleep(repeatDelayMillis);
156-
LogDebugMessage("SendInputThread Repeating...");
153+
int iterations = repeatDelayMillis / MIN_REPEAT_DELAY_MS;
154+
for (int repeatDelayCount = 0; repeatDelayCount < iterations; repeatDelayCount++)
155+
{
156+
Sleep(100);
157+
if (pRemapEntryState->bShutdown)
158+
{
159+
LogDebugMessage("Exiting repeat loop due to shutdown.");
160+
break;
161+
}
162+
}
163+
if (!pRemapEntryState->bShutdown)
164+
{
165+
LogDebugMessage("SendInputThread Repeating...");
166+
}
157167
}
158168
} while (pRemapEntryState->bRepeating);
159169

160-
// flip the overall toggle state
170+
// flip the overall toggle state (even when a thread is canceled this should flip back, arguably to false...)
161171
pRemapEntryState->bToggled = !pRemapEntryState->bToggled;
162-
pRemapEntryState->bShutdown = false;
163172

164-
LogDebugMessage("SendInputThread Completed");
165-
// Disconnect this thread from the state
173+
// reset things on the remap entry
174+
pRemapEntryState->bShutdown = false;
175+
pRemapEntryState->bRepeating = false;
166176
pRemapEntryState->threadHandle = NULL;
177+
#ifdef _DEBUG
178+
char* pInputConfigDescription = GetInputConfigDescription(pRemapEntry->inputConfig);
179+
LogDebugMessage("SendInputThread Completed: %s", pInputConfigDescription);
180+
free(pInputConfigDescription);
181+
#endif
167182
return 0;
168183
}

Properties/AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
[assembly: AssemblyConfiguration("")]
3434
[assembly: AssemblyCompany("NHMK")]
3535
[assembly: AssemblyProduct("KeyCap")]
36-
[assembly: AssemblyCopyright("Copyright © NHMK 2019")]
36+
[assembly: AssemblyCopyright("Copyright © NHMK 2020")]
3737
[assembly: AssemblyTrademark("")]
3838
[assembly: AssemblyCulture("")]
3939

@@ -55,5 +55,5 @@
5555
// You can specify all the values or you can default the Build and Revision Numbers
5656
// by using the '*' as shown below:
5757
// [assembly: AssemblyVersion("1.0.*")]
58-
[assembly: AssemblyVersion("2.0.0.0")]
59-
[assembly: AssemblyFileVersion("2.0.0.0")]
58+
[assembly: AssemblyVersion("2.0.0.1")]
59+
[assembly: AssemblyFileVersion("2.0.0.1")]

bin/Release/History.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
KeyCap Version History
22
--------------------------
3+
2.0.0.1
4+
- Added ability to cancel all active threads (repeats, delays, etc.)
5+
- Fixed a critical bug with shutdown (basically it wouldn't)
6+
37
2.0.0.0
48
- New file format for configurations to support future expansion
59
- Direct control over up/down messages (if needed)

bin/Release/KeyCap.odt

381 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)