Solved

WorkerThread with progressbar and dll function

Posted on 2004-09-14
10
466 Views
Last Modified: 2013-11-20
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
Comment
Question by:dani333
[X]
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
  • 5
  • 4
10 Comments
 

Author Comment

by:dani333
ID: 12058289
Hi,
I found the solution of the first problem. :-)
I need just the second problem now, please.
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 12058426
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
 

Author Comment

by:dani333
ID: 12058889
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dani333
ID: 12059546
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 12062294
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
 
LVL 19

Expert Comment

by:mrwad99
ID: 12063650
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
 

Author Comment

by:dani333
ID: 12063743
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
 
LVL 19

Accepted Solution

by:
mrwad99 earned 500 total points
ID: 12069261
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
 

Author Comment

by:dani333
ID: 12091244
Hi  mrwad99,

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

Thanks a lot.

Dani.
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 12091246
Glad to help.
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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 …
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.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

690 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