Click here to Skip to main content
15,667,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I have an object that reads a large fixed field flat text file into an array of data fields. The issue is that this takes some time to do, and I want to update a progress bar each time a record is read. Anyone know Event design and Delegate design?

Thanks in advance.



1 solution

Declare an event on the class that has the method that does the reading of the text file. Subscribe to that event using a method on your progress update class. If the method signatures of the event you declare and the progress update class method don't match, you can create an intermediary to handle the conversion. In order to do that, you can attach an anonymous delegate to the text reading event... the anonymous delegate signature must match the event signature. Inside of that anonymous delegate, you can then call the progress update method with whatever parameters it takes.

Whenever you want to issue an update of the progress from the text file reading method, just call the event, passing whatever parameters are necessary (such as percent complete). Just FYI, there is one gotcha you have to be careful with for events... they can only be called from the class that it is declare in.

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900