Re: while loop w/ sleep(1) doesnt work
There are a number of threading APIs, but the one I'd recommend is Boost::Thread:
http://www.boost.org/doc/libs/1_37_0...ml/thread.html
since it seems likely that the threading model in the next C++ standard will be based on this one.
The most important concept of multi-threading is, don't let multiple threads touch the same variables at the same time. Or at least not while those variables might get modified. Heavy use of stack-based locals instead of dynamic allocation or globals makes this much easier to control. For the rest, you must protect access to shared variables using mutexes or other locking mechanisms.
Re: while loop w/ sleep(1) doesnt work
Quote:
Originally Posted by
acerunner316
any recommendations on where I should start learning multi thread programming. Given that I am a self-taught programmer...well, windows programming. I learned intro C++ in school.
the are a number of FAQ's on CG dealing with worker threads, multithreading...feel free to give it a search.
Re: while loop w/ sleep(1) doesnt work
>> I have a read-only editbox that displays status and percentage of the entire procedure, and this doesn't update anymore.
If you have a GUI that needs updating, then you'll have to pump messages to keep the GUI "alive".
Code:
bool PumpMessages(bool pump_all = false)
{
MSG msg;
const size_t max_pumps = 5;
size_t num_pumps = 0;
while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
return false;
TranslateMessage(&msg);
DispatchMessage(&msg);
++num_pumps;
if (!pump_all && num_pumps == max_pumps)
break;
}//while
return true;
}//PumpMessages
You could try putting a call to PumpMessages right before the end of the for-loop in Peak_canReadWait.
Since Vector and Kvaser API's support Event signaling, you can handle CAN and Windows messages efficiently with something like this:
Code:
const DWORD timeout = INFINITE;
DWORD status;
status = MsgWaitForMultipleObjects(1, &m_hPortNotificationEvent, FALSE,
timeout, QS_ALLINPUT);
if (status == (WAIT_OBJECT_0 + 1))
{
// there are messages to process
PumpMessages(true);
}//if
else if (status == WAIT_OBJECT_0)
{
// m_hPortNotificationEvent was signaled
// if you process CAN messages until the buffer is empty, you may
// need to "sprinkle" some calls to PumpMessages() in that loop also
}//else if
else if (status == WAIT_TIMEOUT)
{
// specified timeout occurred - when not using INFINITE timeout,
// but there's not reason not to use INFINITE
}//else if
gg