COM对象工作线程 [英] COM object worker thread

查看:65
本文介绍了COM对象工作线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

COM对象工作者线程
我只是在一个旧的C ++ COM对象中添加了一个简单的工作线程,该对象从C ++ MFC应用程序中使用.
问题是,当我关闭使用COM库的主应用程序时,辅助线程将不会退出,并且COM对象将永远挂在炼狱中.
这很奇怪,因为应用程序ExitInstance()都没有
也不会调用COM对象ExitInstance()函数,因此我无法关闭辅助线程句柄并清理互斥量.

请注意,这不是同步问题,因为即使我删除了同步,问题仍然存在.

以下是一些简化的代码段:

COM object worker thread
I just added a simple worker thread to an old C++ COM object, consumed from a C++ MFC application.
The problem is that when I close the main application which uses the COM library the worker thread won’t exit and the COM object will hang in the purgatory forever.
It’s very weird because neither the application ExitInstance()
Nor the COM object ExitInstance()functions will be called so I’m not able to close the worker thread handle and clean the mutex.

Mind you it’s not synchronization problem since even I remove the synchronization the problem persists.

Here is a little simplified code snippet:

//start the thread
BOOL CMyApp::StartParamThread() 
{
	unsigned idThread;
	ASSERT(m_hParamRefreshThread == NULL);
	
	try
	{
		bParamRefreshRunning = true;
		m_hParamRefreshThread = (HANDLE)_beginthreadex(NULL, 0, & CMyApp::RefreshDBParams, NULL, 0, &idThread);
	}
	catch(...)
	{
		return false;
	}
	ASSERT( m_hParamRefreshThread );	
	return true;
}

//thread function
unsigned __stdcall CMyApp::RefreshDBParams(LPVOID pParam)
{
	((CMyApp *) AfxGetApp())->hParamsMutex = CreateMutex (NULL, FALSE, NULL); class
	TRY
	{
		while(bParamRefreshRunning)
		{
			Sleep(5*60*1000); // 5 minutes
			WaitForSingleObject(((CMyApp *) AfxGetApp())->hParamsMutex, INFINITE);
			
			{do some important stuff}
			ReleaseMutex(THISAPP->hParamsMutex);
		}
	}
	CATCH (CException, e)
	{
		THISAPP->LogError(e);
		ReleaseMutex(((CMyApp *) AfxGetApp())->hParamsMutex);
		return 0;
	}
	END_CATCH
	return 0;
}



任何建议将不胜感激,在此先感谢您.



Any suggestions will be appreciated, thank you in advance.

推荐答案

检查是否正确调用Release(),以便在关闭应用程序时没有对COM对象的引用
check if you call Release() properly so when you close your app, there no references to COM object


我不确定您的意思是什么:

请记住,这不是同步问题,因为即使我删除了同步,问题仍然存在."

要回答您的问题,您的线程函数会执行以下两项操作:

1.睡5分钟.
2.等待发出互斥信号.

在执行这两个操作中的任何一个时,它都没有响应,并且不会发生其他任何事情.

1.睡一觉
2.使bParamRefreshRunning = false,然后在要关闭线程时向互斥量发出信号.您可能需要添加一些代码来解决{做一些重要的事情}.
3.调用_endthread(),然后等待线程句柄以确保线程已退出.
4. CloseHandle(threadhandle)
5. CloseHandle(mutex)

或:

使用WaitForSingleObject的最佳方法是检查返回值.
这样,可以将超时从INFINITE更改为一个较小的值-几秒钟以允许线程函数定期掉线.

I am not sure what you mean by:

"Mind you it''s not synchronization problem since even I remove the synchronization the problem persists."

To answer your question your thread function does 2 things:

1. Sleep for 5 minutes.
2. Wait for mutex to be signaled.

While it is doing either of those it is unresponsive and nothing else will happen.

1. Take out the sleep
2. Make bParamRefreshRunning = false then signal the mutex when you want to shut down the thread. You may have to add a little more code to get around {do some important stuff}.
3. call _endthread() then wait on the thread handle to ensure thread has exited.
4. CloseHandle(threadhandle)
5. CloseHandle(mutex)

OR:

The best way to use WaitForSingleObject is to check the return value.
This way the timeout can be changed from INFINITE to some small value - some seconds to allow the thread function to fall through regularly.

//thread function
unsigned __stdcall CMyApp::RefreshDBParams(LPVOID pParam)
{
	((CMyApp *) AfxGetApp())->hParamsMutex = CreateMutex (NULL, FALSE, NULL);
	TRY
	{
		while(bParamRefreshRunning)
		{

			DWORD retval = WaitForSingleObject(((CMyApp *) AfxGetApp())->hParamsMutex, 1000);

			if(WAIT_OBJECT_0 == retval)
			{

				if(bParamRefreshRunning)
				{	do some important stuff}


				ReleaseMutex(THISAPP->hParamsMutex);

			}

		}
	}
	CATCH (CException, e)
	{
		THISAPP->LogError(e);
		ReleaseMutex(((CMyApp *) AfxGetApp())->hParamsMutex);
		return 0;
	}
	END_CATCH
	return 0;
}


这篇关于COM对象工作线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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