Re: Timer/ Multitasking??
Split ur appliction in two parts 1: Backend processing 2: UI Updation.
In Backend processing u have to use a thread which will get signal strength from ur source( Device ). and just insert such data in a shared Queue or Array and nothing else.
In UI Updation part another thread should get such inserted values from ur shared Queue or array one by one and pass it to UI for display graphs and meter.
In first part there is no need to take action with timer. u can put a sleep of 250ms in BackendThread. in Second part (UIUpdation) u just pop values from array or queue and send it to UI for display ( NO timer required at UI Updation thread ). Hope it mayl helps u.
Re: Timer/ Multitasking??
Inaddition to what Nauman suggested, the backend thread can communicate with the UI thread via sending (better option is to post message). If you plan to go via the route of posting message, then remember to put/copy the data (to be displayed) on heap (created via new/malloc) and send the pointer via postmessage. The UI thread should after displaying the data delete/free the memory. This way the two threads will communicate without locking and you dont need criticalsection to guard the Queue or Array of data.
Hope this helps,
Regards,
Usman.
Re: Timer/ Multitasking??
Thank you Naumaan and Usmann.
Is it worker thread (back end thread) you were referring to?
I am not familiar with multithread and is on the learning process.
what my question now is:
1.how to keep both of the thread looping unitil I request them to stop??
for example:
UINT MyThreadProc( LPVOID pParam )
{
.
.
.
}
this function will terminate at the end of line, am I right?? What should I do to keep it looping to collect signal strength from the device??
2.For the UI thread(for display purpose), what I do also to if I am not using timer to constantly update the meter? The same problem will occur as in question 1..
Sorry for these questions. It might sound easy for you to answer my question , but for me it's totally hard as I learn vc++ all by myself. Nobody coach and teach me.
Hope someone can show me how to do multithreading??
Re: Timer/ Multitasking??
Quote:
Is it worker thread (back end thread) you were referring to?
yes.
Quote:
1.how to keep both of the thread looping unitil I request them to stop??
for example:
UINT MyThreadProc( LPVOID pParam )
{
.
.
.
}
this function will terminate at the end of line, am I right?? What should I do to keep it looping to collect signal strength from the device??
you could do something like this in your thread:
Code:
while(TRUE)
{
dwResult = WaitForSingleObject(m_hPortNotificationEvent, INFINITE);
switch(dwResult)
{
case WAIT_OBJECT_0:
............................
where static HANDLE m_hPortNotificationEvent; is an event you can set using SetEvent() when data is coming from the device. everytime you have new data, set the event and process it in the worker thread. When you are done with the calculations, PostMessage(...) to the UI thread and display the signal.
Re: Timer/ Multitasking??
Thank you really much. I think I get the idea already.
Re: Timer/ Multitasking??
Quote:
Originally Posted by well_aisec
2.For the UI thread(for display purpose), what I do also to if I am not using timer to constantly update the meter? The same problem will occur as in question 1..
One other point, in the UI thread when you have nothing to do, you do "nothing". Don't add an infinite loop to this thread, as the system already has a message pump loop running for you in the background. All you have to do in your UI thread is respond to messages from the mouse/keyboard and your thread.
Viggy
Re: Timer/ Multitasking??
Quote:
Originally Posted by well_aisec
a Timer ... will trigger every 250ms ...
As the timer is triggered in fast speed...
Not arguing about the above suggestions, just want to note that 4 times a second is not THAT fast... You should be able to draw plenty in that time, without affecting your app's performance.
Want to share your OnTimer(), OnUpdate) and drawing code? Something has to be wrong there.
Re: Timer/ Multitasking??
Vladimir may well be right. In any case you should always protect Timer Functions against re-entrancy!
Re: Timer/ Multitasking??
Quote:
Originally Posted by VladimirF
Not arguing about the above suggestions, just want to note that 4 times a second is not THAT fast... You should be able to draw plenty in that time, without affecting your app's performance.
Want to share your OnTimer(), OnUpdate) and drawing code? Something has to be wrong there.
Yea, I understand 250ms is a little slow for display, but somehow the faster timer I use, the whole system will become irrespondsive due to other application(real time audio display ) is running also. Besides, the receiver need some delays to response to my command when signal strength value is requested.
SetTimer(ID_Update_3dmeter,250, 0);
void CPSMDlg::OnTimer(UINT nIDEvent)
{
switch (nIDEvent)
{
case ID_Update_3dmeter:
GetSmeter();
break;
.
.
.
}
}
void CPSMDlg::GetSmeter()
{
m_array[0]=0x00;
m_array[1]=0x00;
m_array[2]=0x00;
m_array[3]=0x00;
m_array[4]=0xE7;
send(); // these are some commands send to serial port the request for signal strength value;
Sleep(150); // I have added a 150ms delay here as the receiver need some time to response to my request, else I will get inaccurate value.
read(); // read S value from serial port and put in in[0]
dValue= in[0]; // the rest 2 lines are to update the meter display
m_3DMeterCtrl.UpdateNeedle(dValue);
}
Anything wrong with that?? Thank you.
Re: Timer/ Multitasking??
Quote:
Originally Posted by TheCPUWizard
Vladimir may well be right. In any case you should always protect Timer Functions against re-entrancy!
Sorry, what do you mean by re-entrancy?? Can further elaborate?
Re: Timer/ Multitasking??
Quote:
Originally Posted by well_aisec
Anything wrong with that??
You shouldn't Sleep() in the main thread at all, and you are sleeping 66% of the time (150ms of every 250ms). If you HAVE to Sleep(), you need another thread.
Alternatively, you could have two timers running: do a send() on one event and receive() on another.
Re: Timer/ Multitasking??
Quote:
Originally Posted by VladimirF
You shouldn't Sleep() in the main thread at all, and you are sleeping 66% of the time (150ms of every 250ms). If you HAVE to Sleep(), you need another thread.
Alternatively, you could have two timers running: do a send() on one event and receive() on another.
Thanks VladimirF. You are very nice to tell me all these. I didn't know I shouldn't place a sleep in a timer for delay. Looks like I should add 1 timer for send, then 150 ms later another timer for receive.
I will try out your method to see if It really improve the performance. Thanks.