You have to create a worker thread that performs the operation in the background.
But MFC is not thread safe so that you have to implement some kind of signaling like sending user defined Windows messages if you want to update GUI elements from the thread.
You might also need to track the state of the thread (running or exited) to avoid starting it again / another one and signal your main thread when the worker thread is terminating after the work is done. If the worker thread might run for a longer time it might be also necessary to implement a kill mechanism (called upon a Stop button press or when the application is terminated).
Some readings:
Multithreading with C++ and MFC[
^]
Using Worker Threads[
^]
Simple Thread: Part I[
^]