• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

How to convert member function to a thread

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
jejob007
Asked:
jejob007
  • 3
  • 2
1 Solution
 
mahesh1402Commented:
refer this sample :

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

MAHESH
0
 
mahesh1402Commented:
//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
 
jejob007Author Commented:
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
 
mahesh1402Commented:
jejob007
Is this relavent question with your original query ? anyways You can use CSplitterWnd::CreateView() function for this.

MAHESH
0
 
jejob007Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now