COM Component Development Practices (eight)---multithreaded ActiveX controls and automatic resizing of ActiveX controls (bottom)

Source: Internet
Author: User
Tags thread

Disclaimer: This code is based on CodeProject's article, "A Complete ActiveX Web control Tutorial," and therefore also follows code Project Open License (Cpol).

In the previous article, COM Component Development Practice (VII)---Multithreaded ActiveX controls and automatic resizing of ActiveX controls (top) describes the basic requirements for using multithreading in ActiveX controls and presents a simple threading model, but there are unexpected problems. This article will try to give a feasible solution to the problem, and solve the second problem mentioned above.

In fact, the idea of the solution is also very simple, I have already thought of the beginning, is to use the thread postmessage to send a custom message to inform the main thread, the specific event has occurred, the demand main thread to respond. It's not a great idea, but I'm very scared of threading postmessage because it was the problem in a previous project that led to a memory leak, so the scheme was rejected by me at the outset.

All the way to find a solution can not be, had to CSDN forum on the post for the master, the specific discussion please refer to this thread:

Http://topic.csdn.net/u/20081226/17/9bf0ae08-c54d-4934-b1b2-91baa27ff76e.html

See Jameshooo (Hu Pehua) after the reply, or decided to return to the starting point, try to use postmessage this scheme.

First, two events are customized to indicate operation success and operation failure

#define WM_OPTSUCCESS wm_app+101//Operation successful

#define WM_OPTFAILED wm_app+102//operation failed

Then the callback function becomes very simple and requires only the post corresponding event.

/////////////////////
//回调函数
/////////////////////////
void CMyActiveXCtrl::OnSuccesful()
{//操作成功
  this->PostMessage(WM_OPTSUCCESS,(WPARAM)NULL,(LPARAM)NULL);
}

void CMyActiveXCtrl::OnFailed()
{//操作失败
  this->PostMessage(WM_OPTFAILED,(WPARAM)NULL,(LPARAM)NULL);
}

Overload the message-handling function WindowProc, in which you call an external JavaScript function or fire an event that the external page can respond to.

LRESULT CMyActiveXCtrl::WindowProc(UINT msg,WPARAM wParam,LPARAM lParam)
{
  switch (msg)
  {
  case WM_OPTSUCCESS:
    {//操作成功,通知外部页面
      CString strOnLoaded("OnLoaded");
      this->CallJScript(strOnLoaded);
      return 0;
    }
  case WM_OPTFAILED:
    {//操作失败,通知外部页面
      //这里不写了,同上面
    }
  }
  return  COleControl::WindowProc(msg,wParam,lParam);
}

To add the start work thread code to the OnCreate function:

int CMyActiveXCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
  if (COleControl::OnCreate(lpCreateStruct) == -1)
    return -1;
  m_MainDialog.Create(IDD_MAINDIALOG,this);
  pThread.SetICallBack(this);//设置主线程回调函数
  pThread.Start();//启动工作线程
  return 0;
}

Overload the OnClose function by adding code to close the worker thread:

void CMyActiveXCtrl::OnClose(DWORD dwSaveOption)
{
  pThread.Stop(true);//强行关闭工作线程
  COMRELEASE(pWebBrowser);
  COMRELEASE(pHTMLDocument);
  COleControl::OnClose(dwSaveOption);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.