Link to home
Start Free TrialLog in
Avatar of dave_p
dave_p

asked on

Sharing the system

I have an MFC application that I wrote that is a real number cruncher. It may run for several hours. It seems to hog the system to such a degree that the system is not really usable for any other purposes. How should I make my application be nicer to other applications? What do I need to include in my loops that will allow other applications to get some CPU? I can accept some speed loss in my application if need be.

Thanks!

Dave P

PS: I would apreciate (and need!) specific details.
Avatar of Bridge
Bridge

Put Sleep(x) every now and again in you code. x=10to100
ASKER CERTIFIED SOLUTION
Avatar of doin
doin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is some sample code which creates a thead.. do your number crunching. And when it is done, it posts a message for the parent window so that the parent can take the post crunch actins on it :))



// In Header fIle :))


#define WM_USER_THREAD_FINISHED WM_USER+0x100

UINT ThreadFunc (LPVOID pParam);
int Sieve (int nMax);

typedef struct tagTHREADPARMS {
    int nMax;
      HWND hWnd;
} THREADPARMS;

afx_msg LONG OnThreadFinished (WPARAM wParam, LPARAM lParam);

// Source file

BEGIN_MESSAGE_MAP(CMyApp, CWinApp)
      //{{AFX_MSG_MAP(CMyApp)
      //}}AFX_MSG_MAP
      ON_MESSAGE (WM_USER_THREAD_FINISHED, OnThreadFinished)
END_MESSAGE_MAP()

void CMyApp::OnStartCrunch()
{
      THREADPARMS* ptp = new THREADPARMS;
      ptp->nMax = nMax;
      ptp->hWnd = m_hWnd;
      AfxBeginThread (ThreadFunc, ptp);
}

LONG CMyApp::OnThreadFinished (WPARAM wParam, LPARAM lParam)
{
      int result = (int) wParam;
 
    return 0;
}


UINT ThreadFunc (LPVOID pParam)
{
      THREADPARMS* ptp = (THREADPARMS*) pParam;
      int nMax = ptp->nMax;
      HWND hWnd = ptp->hWnd;
      delete ptp;

    int nCount = CrunchNumbers (nMax);
    ::PostMessage (hWnd, WM_USER_THREAD_FINISHED, (WPARAM) nCount, 0);
    return 0;
}

int CrunchNumbers (int nMax)
{
    PBYTE pBuffer = new BYTE[nMax + 1];
    ::FillMemory (pBuffer, nMax + 1, 1);

      // Do your VOODOO :))

/*
    int nLimit = 2;
    while (nLimit * nLimit < nMax)
        nLimit++;

    for (int i=2; i<=nLimit; i++) {
        if (pBuffer[i]) {
            for (int k=i + i; k<=nMax; k+=i)
                pBuffer[k] = 0;
        }
    }

    int nCount = 0;
    for (i=2; i<=nMax; i++)
        if (pBuffer[i])
            nCount++;
*/
    delete[] pBuffer;
    return nCount;
}