[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 469
  • Last Modified:

WorkerThread with progressbar and dll function

Hello,
I have a dll and a client, in the dll i have this function:
DLL1_API void ComputeSomeAlgorithm(CString filename1,CString filename2)
{
  FILE* FP;
   FP= fopen(filename1,"r");
  ...
   while (fscanf(fp,"%lf,&x) == 1)
   {       
     ....      
   }
   qsort(...);
   ComputeAnotherAlgorithm(int x,int y);
}

In my client I have a dialog which i choose  filename1 & filename2 and save them
in my dialog class members.
In the dialog i have a progress bar , and after i choose the two files from the dialog
I'm  creating a thread with WorkerThread function like this:

void CClientDlg::OnOK()
{
    AfxBeginThread (WorkerThread,this);
}


UINT WorkerThread(LPVOID lParam)
{
   CClientDlg* pObject = (CClientDlg*)lParam;
   ComputeSomeAlgorithm(pObject->m_FileName1,pObject->m_FileName2);// dll function
   for (int i=0;i<100;i++)
   {
     AfxGetMainWnd()->PostMessage(WM_USER_THREAD_UPDATE_PROGRESS,i,100);
     Sleep(100);
   }
   AfxGetMainWnd()->PostMessage(WM_USER_THREAD_FINISHED,0,0);
  return 0;
}

1) I can't get the two file names, when i'm insert to my dll function the parameters are wrong.
2) I want to compute the time of my dll function and show it on the progress bar instead of the
 the "for loop", how can i calculate the time of the dll function ?

I'm working with winXP Visual C++ 6.0 with MFC.

Thanks.

 
0
dani333
Asked:
dani333
  • 5
  • 4
1 Solution
 
dani333Author Commented:
Hi,
I found the solution of the first problem. :-)
I need just the second problem now, please.
0
 
mrwad99Commented:
I dont see how what you have at the minute is working anyway....

ComputeSomeAlgorithm(pObject->m_FileName1,pObject->m_FileName2);// dll function
   for (int i=0;i<100;i++)
   {

What you are doing there is essentially not entering the for loop until the dll function has finished.  So the progress bar is not showing anything relevant to the actual progress of the function processing since the function has already finished by the time the bar starts to get updated.  Surely you need to update the progress bar in the function that starts the thread...?

Regarding getting the time you could just use CTime::GetCurrentTime() before starting the thread, and then again when the processing is over and compare the two values.

HTH
0
 
dani333Author Commented:
hi,
Please ignore the  for (int i=0;i<100;i++)
the code need to be as follwing:

UINT WorkerThread(LPVOID lParam)
{
   CClientDlg* pObject = (CClientDlg*)lParam;
 // here i need to calculate some how the function time and send message to the progress bar
//   as long as it goes on
 
// should be replace some how
///////////////////////////////////////////
 AfxGetMainWnd()->PostMessage(WM_USER_THREAD_UPDATE_PROGRESS,i,100);
   ComputeSomeAlgorithm(pObject->m_FileName1,pObject->m_FileName2);// dll function
//////////////////////////
 //  then finished
    AfxGetMainWnd()->PostMessage(WM_USER_THREAD_FINISHED,0,0);
  return 0;
}

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
dani333Author Commented:
Let's say I know that the complexity of the function is O(n^2)
and i know the n number as i'm going into the function.
0
 
AndyAinscowCommented:
What is the problem?  From your question mrwad99 has given you a solution - GetCurrentTime.  

Do you mean you want to calculate beforehand that it will take 100 seconds?
0
 
mrwad99Commented:
If you want to update the time repeatedly, i.e. have a running counter displayed in the task bar, then you could just run a loop in the thread that posts a message to the main window (whose handler updates the statusbar) every one second or similar...
0
 
dani333Author Commented:
hi,
sorry I don't understand how to use it.
How can i know the time of the function?
shouldn't  I send message from the dll function to update the progress bar in the client?
and if so how can i talk with the Progress bar from the dll function,i don't know how
to use this line:  
AfxGetMainWnd()->PostMessage(WM_USER_THREAD_UPDATE_PROGRESS,i,100);
in my dll function?
0
 
mrwad99Commented:
Right.  You cannot (and it would not be wise) to (easily) alter the code in the DLL function to explicitly pass back progress updates.  Do this

UINT WorkerThread(LPVOID lParam)
{
    CClientDlg* pObject = (CClientDlg*)lParam;

    CTime tBefore = CTime::GetCurrentTime();  // Get current time

   AfxGetMainWnd()->PostMessage(WM_USER_THREAD_UPDATE_PROGRESS,i,100);

   ComputeSomeAlgorithm(pObject->m_FileName1,pObject->m_FileName2);// dll function

   CTime tAfter = CTime::GetCurrentTime(); // Get current time (again)


// At this point, tAfter - tBefore will give the time that you function has executed for.

CTimespan tLength = tAfter - tBefore;  // Overall difference in time

int nTime = tLength.GetTotalSeconds(); // Total seconds in the time period

// Now nTime represents (in seconds) the total time your dll function has executed for.  You can pass this int back by calling a (static) function in your main window, or by posting a message to your main window.  Then you have it to play with (to stick in the status bar or whatever).

>> and if so how can i talk with the Progress bar from the dll function,i don't know how
to use this line:  
AfxGetMainWnd()->PostMessage(WM_USER_THREAD_UPDATE_PROGRESS,i,100);

You need a handler for WM_USER_THREAD_UPDATE_PROGRESS in the main window that responds to this call. I would change it to pass the time in seconds calculated above back.  It looks like you know how to do this since you created the message yourself so you should be ok passing the integer as LPARAM or similar.

HTH
0
 
dani333Author Commented:
Hi  mrwad99,

I used another way , but i will give you your points for tring to help me.

Thanks a lot.

Dani.
0
 
mrwad99Commented:
Glad to help.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now