880228 - taking time in handling stops responding
-
hi in a dialog when i hit a button some robust calculations begin. they may take a long time and the user may decide to cancel them and examine another parameters. i put a button for this purpose, but how can it be hit while the program is still busy with th e calculations begun by hitting the other button? the code is not yet done its job and the function is not yet returned from. so the hit button message cannot be processed until returning from the function. this happens mostly in such programs. what's the routine and the best way to resolve it and what's the method used in such cases? thx
-
hi in a dialog when i hit a button some robust calculations begin. they may take a long time and the user may decide to cancel them and examine another parameters. i put a button for this purpose, but how can it be hit while the program is still busy with th e calculations begun by hitting the other button? the code is not yet done its job and the function is not yet returned from. so the hit button message cannot be processed until returning from the function. this happens mostly in such programs. what's the routine and the best way to resolve it and what's the method used in such cases? thx
Windows needs to be given control, so it can route messages (like the 'button has been clicked' message) around your program. The usual solution is to do your long calculation in a worker thread[^].
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
hi in a dialog when i hit a button some robust calculations begin. they may take a long time and the user may decide to cancel them and examine another parameters. i put a button for this purpose, but how can it be hit while the program is still busy with th e calculations begun by hitting the other button? the code is not yet done its job and the function is not yet returned from. so the hit button message cannot be processed until returning from the function. this happens mostly in such programs. what's the routine and the best way to resolve it and what's the method used in such cases? thx
If your calculation is just a calculation and never refers any other resources, I recommend the calculation is to be a thread and dialog button kickes start and stop only. I figure it very simply like belows.
unsinged __stdcall calculation(void*)
{
// start real calculation
}void CMyDialog::OnStart() {
m_threadHandle = (HANDLE)_beginthreadex(NULL, 0, calculation, NULL, 0, &m_threadId);
};void CMyDialog::OnCancel() {
if (m_threadHandle) {
TerminateThread(m_threadHandle, 0x13);
CloseHandle(m_threadHandle);
}
};Consider the calculation() must be thread-safe and makes no resource leaks. If your calculation refers some other resources and should not be killed by TerminateThread(), another method is a stop flag refernce inside a loop. like;
void calc(bool volatile f_stop) {
for () {
for () {
for () {
if (f_stop) goto exit_all_loop;
}
}
}
exit_all_loop:
// clean up all resources
}The function calc() is to be well designed at a point of cleaning up and performance trade-off.
-
If your calculation is just a calculation and never refers any other resources, I recommend the calculation is to be a thread and dialog button kickes start and stop only. I figure it very simply like belows.
unsinged __stdcall calculation(void*)
{
// start real calculation
}void CMyDialog::OnStart() {
m_threadHandle = (HANDLE)_beginthreadex(NULL, 0, calculation, NULL, 0, &m_threadId);
};void CMyDialog::OnCancel() {
if (m_threadHandle) {
TerminateThread(m_threadHandle, 0x13);
CloseHandle(m_threadHandle);
}
};Consider the calculation() must be thread-safe and makes no resource leaks. If your calculation refers some other resources and should not be killed by TerminateThread(), another method is a stop flag refernce inside a loop. like;
void calc(bool volatile f_stop) {
for () {
for () {
for () {
if (f_stop) goto exit_all_loop;
}
}
}
exit_all_loop:
// clean up all resources
}The function calc() is to be well designed at a point of cleaning up and performance trade-off.
thank u for ur suggestions, so u think i've to use a separated thread for this purpose? is it the best way? what if i want to avoid multi-threading problems by keeping it a single-thread app?
-
thank u for ur suggestions, so u think i've to use a separated thread for this purpose? is it the best way? what if i want to avoid multi-threading problems by keeping it a single-thread app?
I think multi-threaded model is best practice for these case. Another approach is message processing like windows 3.1 ages programming style. Like this;
bool f_stop = false; //global
void CMyDialog::OnCancel() {
::f_stop = true;
}void Yield() {
MSG msg;
for (int j = 0; j < 100; j++) {
if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
DispatchMessage(&msg);
}
}
}void calc() {
for () {
for () {
for() {
// here is can quit calc
Yield();
if (f_stop) goto exit_all_loop;
}
}
}
exit_all_loop:
// clean up resources
}Above is rather old style but suitable some situations; (hate multi-threading or so.) But consider this code makes calc() so slower, so I think multi-threading is better.
-
thank u for ur suggestions, so u think i've to use a separated thread for this purpose? is it the best way? what if i want to avoid multi-threading problems by keeping it a single-thread app?
i think it's better to comply with using a thread for this purpose. it's better not to alter the ordinary msg loop functionality. it does additional works like TranslateMessage, calling application's OnIdle, etc. which i don't intend to implement separately nor to lose what they do. thank u very much