Jump to content

progress bar is not working properly while minimize the application

- - - - -

  • Please log in to reply
9 replies to this topic

#1
wizard

wizard

    Newbie

  • Members
  • PipPip
  • 12 posts
Hello all,

I am using Microsoft visual C++ 8 for development. In this i am loading 72 images into application.

I am trying to show a progress bar while my process is going on.i.e loading images.., while this progress is under process minimize the current application and maximize the progress bar is not working properly that means application shows blank area.

I got some information in Google. Implement BackGroundWorker threads to solve this problem.

Please help me to implement threads in VC++.

Thanks in Advance.

Edited by wizard, 02 November 2010 - 03:58 AM.


#2
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

I'm assuming that you're using Progress Control in your project and you're developing a MFC dialog application

1) Create a function in your Dlg class SetProgress(int currentProgress) which calls progress control object SetPos(int nPos)
2) Declare a global variable that contains the current progress value(0-100).
3) Create a thread in InitDialog function, and pass it to your Dlg object pointer.
4) In Thread, after few Milli second say 500 call SetProgress(GlobalVariable) using the dlg object pointer.

I hope this should help!

Munir

#3
wizard

wizard

    Newbie

  • Members
  • PipPip
  • 12 posts
Hello mnirahd,

Thanks for your reply..

ya, I am using progress control and developing MFC dialog application only..I will post my implementation for your response.

I understand first two steps in your reply But i didn't get 3rd and 4th steps, B' Coz i am very bad in threads concept.

Please help to implement threads.

Progress Bar Implementation:

I take a new dialog, created a new class for that and placed a progress bar control on dialog.

The following code was inserted in OnInitDialog() in progress class.

m_Progress.SetWindowPos(&wndTop,10,1,wh-150,40,SWP_SHOWWINDOW);

Here m_Progress is a variable for progress bar control.

In loading images function i am inserted the following code to show progress bar.

m_Dlg = new ProgDlg(this);
m_Dlg->Create();

Here m_Dlg is object for pogress bar class.

ProgressBar updating:

For this, i wrote one function in progress bar class and set image index to that function. call this function in for loop in loading images function.

m_Progress.SetPos(count); this code is used to update progress bar.

#4
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts

wizard said:

m_Progress.SetWindowPos(&wndTop,10,1,wh-150,40,SWP_SHOWWINDOW);

m_Dlg = new ProgDlg(this);
m_Dlg->Create();

Here m_Dlg is object for pogress bar class.

you told me that m_Dlg is progress bar class object, How about m_Progress? What is that?

you used m_Progress to set position of Progress Control Window, and you're creating it through another object m_Dlg: I'm a bit confused about it. Can you post a bit of your Progressbar Control class and OnInitDialog function along with the definition of m_progress and m_dlg variables. Also post code where you're working with images. Then I can direct you how to implement progress bar more clearly

About Threads you can have a detail on .

Processes and Threads (Windows)


I hope this helps

Munir

#5
wizard

wizard

    Newbie

  • Members
  • PipPip
  • 12 posts
Hello mnirahd,

m_Progress is a variable for progress bar control and m_Dlg is a object for progress dialog class.

code:

Iam loading more number of images into my application,apply some changes to those images and save all images into a target folder. For this task i implemented MFC application. when i press SaveAll button in view, it callls progress dialog class and set progress position in that. while performing this task, if i minimize the application and maximize application then whole application shows only white area. If once task completed then application works as fine.

CODE:

SaveAllImage() in View Class.

CString temp=m_SavePath;
int num=pDoc->GetNoOfImages(); //Get Total num of loaded images

m_Dlg = new ProgDlg(this); // Create object for dialog class
m_Dlg->Create();

m_Dlg->SetRange(num-1); // call SetRange() in progress dialog class for setting min and max range of progress bar


for(int i=0;i<=num;i++) // loop for start image to end image
{
CString ImageName=pDoc->m_ImageNames[i]; // Get Image name
m_SavePath=m_SavePath+"\\"+ImageName+"_DepthMap"+pDoc->GetImageExten(); // Get Image Path


m_Dlg->SetWindowText (ImageName+" Saving....."); // Set windows text to Dialog class.

GoldImage Test;
Test.SetImage(m_DMapImage);
Test.SaveImage(m_DMapSavePath); // Save Image
m_SavePath=temp;

m_Dlg->SetStepCount(i); // calls SetStepCount() in Dialog Class for setting progress bar position.

}

Progress Class:

Implemented the following methods.

BOOL ProgDlg::OnInitDialog() // Set progressbar windows position
{
CDialog::OnInitDialog();

int wh=GetSystemMetrics(SM_CXFULLSCREEN);
int he=GetSystemMetrics(SM_CYFULLSCREEN);

m_Progress.SetWindowPos(&wndTop,10,1,wh-150,40,SWP_SHOWWINDOW);

return TRUE;
}

void ProgDlg::SetRange(short m_MaxCount) // Set progress bar range
{
m_Progress.SetRange(0,m_MaxCount);
}

void ProgDlg::SetStepCount(int count) // Set progress bar position
{
m_Progress.SetPos(count);
}

Edited by Alexander, 04 November 2010 - 01:35 PM.
Added code formatting


#6
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

My suggestions

1. Do not create m_dlg dialog in SaveAll function in View Class. Instead of doing this, you can simply create a thread.

2. Declare two Global Variables,




int CurrentProgressCount;

CString CurrentFile;




This would change your SaveAll function and it may appear like.




//SaveAllImage() in View Class.


CurrentProgressCount = 0;

CurrentFile = "";


CString temp=m_SavePath;

int num=pDoc->GetNoOfImages(); //Get Total num of loaded images


[B]/[COLOR="Red"]/Create a Thread routine.

(HANDLE)_beginthreadex( NULL, 0, &ProgressDlgThread, (void *) this, 0, 0);[/COLOR]

[/B]



for(int i=0;i<=num;i++) // loop for start image to end image

{

CString ImageName=pDoc->m_ImageNames[i]; // Get Image name

m_SavePath=m_SavePath+"\\"+ImageName+"_DepthMap"+p Doc->GetImageExten(); // Get Image Path


GoldImage Test;

Test.SetImage(m_DMapImage);

Test.SaveImage(m_DMapSavePath); // Save Image

m_SavePath=temp;


[B][COLOR="Red"]CurrentFile = ImageName;

CurrentProgressCount = (i * 100) / num; //this would tell a percentage of files that has been processed

Sleep(700); //put this sleep to make sure that current file name appears on progress dlg for once

[/COLOR][/B]}



Your thread routine function should look like this.



UINT ProgressDlgThread(void *arg)

{

[B][COLOR="Red"]  //NOTE: I'm assuming that the name of your progressBar Dialog is CProgressBarDlg

[/COLOR][/B]


 CProgressBarDlg *m_progressDlg = new CProgressBarDlg((CWnd *)arg);

 m_progressDlg->DoModal(); //this would bring the dialogbox in front of you main application window

 delete m_progressDlg;

 return 0;

}


In your ProgressBar Dialog Class, you need to create OnTimer function. I hope you know about how to overload an OnTimer Function in Dlg Class
Otherwise you can have a look on MFC Controls - Timers

In OnInitDialog function you have to set a timer



SetTimer(100, 500); //this would cause your OnTimer Function to be called after every 500 milli seconds



Your OnTimer Function should be like



KillTimer(100);

SetStepCount(CurrentProgressCount);

SetWindowText (CurrentFile + " Saving.....");

SetTimer(100, 500);


I'm hopeful that it should work. Let me know how does it goes.

Munir

#7
wizard

wizard

    Newbie

  • Members
  • PipPip
  • 12 posts
Hi mnirahd,

Thank you so much for your reply, i am trying to implement this i have a problem. I set timer in OnInitDialog function, it calls onTimer() in that method CurrentProgressCount and CurrentFile values are not updating.

My Implementation:

Declared variables globally in stdAfx.h

extern int CurrentProgressCount;

extern CString CurrentFile;

And declared in view class and progress dialog class also.

int CurrentProgressCount;

CString CurrentFile;

Progress dialog class:

BOOL ProgDlg::OnInitDialog() 

{

	CDialog::OnInitDialog();

	

	SetTimer(100, 500);

	  

	return TRUE;          

}

void ProgDlg :: OnTimer(UINT nTimerID)

{

	

		KillTimer(100);

		SetStepCount(CurrentProgressCount);

		SetWindowText (CurrentFile + " Saving.....");

		SetTimer(100, 500);

	

}

In OnTimer() CurrentProgressCount contains some garbage value and CurrentFile contains null string.

View class:

I wrote the following function globally in view class.

unsigned int __stdcall ProgressDlgThread(void *arg)

{

  //NOTE: I'm assuming that the name of your progressBar Dialog is CProgressBarDlg


 ProgDlg *m_progressDlg = new ProgDlg((CWnd *)arg);

 m_progressDlg->DoModal(); //this would bring the dialogbox in front of you main application window

 delete m_progressDlg;

 return 0;


}

It opens progress bar but not updating any progress.

I added the following code in saveAll().

CString temp=m_DMapSavePath;

int num=pDoc->GetNoOfImages();


//Create a Thread routine.

(HANDLE)_beginthreadex( NULL, 0, &ProgressDlgThread, (void *) this, 0, 0);


for(int i=0;i<=num;i++)

{	


         CString ImageName=pDoc->m_ImageNames[i];

			                                                                                                                                                             m_DMapSavePath=m_DMapSavePath+"\\"+ImageName+"_DepthMap"+pDoc->GetImageExten();

          

          GoldImage Test;

	  Test.SetImage(m_DMapImage);

	  Test.SaveImage(m_DMapSavePath);

	  

	   CurrentFile = ImageName;

	   CurrentProgressCount = i++; 

	   Sleep(700); 

}

Please help me to solve this problem.

Edited by wizard, 09 November 2010 - 03:38 AM.


#8
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

In order to get onTimer function called make sure you're doing following,

In your header file for progressDialog, make sure you add following declaration


afx_msg void OnTimer(   UINT_PTR nIDEvent );  


In CPP file of your progressDialog class, make sure you add WM_TIMER in BEGIN_MESSAGE_MAP
For example


BEGIN_MESSAGE_MAP(CID24ForwardDlg, CDialog)

	ON_WM_PAINT()

	[COLOR="red"]ON_WM_TIMER()[/COLOR]

	ON_WM_CLOSE()

	ON_WM_QUERYDRAGICON()

	ON_WM_SYSCOMMAND()

	//}}AFX_MSG_MAP

END_MESSAGE_MAP()




Apply the above suggestion, and let me know how does it goes.

Munir

#9
wizard

wizard

    Newbie

  • Members
  • PipPip
  • 12 posts
Hi

I implemented same as you sent. onTimer function is called, But progress bar is not updating Because of CurrentProgressCount and CurrentFile variables doesn't contain any value in progress dialog class. And progress bar shows after execute for loop. I want to open and update progress bar while executing the loop.

Application shows only empty progress bar.How can i update progress bar please help me...

Edited by wizard, 11 November 2010 - 04:11 AM.


#10
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

The progress bar should be showing the bar on the values of CurrentProgressCount. You must change this value within the loop. Also can you post your code so that I could see what's the problem.

Munir




2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users