?
Solved

WorkerThread with progressbar and dll function

Posted on 2004-09-14
10
Medium Priority
?
467 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 

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 1500 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

762 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