Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Sharing the system

Posted on 1999-07-12
Medium Priority
Last Modified: 2013-11-20
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.


Dave P

PS: I would apreciate (and need!) specific details.
Question by:dave_p
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 1309638
Put Sleep(x) every now and again in you code. x=10to100

Accepted Solution

doin earned 200 total points
ID: 1309639
create a thread using AfxBeginThread, with an entry function to your programm as parameter
you are able to define the priority of your newly created thread with the parameter nPriority (see online doc.).
priority from THREAD_PRIORITY_HIGHEST to THREAD_PRIORITY_LOWEST ( 7 different states).

hope it helps
LVL 23

Expert Comment

ID: 1309640
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 :))


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

typedef struct tagTHREADPARMS {
    int nMax;
      HWND hWnd;

afx_msg LONG OnThreadFinished (WPARAM wParam, LPARAM lParam);

// Source file


void CMyApp::OnStartCrunch()
      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)

    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])
    delete[] pBuffer;
    return nCount;

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

721 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