单击对话框关闭按钮时正常中止线程 [英] Abort thread properly when dialog box close button is clicked

查看:156
本文介绍了单击对话框关闭按钮时正常中止线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我的同事来自StackOverflow!

Hello my fellow colleagues from StackOverflow!

我会尽量简短,所以我会切入点:

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

我在Windows XP中使用纯Win32来创建一个对话框。

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.

然后线程从编辑控件收集文本,并使用OLE自动化将其写入MS Word文档。

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

一切正常,当我按下OK按钮,并等待线程显示填充的Word文档。

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

OK按钮,然后关闭对话框,而线程正在工作的中间,一个空白的Word文档弹出。

为了进一步说明我的问题是一些代码片段:

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;

我已经查看了MSDN的一个线索,发现只有ExitThread作为解决方案,因为我对线程没有经验,所以不知道如何正确使用它。

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.

通过SO archive浏览,我在C#中发现了一些例子,人们引入了布尔变量并测试它在while循环中的值,所以他们可以确定是否中止线程或让它工作。另一种方式被建议,其中线程放置在单独的进程,然后杀死。

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.

我的问题是:

我应该添加或更改什么,所以当我关闭对话框时,Word应用程序关闭线程销毁?

如果还有其他事情可以帮助,请问,我会很乐意这样做。

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.

推荐答案

如果你使用WinApi,你必须让线程句柄的其他部分代码。
然后终止你的线程你可以使用ExitThread - 这是MSDN的首选选项。我告诉你如何使用它:

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 );
}



现在当你想终止线程时,只需调用TerminateYourThread函数。它等待,直到线程关闭。 这只是建议不是最终解决方案,所以您可以在将来重构它。

这篇关于单击对话框关闭按钮时正常中止线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆