Click to See Complete Forum and Search --> : problem with multithreading and wrapper-class objects


Ky.
September 12th, 2007, 11:11 AM
Hi guys,

I´m not sure I am using the corect terminology, since I´m pretty new to MFC programming. Sorry for that.

I created an object of a wrapper-class from an OCX libray (included with class _myObjectClass : public COleDispatchDriver).

As opposed to other classes in this library and what "OCX" suggests, this one is non-visual (no ActiveX).

I can use this object in a function just fine, except for when calling the function from a thread.


This works fine:

void TestDlg::DoIt()
{
_myObject m_obj;

m_obj.CreateDispatch("xxxx.xxxx");

m_obj.ObjMethod(xxx);
}


void TestDlg::OnButton()
{
DoIt();
}



But this does not

static UINT MyThread(LPVOID p);

UINT TestDlg::MyThread(LPVOID p)
{
TestDlg * app=(TestDlg *) p;

app->DoIt();

return 0;
}

void TestDlg::DoIt()
{
_myObject m_obj;

m_obj.CreateDispatch("xxxx.xxxx");

m_obj.ObjMethod(xxx);
}

void TestDlg::OnButton()
{
AfxBeginThread(MyThread, this);
}


Can anyone explain that behaviour or even suggest a solution?

Any advice or idea is greatly appreciated.

Thanks in advance,
..ky.


P.S. I already implemented a version, where PostMessage from MyThread calls the DoIt function, instead of calling it directly. It works, but it blocks my dialogue while DoIt is executed. But I need to run methods of m_obj while the main thread does other things...

Arjay
September 12th, 2007, 12:38 PM
You need to call CoIniitialize/CoUninitiazed at the start/end of the thread proc. There may be other issues but for sure you need to at least initialize COM in the thread it is being used.

Ky.
September 12th, 2007, 01:22 PM
Thanks a lot for your reply and for the catchwords. I´ll try and see what I can make out of that.

..Ky.

Ky.
September 12th, 2007, 02:03 PM
Using CoInitalize and UnInitialize worked!

You saved my day, week and project.

Many many thanks,
..Ky.