简体   繁体   中英

Abort thread properly when dialog box close button is clicked

Hello my fellow colleagues from StackOverflow!

I will try to be brief, so I will cut to the point:

I work on Windows XP, in C++, using pure Win32 to create a dialog box.

That dialog box has some edit controls, and OK button, which activates a thread when pressed.

Thread then gathers text from edit controls and writes them into MS Word document, using OLE Automation.

Everything works fine,when I press OK button, and wait for thread to show filled Word document.

However, when I push the OK button and then close the dialog, while thread is in the middle of the work, a blank Word document pops up.

To further illustrate my problem here are some code snippets:

This is snippet for thread function:

    DWORD WINAPI TabelaSvihObjekata( LPVOID hWnd ) // hWnd is handle of the Dialog box
    {

            // obtain dialogs window handle

        HWND hwnd = (HWND)hWnd;

        // Initialize COM for this thread...

        CoInitialize(NULL);

        // Get CLSID for our server...

        CLSID clsid;

        HRESULT hr = CLSIDFromProgID(L"Word.Application", &clsid);

            // do other Automation stuff and clean afterwards
    }

In dialog box, this is the snippet for button handler:

   case IDOK:
   {
        // create thread

        DWORD threadID;

        HANDLE threadHandle = CreateThread( NULL , 0 , 
                            (LPTHREAD_START_ROUTINE)TabelaSvihObjekata , 
                            (void*)hwnd , 0 , &threadID );

        if( !threadHandle )
        {
           MessageBox( hwnd, L"Error", L"Error", MB_ICONERROR );

           EndDialog( hwnd, IDCANCEL );
        }

        CloseHandle( threadHandle );

   }

And this is the problematic handler:

    case IDCANCEL:

            EndDialog(hwnd, IDCANCEL);

        break;

I have looked on MSDN for a clue, and have found only ExitThread as a solution, but I don't know how to use it properly since I am inexperienced with threads.

Browsing through SO archive, I have found some examples in C# where people introduce boolean variable and test it's value in while loop, so they can determine whether to abort the thread or let it work.The other way was suggested, where thread is placed in separate process and then killed.

My question is:

What should I add or change, so when I close the dialog box, Word application closes along with threads destruction ?

If there is anything else that I can do to help, ask and I will gladly do it.

Thanks to everybody who tries to help.

If you use WinApi you have to make threadhandle accesible by other part of code. Then to terminate your thread you can use ExitThread - this is preferred option by MSDN. I show you how you can use it:

DWORD threadID;
HANDLE hthread;
void TerminateYourThread()
{
  DWORD exitCode;
  if(GetExitCodeThread(hThread,&exitCode) != 0) // check if your thread is active
  {
    ExitThread(exitCode); // terminating thread

    if(CloseHandle(hThread)) // closing handle
    {
       //
    }
  }
}

void CreateYourThread()
{
   hThread = CreateThread( NULL , 0 , 
                        (LPTHREAD_START_ROUTINE)TabelaSvihObjekata , 
                        (void*)hwnd , 0 , &threadID );
}

Now when you want to terminate the thread just call TerminateYourThread function. It waits until thread is closed. This is only suggestion not finally solution so you can refactor it in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM