Click here to Skip to main content
15,891,567 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: string vs CString problems. Pin
ThatsAlok28-Mar-05 20:27
ThatsAlok28-Mar-05 20:27 
GeneralRe: string vs CString problems. Pin
mlatimer29-Mar-05 0:17
mlatimer29-Mar-05 0:17 
GeneralRe: string vs CString problems. Pin
mlatimer29-Mar-05 0:18
mlatimer29-Mar-05 0:18 
GeneralRe: string vs CString problems. Pin
ThatsAlok29-Mar-05 0:43
ThatsAlok29-Mar-05 0:43 
GeneralRe: string vs CString problems. Pin
David Crow29-Mar-05 4:57
David Crow29-Mar-05 4:57 
Generalwsprintf help Pin
28-Mar-05 18:49
suss28-Mar-05 18:49 
GeneralRe: wsprintf help Pin
Michael Dunn29-Mar-05 0:00
sitebuilderMichael Dunn29-Mar-05 0:00 
QuestionPlease let me know the alternate to using threads? Pin
ledallam28-Mar-05 18:23
ledallam28-Mar-05 18:23 
Hello,

I am working with serial communication for interacting with a hardware device i.e ControlPanel. The Control Panel has many buttons on it. I need to track which key is pressed or released and give appropriate messages to the application. On key press, I started a thread to track the key down and repeat states of the key. Then when the next key is down, I am ending the previously started thread and starting a new thread to track the state of this new button. This is working fine for me. But the overhead associated with constantly spawning and ending threads can be quite significant for a real time application.
The reason for using threads is that tracking of key state process should not hinder the user interface.
Can anyone please suggest any alternative to using threads in the above scenario.

Below is my thread function code for your reference:
unsigned WINAPI CControlPanel::KeyMsgThread(void *i_param)
{

CControlPanel *cp = (CControlPanel*)i_param;

//key hit
//
string Key;
Key = cp->m_KeyValue;
int repeat_count = 1;

//Create an event object for that thread
//
cp->m_hStopEvent = CreateEvent(NULL, TRUE, TRUE, NULL);

if(cp->m_hStopEvent)
{
ResetEvent(cp->m_hStopEvent);
}
else
{
CloseHandle(cp->m_hStopEvent);
cp->m_hStopEvent = NULL;
cp->m_IsActive = false;
return 0; //exit the loop
}


//Check for delay time first
//
if(WaitForSingleObject(cp->m_hStopEvent, cp->m_Delay) == WAIT_OBJECT_0)
{
CloseHandle(cp->m_hStopEvent);
cp->m_hStopEvent = NULL;
cp->m_IsActive = false;
return 0; //exit the loop
}

//Stop processing data
//
if(cp->m_bKeyMsgThreadStart == false)
{
CloseHandle(cp->m_hStopEvent);
cp->m_hStopEvent = NULL;
cp->m_IsActive = false;
return 0;
}

//If the key is still pressed down after the delay time,
//post additional key down message to the application thread
//
cp->PostKeyStateMessage(Key,"Repeat",repeat_count);
repeat_count++;

//Check for repate rate after the delay
//
while (cp->m_bKeyMsgThreadStart )
{
//Check for repeat rate
//
if(WaitForSingleObject(cp->m_hStopEvent, cp->m_Repeat) == WAIT_OBJECT_0)
{
CloseHandle(cp->m_hStopEvent);
cp->m_IsActive = false;
cp->m_hStopEvent = NULL;
break; //exit the loop
}

//Stop processing data
//
if(cp->m_bKeyMsgThreadStart == false)
{
CloseHandle(cp->m_hStopEvent);
cp->m_IsActive = false;
cp->m_hStopEvent = NULL;
break;
}

//If the key is still pressed down after the repeat time,
//post additional key down message to the application thread
//
cp->PostKeyStateMessage(Key,"Repeat",repeat_count);
repeat_count++;
}

CloseHandle(cp->m_hStopEvent);
cp->m_IsActive = false;
cp->m_hStopEvent = NULL;
return 0;

}//KeyMsgThread

To start the thread:
//Start thread to track its state(up/down) change
unsigned thread_id;
m_hKeyMsgThread = (HANDLE)_beginthreadex(NULL, 0, CControlPanel::KeyMsgThread, (LPVOID)this, 0, &thread_id);

To end the thread:
/Post Key Up State message to application
PostKeyStateMessage(m_KeyValue,"Up");
//End the thread that keeps track of repeat and delay msgs
if(m_KeyValue.compare(m_LastKeyDown) == 0)
{
SetEvent(m_hStopEvent);
}


Your expert guidance will be of great help to me!

Thanks
Madhavi.
GeneralBMPs VC++ 7 Pin
Don1228-Mar-05 17:56
Don1228-Mar-05 17:56 
GeneralToolBar Pin
Anand for every one28-Mar-05 17:50
Anand for every one28-Mar-05 17:50 
GeneralLoading BMPS VC++7 Pin
Don1228-Mar-05 17:48
Don1228-Mar-05 17:48 
QuestionShare file\folder dialog? Pin
RobJones28-Mar-05 16:45
RobJones28-Mar-05 16:45 
QuestionHow does it piece of code work??? Pin
28-Mar-05 16:31
suss28-Mar-05 16:31 
AnswerRe: How does it piece of code work??? Pin
Christian Graus28-Mar-05 16:45
protectorChristian Graus28-Mar-05 16:45 
AnswerRe: How does it piece of code work??? Pin
toxcct28-Mar-05 18:39
toxcct28-Mar-05 18:39 
Generaldisplaying images in Visual C++ Pin
a-huge28-Mar-05 16:05
a-huge28-Mar-05 16:05 
GeneralRe: displaying images in Visual C++ Pin
Christian Graus28-Mar-05 16:15
protectorChristian Graus28-Mar-05 16:15 
GeneralRe: displaying images in Visual C++ Pin
a-huge28-Mar-05 16:30
a-huge28-Mar-05 16:30 
GeneralRe: displaying images in Visual C++ Pin
Christian Graus28-Mar-05 16:42
protectorChristian Graus28-Mar-05 16:42 
GeneralRe: displaying images in Visual C++ Pin
a-huge28-Mar-05 21:45
a-huge28-Mar-05 21:45 
GeneralRe: displaying images in Visual C++ Pin
Christian Graus29-Mar-05 12:13
protectorChristian Graus29-Mar-05 12:13 
GeneralCreate wizard in VC++ Pin
swati2428-Mar-05 15:17
swati2428-Mar-05 15:17 
GeneralRe: Create wizard in VC++ Pin
RobJones28-Mar-05 16:46
RobJones28-Mar-05 16:46 
GeneralRe: Create wizard in VC++ Pin
swati2428-Mar-05 18:02
swati2428-Mar-05 18:02 
GeneralLittle doubts Pin
Member 154898328-Mar-05 14:56
Member 154898328-Mar-05 14:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.