Solved

Multithread with MFC 4.2

Posted on 1997-12-03
5
380 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Generic progress indicator 6 113
How to know only "File created" with EventLog 3 32
FizzBuzz challenge 9 76
Unix Command -- Challenging  question 7 87
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 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

16 Experts available now in Live!

Get 1:1 Help Now