Solved

WorkerThread with progressbar and dll function

Posted on 2004-09-14
10
452 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
  • 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
 

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

747 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

12 Experts available now in Live!

Get 1:1 Help Now