Click to See Complete Forum and Search --> : loop control


July 13th, 1999, 02:10 PM
Hello,

I am using MFC and right now I am trying to write a loop to capture images from A/D boards continously using _outp(). I used WHILE(!_kbhit()) to constructure the loop.The loop is supposed to be interupted by hitting any key. However, it did not work. I also tried WHILE(GetMessage(msg,NULL,0,0)). It is supposed to be interupted when I close window or select Exit from the menu. However, the loop was working,but could not be interupted because the window was frozen. Do you know what the problem might be. Do you have good ways to do this job. Could you kindly give me some samples or reference about response to keyboard.Any help is greatly appreciated.

Sincerely yours,

Jiasheng

bimmer boy
July 13th, 1999, 02:19 PM
what i have done in situations like this is to have my data reading loop in a worker thread. when the user clicks a button, or closes the window, or whatever other condition i choose to be the termination step, i just send a message to the thread. then the window goes about it's business closing while the thread starts to stop reading the data

miked

July 13th, 1999, 04:07 PM
Hi, Miked,

Thank you so much for your quick response. Your message helped me much. However, I am a beginner in Windows programming, I don't know much about thread, could you please give me a piece of sample? Many many thanks!

Best regards,

Jiasheng

bimmer boy
July 14th, 1999, 07:18 AM
well it's kinda hard for me to give an example, i can tell you what i do though.

create a controler function for the thread (must have the following function signature)
UINT MyThreadFunc(LPVOID pParam);

create a controler class for the thread
CMyThreadControler
{
private:
BOOL IsRunning;
...
public:
void Start();
void Stop();
...
}

In your dialog that wants to use the thread create a pointer to the controler class.

Ok now that you have these things here's the steps to creating the thread and allowing it to communicate to the dialog

Start The Thread: AfxBeginThread(MyThreadFunc,PointerToMyDialog);

In the thread, create an instance of CMyThreadControler
the LPVOID is a pointer to the dialog. so do something like this:
((CMyDialog*)pParam)->SetWorkerThread(&MyInstanceOfCMyThreadControler);

now inside the dialog you can use the CMyThreadControler to do things such as start and stop and whatever else you want with the thread. you can have something inside of MyThreadFunc like this:
while (MyInstaceOfCMyThreadControler.IsRunning())
{
readdata();
updatescreen();
sleep(100);
}


get the idea? it hard to give an example because my code does other things too. basically read up on AfxBeginThread, this will help alot

miked