?
Solved

How to convert member function to a thread

Posted on 2006-04-05
5
Medium Priority
?
292 Views
Last Modified: 2013-11-20
Hi,

I would like to know how to convert a member function in a class to a thread.My application is made in "Win32 Application" and here is my program format:


#include <stdafx.h>
#include <afxcmn.h>
#include <process.h>
#include "resource.h"
#include "stdio.h"
#include "stdlib.h"



// 'ABOUT' DIALOG BOX

class aboutdialog : public CDialog
{
public:

      aboutdialog(int n) : CDialog(n)
      {
      }

      void OnOK()
      {
            CDialog :: OnOK();
      }
};




class myframe : public CFrameWnd
{
private:

      CString mainwindow;
      CMenu menu;
      CListCtrl tablist;
      HBRUSH bckcolorbrush;


public:
      
      myframe()
      {
            bckcolorbrush = (HBRUSH)::GetStockObject(LTGRAY_BRUSH);

            mainwindow = AfxRegisterWndClass (CS_HREDRAW|CS_VREDRAW,0,bckcolorbrush,0);

            Create (mainwindow, "Packet Sniffer");
      }




// MESSAGE HANDLERS

      int OnCreate (LPCREATESTRUCT l)
      {
            CFrameWnd :: OnCreate (l);


//here i write code for message handler for MENUS and  LIST CONTROLS


            return 0;
      }




// MENU FUNCTIONS

      void open()
      {
            MessageBox ("Code for Open Frame", "Open Frame");
      }

      void save()
      {
            MessageBox ("Code for Save Frame", "Save Frame");
      }

      void exit()
      {
            _exit(0);
      }

                void display()
                {
           
                             MessageBox ("Code for Display", "Display");

                }


      void about()
      {
            aboutdialog d (IDD_DIALOGBAR1);
            d.DoModal();
      }

      DECLARE_MESSAGE_MAP()
};



BEGIN_MESSAGE_MAP (myframe, CFrameWnd)

ON_WM_CREATE()

ON_COMMAND (101, open)
ON_COMMAND (102, save)      //here i write all the message maps
ON_COMMAND (103, exit)                //for menus and sub menus
ON_COMMAND (501, about)


END_MESSAGE_MAP()




class myapp : public CWinApp
{
public:
      int InitInstance()
      {
            myframe *p;
            p=new myframe;
            p->ShowWindow(3);
            m_pMainWnd = p;

            return 1;
      }
};

myapp a;


//END OF PROGRAM



Now I would to convert the function display into a thread.Can any one tell me what code i should add to convert this member function to thread preferably a workers thread.


Thanks

Jejo
0
Comment
Question by:jejob007
  • 3
  • 2
5 Comments
 
LVL 22

Expert Comment

by:mahesh1402
ID: 16379280
refer this sample :

http://www.codeproject.com/threads/memberthread.asp <====

MAHESH
0
 
LVL 22

Accepted Solution

by:
mahesh1402 earned 200 total points
ID: 16379326
//declare display function like following in header :
static UINT display(LPVOID param);

typedef struct THREADSTRUCT
{
    myframe*    _this;
        //you can add here other parameters you might be interested on
} THREADSTRUCT;


// function imple.
UINT display(LPVOID param)
                {
                           THREADSTRUCT*    ts = (THREADSTRUCT*)param;
                             MessageBox ("Code for Display", "Display");

                }


//Now call the thread on a button action or menu
    THREADSTRUCT *_param = new THREADSTRUCT;
    _param->_this = this;
    AfxBeginThread (display, _param);
0
 

Author Comment

by:jejob007
ID: 16475524
Hi,

Thanks mahesh1402 for ur valuable post.My thread is working properly.I have another problem I hope u can solve that too.

How do I split the main frame into two parts - top and bottom with the above code format.I tried to use the CSplitterWnd class but it is not working.Iam a newbie in this topic of MFC.

Hope u could help me.

Thanks

Jejo
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 16475561
jejob007
Is this relavent question with your original query ? anyways You can use CSplitterWnd::CreateView() function for this.

MAHESH
0
 

Author Comment

by:jejob007
ID: 16476364
Hi

Yes it is a relevat question with the original query.I would like to know what code to add to split the window or changes I need to make for the existing one.Should I change the entire frame class.

Please help.

Thanks

Jejo

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question