Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Background worker and reporting progress 1

Status
Not open for further replies.

dseaver

IS-IT--Management
Jul 13, 2006
467
I have a dll which I wrote that contains a function that could take a minute or so (reading a lot of files and doing various other things). In that function I use a foreach loop. I call this function from my Gui exe in a background worker. How do I raise an event in my function that will be seen as a progress changed event to the background worker?

Code:
//doesSomething.dll

public void reallyLongFunction()
{
     foreach(item thing in listOfItems)
     {
          //Want to report the start of this iteration
          //does a whole set of functions for each item
          :
          :
     }
}

Code:
//Gui.exe

      void CallBgFunction()
      {
           BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerAsync();
            worker.WorkerReportsProgress = true;
       }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            doesSomething.Class item = new doesSomething.Class();
            item.reallyLongFunction();
         }
 
If the dll throws an event back to you then you can show it in the GUI using an Invoke.

public class MyDLL
{
public event IntEventHandler ReportProgress;

public MyDLL()
{
this.ReportProgress += new IntEventHandler(donothing);
}

private void donothing(int e) {}

public void reallyLongFunction()
{
//Do some work
this.ReportProgress(5);
//Do more work
this.ReportProgress(10);
}
}

public delegate void IntEventHandler(int e); //put this just outside your class


public class MyGUIClass //This is your form
{
void CallBgFunction()
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerAsync();
worker.WorkerReportsProgress = true;
}

void worker_DoWork(object sender, DoWorkEventArgs e)
{
doesSomething.Class item = new doesSomething.Class();

item.ReportProgress += new IntEventHandler(doesSomething_ReportProgress); //Attach the progress report event

item.reallyLongFunction();
}

private void doesSomething_ReportProgress(int e)
{
if (this.InvokeRequired)
{
this.Invoke(new IntEventHandler(), new object[] {e});
}
else
{
this.txtProgress.Text = e.ToString() + "%";
}
}
}


Please note that this won't compile as I just wrote it in this window. If it is throwing lots of errors let me know and I'll do it again for you in an IDE


 
I'll try to implement this, I also found a bit of a hacky way of doing it. If I pass a ref of the worker all the way down to the DLL, I can make it work, but I know that is not the correct way.
 
There are really 2 good ways to get data out of a component - either via events or via an interface. An interface gives enough information to make a reference to an object do something without being concerned with the actual type of object being passed in.

The advantage of events is that you don't care if anyone is listening or not and it's easier to implement.

Again, if you have trouble let me know and I'll do up a proper version for you.

 
I am getting error on this line " this.Invoke(new IntEventHandler(), new object[] {e});" I created a custom delegate, and it says delegate does not take 0 arguments. WHat would be the function here?
 
when i put the doesSomething_ReportProgress in the invoke, it works, is that the correct way of doing this?
 
Yes that is correct. I've been working with a lot of reflection recently where all I look at is types - that's why I threw in the Handler type rather than the method name.

:)

 
I always tried to figure out how to get a good status bar in my program, and with your help, I was able to implement it quickly. I found it a little bit hard to follow some of the web resources regarding progress bar and didn't take the time, but it was easy with this example. I will definitely use this in the future, I may even write a short FAQ for implementing a progress bar. Thanks, star for you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top