Jump to content

Assistance required with MFC application development

ClobberXD

I've scoured the internet for some MFC tutorials that are actually comprehensible, but in vain. I'm stuck in the middle of my coding, where I need to initiate a loop, when a button is pressed. I've created a button, but I don't know how to assign a function to it. I'll be grateful if someone could make me understand.

 

Thanks!

Nothing to see here ;)

Link to comment
Share on other sites

Link to post
Share on other sites

In visual studio's form editor where you added the button, just doubleclick the button and visual studio will add the function for you and immediately go there in the editor.

 

Do note that starting a loop from a button press is a bad idea. You should never lock up the main framework thread (that handles events). If you do you will get a "window not responding" as long as the thread is stuck in your loop and the window won't react anymore.

 

The correct way is for the button handling code to launch a new thread to do the actual work and return immediately.

Link to comment
Share on other sites

Link to post
Share on other sites

7 hours ago, Unimportant said:

In visual studio's form editor where you added the button, just doubleclick the button and visual studio will add the function for you and immediately go there in the editor.

 

Do note that starting a loop from a button press is a bad idea. You should never lock up the main framework thread (that handles events). If you do you will get a "window not responding" as long as the thread is stuck in your loop and the window won't react anymore.

 

The correct way is for the button handling code to launch a new thread to do the actual work and return immediately.

Can you tell me how to create new thread to run the loop?

 

BTW Planning to add progress bar to show progress of loop (loop runs for exactly 30 secs.). Can you also help me with that?

 

Thanks!

Nothing to see here ;)

Link to comment
Share on other sites

Link to post
Share on other sites

You can create a new thread with the 'CreateThread' function: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682453%28v=vs.85%29.aspx

The actual thread function must be of type 'DWORD WINAPI func(LPVOID)' where the LPVOID parameter is a value that can be passed to the thread function in the call to 'CreateThread' and the DWORD return value should be the thread's exit code.

 

Example:

//The thread function...
DWORD WINAPI
MyThread(LPVOID)
{
	//Thread code

	return 0;
}

//Creating the thread...
HANDLE MyThreadHandle;
MyThreadHandle = CreateThread(NULL, 0, MyThread, 0, 0, 0);
if (MyThreadHandle == NULL)
{
	//Error creating thread...
}

As for how to have the thread send events so the main thread can update the progress bar etc - here's a link to a SO answer i gave to save my having to copy it or type it all again...

http://stackoverflow.com/questions/36778173/gui-with-c-lagged-when-i-use-button/36785506#36785506

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×