Skip to content

Commit e418dfa

Browse files
committed
Destroy thread created and exit app safely
1 parent 6c83468 commit e418dfa

6 files changed

+36
-11
lines changed

Main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ int main(int argc, char *argv[])
117117

118118
pMiniThingCore->StopMonitorThread();
119119
pMiniThingCore->StopQueryThread();
120+
pMiniThingCore->SQLiteClose();
120121

121122
return 0;
122123
#endif

MiniThing.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@
188188
<ClInclude Include="MiniThing\Utility\Utility.h" />
189189
</ItemGroup>
190190
<ItemGroup>
191+
<None Include=".github\workflows\msbuild.yml" />
191192
<None Include=".gitignore" />
192193
<None Include="LICENSE" />
193194
<None Include="README-CN.md" />

MiniThing.vcxproj.filters

+9
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@
6464
<Filter Include="MiniThing\ThirdParty\EasyLogger\demo\os\windows\easylogger\port">
6565
<UniqueIdentifier>{d92dc947-8176-4b4b-91ac-a675cdbd7335}</UniqueIdentifier>
6666
</Filter>
67+
<Filter Include=".github">
68+
<UniqueIdentifier>{65a01b04-f0eb-4eb4-9620-e5e8dfccac45}</UniqueIdentifier>
69+
</Filter>
70+
<Filter Include=".github\workflows">
71+
<UniqueIdentifier>{e7177173-df38-4607-8632-68e5ad8df407}</UniqueIdentifier>
72+
</Filter>
6773
</ItemGroup>
6874
<ItemGroup>
6975
<ClCompile Include="MiniThing\Core\MiniThingCore.cpp">
@@ -151,6 +157,9 @@
151157
<None Include="LICENSE" />
152158
<None Include="README.md" />
153159
<None Include="README-CN.md" />
160+
<None Include=".github\workflows\msbuild.yml">
161+
<Filter>.github\workflows</Filter>
162+
</None>
154163
</ItemGroup>
155164
<ItemGroup>
156165
<Image Include="Docs\Pictures\Architecture.png">

MiniThing/Core/MiniThingCore.cpp

+16-10
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ MiniThingCore::MiniThingCore(void)
2323

2424
MiniThingCore::~MiniThingCore(void)
2525
{
26-
if (FAILED(SQLiteClose()))
27-
{
28-
assert(0);
29-
}
3026
}
3127

3228
void MiniThingCore::SetDataBasePath(std::wstring dbName)
@@ -727,18 +723,28 @@ void MiniThingCore::StopMonitorThread(void)
727723
{
728724
for (auto it = m_volumeSet.begin(); it != m_volumeSet.end(); it++)
729725
{
730-
SetEvent(it->hMonitorExitEvent);
731-
DWORD dwWaitCode = WaitForSingleObject(it->hMonitor, INFINITE);
732-
assert(dwWaitCode == WAIT_OBJECT_0);
726+
//SetEvent(it->hMonitorExitEvent);
727+
//DWORD dwWaitCode = WaitForSingleObject(it->hMonitor, INFINITE);
728+
//assert(dwWaitCode == WAIT_OBJECT_0);
729+
730+
// Normally we shoud send exit event, and wait thread exit itself
731+
// But it seems that the thread enter dead lock when exit
732+
// So terminate the thread by hand
733+
TerminateThread(it->hMonitor, 0);
733734

734735
CloseHandle(it->hMonitor);
735736

736737
delete it->pMonitorTaskInfo;
737738
}
738739

739-
SetEvent(m_hUpdateSqlDataBaseExitEvent);
740-
DWORD dwWaitCode = WaitForSingleObject(m_hUpdateSqlDataBaseThread, INFINITE);
741-
assert(dwWaitCode == WAIT_OBJECT_0);
740+
//SetEvent(m_hUpdateSqlDataBaseExitEvent);
741+
//DWORD dwWaitCode = WaitForSingleObject(m_hUpdateSqlDataBaseThread, INFINITE);
742+
//assert(dwWaitCode == WAIT_OBJECT_0);
743+
744+
// Normally we shoud send exit event, and wait thread exit itself
745+
// But it seems that the thread enter dead lock when exit
746+
// So terminate the thread by hand
747+
TerminateThread(m_hUpdateSqlDataBaseThread, 0);
742748
}
743749

744750
HRESULT MiniThingCore::CreateQueryThread(void)

MiniThing/Qt/MiniThingQt.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,11 @@ MiniThingQt::MiniThingQt(QWidget* parent) : QMainWindow(parent)
114114

115115
MiniThingQt::~MiniThingQt()
116116
{
117-
delete m_pMiniThingCore;
117+
// Destroy qt work thread firstly, cause there are some data strcture
118+
// we need use to terminate thread and so on
119+
// after we destroy qt work thread, we can destroy core safely
118120
delete m_pMiniThingQtWorkThread;
121+
delete m_pMiniThingCore;
119122

120123
delete m_rightKeyMenu;
121124
delete m_rightKeyActionOpen;

MiniThing/Qt/MiniThingQtBackgroud.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ MiniThingQtWorkThread::MiniThingQtWorkThread()
88

99
MiniThingQtWorkThread::~MiniThingQtWorkThread()
1010
{
11+
// Terminate monitor thread, and update sql base thread within it
12+
m_pMiniThingCore->StopMonitorThread();
13+
14+
// Close sqlite database before return
15+
m_pMiniThingCore->SQLiteClose();
1116
}
1217

1318

0 commit comments

Comments
 (0)