Maintaining program control while in a loop...
-
Hi I'm currently trying to code a REAL-TIME midi remapper program in Visual C++ using MFC.. I need the program to constantly look for a input midi signal... The only way I have figured out how to do this is to run a constant loop that contains the midipoll(); I've had to set it to only recieve a certain number of signals so it will eventually end. I works great and recieves all of the messages.. but while in this loop I loose control of the rest of the program... The loop takes over. Is their a way I can do this without loosing control of the program?
-
Hi I'm currently trying to code a REAL-TIME midi remapper program in Visual C++ using MFC.. I need the program to constantly look for a input midi signal... The only way I have figured out how to do this is to run a constant loop that contains the midipoll(); I've had to set it to only recieve a certain number of signals so it will eventually end. I works great and recieves all of the messages.. but while in this loop I loose control of the rest of the program... The loop takes over. Is their a way I can do this without loosing control of the program?
Put that processing into a secondary thread. People that start writing code immediately are programmers (or hackers), people that ask questions first are Software Engineers - Graham Shanks
-
Put that processing into a secondary thread. People that start writing code immediately are programmers (or hackers), people that ask questions first are Software Engineers - Graham Shanks
Thanks.. that worked out great using a work thread... Now i'm having a problem accessing the UI from within the thread.. ie. a listbox I need to send info to the listbox from within the thread.. Any Ideas this is what I got... UINT MyThread( LPVOID pParam ); //prototye UINT MyThread( LPVOID pParam ) { while(RunThread) { If (inputdetected){ m_ListBox1.Additem("input detected"); // <-- I don't have access to ListBox 1 from withing this thread. } } return 0; // thread completed successfully } I call the thread to start with.. AfxBeginThread(MyThread, NULL); -- modified at 15:50 Thursday 6th April, 2006
-
Thanks.. that worked out great using a work thread... Now i'm having a problem accessing the UI from within the thread.. ie. a listbox I need to send info to the listbox from within the thread.. Any Ideas this is what I got... UINT MyThread( LPVOID pParam ); //prototye UINT MyThread( LPVOID pParam ) { while(RunThread) { If (inputdetected){ m_ListBox1.Additem("input detected"); // <-- I don't have access to ListBox 1 from withing this thread. } } return 0; // thread completed successfully } I call the thread to start with.. AfxBeginThread(MyThread, NULL); -- modified at 15:50 Thursday 6th April, 2006
You can not use the MFC control placeholders from the thread. Instead, it is better to use the listbox control's window handle directly to add elements to the listbox. The LPVOID pParam passed to thread could be a data structure, in that data structure, one of the fields could be the window handle for the list box control. So you call typedef struct { HWND hListBox; } MYSTRUCT ; MYSTRUCT MyThreadInfo; MyThreadInfo.hListbox = m_ListBox1.m_hWnd; then AfxBeginThread(MyThread, &MyThreadInfo); UINT MyThread( LPVOID pParam ) { MYSTRUCT* pMyThreadInfo = (MYSTRUCT*)pParam; while( RunThread ){ if( inputdetected ){ SendMessage(pMyThreadInfo->hListbox, LB_ADDSTRING, 0, (LPARAM)"input detected"); } } return 0; // thread completed successfully } People that start writing code immediately are programmers (or hackers), people that ask questions first are Software Engineers - Graham Shanks
-
You can not use the MFC control placeholders from the thread. Instead, it is better to use the listbox control's window handle directly to add elements to the listbox. The LPVOID pParam passed to thread could be a data structure, in that data structure, one of the fields could be the window handle for the list box control. So you call typedef struct { HWND hListBox; } MYSTRUCT ; MYSTRUCT MyThreadInfo; MyThreadInfo.hListbox = m_ListBox1.m_hWnd; then AfxBeginThread(MyThread, &MyThreadInfo); UINT MyThread( LPVOID pParam ) { MYSTRUCT* pMyThreadInfo = (MYSTRUCT*)pParam; while( RunThread ){ if( inputdetected ){ SendMessage(pMyThreadInfo->hListbox, LB_ADDSTRING, 0, (LPARAM)"input detected"); } } return 0; // thread completed successfully } People that start writing code immediately are programmers (or hackers), people that ask questions first are Software Engineers - Graham Shanks
AWSOME!!!!! everything works perfectly using this method... Thanks Blake ;)