Solved

VC++ HELP

Posted on 2003-12-05
4
648 Views
Last Modified: 2012-08-14
My Menus In My VC++ Program Become Inactive When Doing Mathematical Calculations. How Can I Make Them Availible?

Also, How can i make a function run on startup of the program. MFC Aplication
0
Comment
Question by:Deathbob
  • 2
4 Comments
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
Comment Utility
Your (very short) description sounds like you have a program that should start a lengthly calculation, but you still want to use the user interface.

You should create a multi-threaded application with one worker thread (that does the calculation), and a UI thread, that is responsible for handling the user interface.

This article shows how you can do this: http://www.codeguru.com/misc/lenop.html
0
 
LVL 1

Author Comment

by:Deathbob
Comment Utility
Here is part of the script so you caan see how it is layed out.
This was originaly a C++ application that im converting to VC++.
My main problem is the pseudo_main function must be in the class so i can display info in the program (as far as i know).
Granted im a C++ programer, not so much VC++


// GUIPIDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GUIPI.h"
#include "GUIPIDlg.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/* #define HAS_LONG_LONG */

#ifdef HAS_LONG_LONG
#define mul_mod(a,b,m) (( (long long) (a) * (long long) (b) ) % (m))
#else
#define mul_mod(a,b,m) fmod( (double) a * (double) b, m)
#endif

/////////////////////////////////////////////////////////////////////////////
// CGUIPIDlg dialog

CGUIPIDlg::CGUIPIDlg(CWnd* pParent /*=NULL*/)
      : CDialog(CGUIPIDlg::IDD, pParent)
{
      //{{AFX_DATA_INIT(CGUIPIDlg)
      m_nvalue = _T("");
      m_Start = 0;
      m_Stop = 0;
      m_CR = _T("");
      m_CUR = _T("");
      m_TET = _T("");
      m_CET = _T("");
      m_LT = _T("");
      //}}AFX_DATA_INIT
      // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
      m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGUIPIDlg::DoDataExchange(CDataExchange* pDX)
{
      CDialog::DoDataExchange(pDX);
      //{{AFX_DATA_MAP(CGUIPIDlg)
      DDX_Control(pDX, IDC_PROGRESS1, m_P);
      DDX_Text(pDX, IDC_EDIT6, m_nvalue);
      DDX_Text(pDX, IDC_Start, m_Start);
      DDX_Text(pDX, IDC_Stop, m_Stop);
      DDX_Text(pDX, IDC_EDIT1, m_CR);
      DDX_Text(pDX, IDC_EDIT2, m_CUR);
      DDX_Text(pDX, IDC_EDIT3, m_TET);
      DDX_Text(pDX, IDC_EDIT4, m_CET);
      DDX_Text(pDX, IDC_EDIT5, m_LT);
      //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGUIPIDlg, CDialog)
      //{{AFX_MSG_MAP(CGUIPIDlg)
      ON_WM_PAINT()
      ON_WM_QUERYDRAGICON()
      ON_BN_CLICKED(ID_Start, OnStart)
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGUIPIDlg message handlers

BOOL CGUIPIDlg::OnInitDialog()
{
      CDialog::OnInitDialog();

      // Set the icon for this dialog.  The framework does this automatically
      //  when the application's main window is not a dialog
      SetIcon(m_hIcon, TRUE);                  // Set big icon
      SetIcon(m_hIcon, FALSE);            // Set small icon
      
      // TODO: Add extra initialization here
      
      return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CGUIPIDlg::OnPaint()
{
      if (IsIconic())
      {
            CPaintDC dc(this); // device context for painting

            SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

            // Center icon in client rectangle
            int cxIcon = GetSystemMetrics(SM_CXICON);
            int cyIcon = GetSystemMetrics(SM_CYICON);
            CRect rect;
            GetClientRect(&rect);
            int x = (rect.Width() - cxIcon + 1) / 2;
            int y = (rect.Height() - cyIcon + 1) / 2;

            // Draw the icon
            dc.DrawIcon(x, y, m_hIcon);
      }
      else
      {
            CDialog::OnPaint();
      }
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CGUIPIDlg::OnQueryDragIcon()
{
      return (HCURSOR) m_hIcon;
}

/* returns the inverse of x mod y */

int inv_mod(int x,int y) {
  int q,u,v,a,c,t;

  u=x;
  v=y;
  c=1;
  a=0;
  do {
    q=v/u;
   
    t=c;
    c=a-q*c;
    a=t;
   
    t=u;
    u=v-q*u;
    v=t;
  } while (u!=0);
  a=a%y;
  if (a<0) a=y+a;
  return a;
}

/* returns (a^b) mod m */

int pow_mod(int a,int b,int m)
{
  int r,aa;
   
  r=1;
  aa=a;
  while (1) {
    if (b&1) r=mul_mod(r,aa,m);
    b=b>>1;
    if (b == 0) break;
    aa=mul_mod(aa,aa,m);
  }
  return r;
}
     
/* returns true if n is prime */

int is_prime(int n)
{
   int r,i;
   if ((n % 2) == 0) return 0;

   r=(int)(sqrt(n));
   for(i=3;i<=r;i+=2) if ((n % i) == 0) return 0;
   return 1;
}

/* returns the prime number immediatly after n */

int next_prime(int n)
{
   do {
      n++;
   } while (!is_prime(n));
   return n;
}

/* finds out nine digits of pi starting from n */

/* will probably be the main function in openmosix capable version*/

int CGUIPIDlg::pseudo_main(int n, double cst, int AA, int BB)
{
  int av,a,vmax,N,num,den,k,kq,kq2,t,v,s,i;
  long double sum;
  char filename[100];
  FILE *f_out;
 

  double start_time, elap_time, cstb;
  start_time = (double) clock ();

  sprintf (filename, "%i.pib", n);
  f_out = fopen (filename, "w");

  N=(int)((n+20)*log(10)/log(2));

  sum=0;


  for(a=3;a<=(2*N);a=next_prime(a)) {
    vmax=(int)(log(2*N)/log(a));
    av=1;
    for(i=0;i<vmax;i++) av=av*a;

    s=0;
    num=1;
    den=1;
    v=0;
    kq=1;
    kq2=1;

    for(k=1;k<=N;k++) {

      t=k;
      if (kq >= a) {
       do {
          t=t/a;
          v--;
       } while ((t % a) == 0);
       kq=0;
      }
      kq++;
      num=mul_mod(num,t,av);

      t=(2*k-1);
      if (kq2 >= a) {
       if (kq2 == a) {
          do {
             t=t/a;
             v++;
          } while ((t % a) == 0);
       }
       kq2-=a;
      }

      den=mul_mod(den,t,av);
      kq2+=2;
     
      if (v > 0) {

      t=inv_mod(den,av);
      t=mul_mod(t,num,av);
      t=mul_mod(t,k,av);
      for(i=v;i<vmax;i++) t=mul_mod(t,a,av);
      s+=t;
      if (s>=av) s-=av;
      }
     
    }

    t=pow_mod(10,n-1,av);
    s=mul_mod(s,t,av);

    sum=fmod(sum+(double) s/ (double) av,1.0);
      elap_time = ((double) clock () - (double) start_time) / CLOCKS_PER_SEC;
      cstb = ((double) clock () - (double) cst) / CLOCKS_PER_SEC;
      m_TET.Format("%f",cstb);
      m_CET.Format("%f",elap_time);
UpdateData(FALSE);
UpdateWindow( );

  }

  elap_time = ((double) clock () - (double) start_time) / CLOCKS_PER_SEC;
//  printf("Position: %9d Digits: %09d Time: %9.2lf seconds\n",n,(int)(sum*1e9),elap_time);
m_nvalue.Format("%09d",(int)(sum*1e9));
m_LT.Format("%f",elap_time);
UpdateData(FALSE);
UpdateWindow( );
  fprintf (f_out, "%09d\n", (int)(sum*1e9));
  fclose (f_out);
  sprintf (filename, "start.pib");
  f_out = fopen (filename, "w");
  fprintf (f_out, "%d\n", n);
  fclose (f_out);

  sprintf (filename, "piw/data.txt");
  f_out = fopen (filename, "w");
  fprintf (f_out, "%i-%i\n%s\n%f\n%f\n%i", AA, AA+BB, m_CUR, cstb, elap_time, m_nvalue); //, m_CUR, m_TET, m_CET, m_LT, m_nvalue);
  fclose (f_out);


  return 0;
}

void CGUIPIDlg::OnStart()
{

int position=1,counter=1,count=1,number;
  double start_time, elap_time;
  char filename_in[100], filename_out[100];
  FILE *f_out, *f_in;

   /* sprintf (filename_in, "start.pib");
      f_in = fopen (filename_in, "r");

    number=gets (f_in);
        fclose (f_in);

fclose (f_out);

*/

//      printf("\nPlease type the number of digits of pi you want to compile.\n");
//   printf("\nNumber of Digits = ");
//    scanf("%d",&number);
//    printf("\n\nStart of Digits = ");
//    scanf("%d",&count);

  start_time = (double) clock ();

  UpdateData(TRUE);

  number = m_Stop;
  count = m_Start;

  number = number / 9;
  number = number * 9;

  //count=number+500;

  count = count / 9;
  count = count * 9;


m_CR.Format("%i-%i",count,count+number);
UpdateData(FALSE);
UpdateWindow( );

  if (count <= 0)
  {
  count = 1;
  }
  if (count > 1)
  {
  count++;
  }
//  printf("\nComputing %d digits of pi ...\n\n",number-count);

  counter=count;
  while (counter<number+count)
      {
m_CUR.Format("%i-%i",counter,counter+9);
m_P.SetPos(((counter-count)*100)/number);
UpdateData(FALSE);
UpdateWindow( );
      pseudo_main(counter, start_time, number, count);
      counter=counter+9;
      }

  elap_time = ((double) clock () - (double) start_time) / CLOCKS_PER_SEC;



 // printf("\nTotal calculation done in: %9.2lf seconds.\n",elap_time);

 // printf("\nGenerating output file ...\n");
  start_time = (double) clock ();

  sprintf (filename_out, "pi_%i_%i.txt", count, number);
  f_out = fopen (filename_out, "w");
  if (count == 1)
  {
  fprintf(f_out, "3.");
  }
  counter = count;
  while (counter<number)
      {
      sprintf (filename_in, "%i.pib", counter);
      f_in = fopen (filename_in, "r");
      position = 1;
      while (position<10)
            {
            putc((getc (f_in)),f_out);
            position=position+1;
            }
      counter=counter+9;
      fclose (f_in);

// Next line contributed by Arko Provo Mukherjee, arkoprovo@peacefulaction.org
//      remove (filename_in);

      }

  fclose (f_out);

  elap_time = ((double) clock () - (double) start_time) / CLOCKS_PER_SEC;
  //printf("\n%s generated in: %9.2lf seconds.\n",filename_out,elap_time);

}
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 50 total points
Comment Utility
There are two ways to make program responsive. Simple way is using DoEvents:

void DoEvents()
{
    MSG msg;
   
    while ( ::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE ) )
    {  
        if ( ::GetMessage(&msg, NULL, 0, 0))
        {
            ::TranslateMessage(&msg);
            ::DispatchMessage(&msg);
        }
        else
        {
            break;
        }
    }
}

Now when you have some long loop call DoEvents from it periodically:

while ( ... )
{
    // some operations
   
    DoEvents();          // here window is redrawn, menu items are handled etc.
}

Another way us using multi-threading. Check out MFC sample MTRECALC.
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
Comment Utility
You can call the DoEvents() function e.g. at the start of your two for lools in the pseudo_main function.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

728 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

14 Experts available now in Live!

Get 1:1 Help Now