Solved

Multithread with MFC 4.2

Posted on 1997-12-03
5
379 Views
Last Modified: 2013-11-20
How can I implement a multithread functionality in my program in order to start a ca. 2 minutes longing procedure without interrupting users work?
Please show me what I have to initialize, where do I have to place the code, how I call the routine and what have to be done after the procedure has finished?
0
Comment
Question by:trouvain
  • 3
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
IgorGrebnev earned 150 total points
ID: 1311438
In general you need to start additional thread with ( for example ) AfxBeginThread.
I show you haw I would implement the code:
Lets say you have dialog box with the button "Start Calculation"

ON_COMMAND( IDC_STARTCALC,OnStartCalculation)
ON_COMMAND( IDC_CALCFINISHED, OnCalcFinished )


UINT CalcProc( LPVOID pMyDlg )

{ return (CMyDlg*)pMyDlg->CalcProc();
}

void CMyDlg::OnStartCalculation()

{//---------- THREAD_PRIORITY_BELOW_NORMAL - thus the user //interface is not affected by calculation. You can also use
//THREAD_PRIORITY_NORMAL
  AfxBeginThread( ::CalcProc, this, THREAD_PRIORITY_BELOW_NORMAL );
//-- Updates user interface and variables to show that // calculation starts
......
}

UINT CMyDlg::CalcProc()

{//----------- Makes long calculation
  __int64 count = 0;
  while ( count++ < 0xFFFFFFFFFFFF )
  {
  }
//--------- The calulation finished. Updates the interface
  PostMessage( WM_COMMAND, IDC_CALCFINISHED, 0 );
//  Or optinally message box
  AfxMessageBox( "Calculation Finished" );
  return 0;
}

void OnCalcFinished()

{//-------- Updates user interface that calculation finished
...
}

When the function CalcProc() exits the calculation is finished.
You should be careful whiel accessing variables and user interface from CalcProc(), because it executes from contenst of another thread. There are problems with syncronisation and thread local storage. Thus I prefer to post command message to the dialog to announce finish of calculation, instead of taking some action inside of CalcProc().
I can give you more advises if you ask specific questions.
Yours, Igor.
   
0
 

Author Comment

by:trouvain
ID: 1311439
Thanks for it. After I tried it I immediatly was shocked that I wasn't aware to ask how I can throw out data by that thread:
This thread is created as a worker thread but also this thread has to report the process of his work. I do this by inserting text into a dialog box with an CEdit field in it. But this time it returns the failure "Access violation". therefore my question: how can I share this dialog box which is an element of my derived CMDIFrameWnd ?
Hope you can manage this!
0
 
LVL 1

Expert Comment

by:IgorGrebnev
ID: 1311440
Dear  trouvain,
I am sorry I didn't your comment - do you have dialog or class derived from CMDIFrameWnd, where the CEdit field.
IN general the multithreading with MFC is tricky, you cannot pass freely the CWnd pointer from one thread to another. IN general hte safe way is to update the internal data structures ( your data ) and post user-defined notification messages to the main window.

In my example you can access member variables of CMyDlg and you can change the test in the controls:
UINT CMyDlg::CalcProc()

{//----------- Makes long calculation
__int64 count = 0;
while ( count++ < 0xFFFFFFFFFFFF )
{ if ( count == 1000000 )
  { CWnd *pControl = GetDlgItem( IDC_SHOWNUMBER );
    char buffer[0x20];
    itoa( count, buffer, 10 );
    pControl->SetWindowText( buffer );
  }
}
//--------- The calulation finished. Updates the interface
PostMessage( WM_COMMAND, IDC_CALCFINISHED, 0 );
// Or optinally message box
AfxMessageBox( "Calculation Finished" );
return 0;
}
Write if you have more comments.
 
0
 

Author Comment

by:trouvain
ID: 1311441
Hi Igor,
Unfortunately are my surroundings different:
The worker thread is called from a message handler of CMDIMyFrame. My reporting routine is implemented as a dialog which is a member of CMDIMyFrame. Since there existing more than only one functionality I have a global report function. This function gets the dialog pointer via calling AfxGetMainWindow. But every time I try to get the dialog pointer by referencing ((CMDIMyFrame*)AfxGetMainWindow())->m_dlgReport->SetWindowText(...);
I get a access violation failure.
Is there at all a way to access data respective functions of another thread?
In case there is please explain me for a last time how to do it.
Many thanks in advance.
Marcel
0
 
LVL 1

Expert Comment

by:IgorGrebnev
ID: 1311442
Try to save the pointer to mainWnd in global variable and use it

glb_pMyMDIFrame->m_dlgReport->SetWindowText(...);
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 85
dog bark java program 15 82
java ^ examples 8 57
ShiftLeft challenge 21 66
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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…
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now