Monitoring progress of a subroutine that is using a file stream as an input
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Monitoring progress of a subroutine that is using a file stream as an input

Hybrid View

  1. #1
    Join Date
    May 2013
    Posts
    10

    Monitoring progress of a subroutine that is using a file stream as an input

    What is the traditional way to monitor a blocking subroutine that is using a file stream as its input? That is, what is the traditional way to make a progress meter in the console?

    Say I have a function that takes in a filestream and works with it. Suppose this file stream has a position and length property like in C# FileStream:

    Code:
    void sub (filestream file)
    {
       //read a bit of the stream, which advances stream cursor position
       //do something 
      //repeat until all of file stream has been used up
    
    }
    Presumably the function is chomping along the file stream as it is doing some calculations serially on the file stream.

    Because the function is blocking, there is no way to access the file streams position and length in this thread. So naturally it seems like the best thing to do to monitor the progress of that function is to make another thread and pass it the file stream object as a parameter, and in this separate thread, monitor the distance between filestream's position and length to determine how much of the file stream has been used up, and every second or so output the amount of file stream used onto the console.

    Is this how things are traditionally done?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,424

    Re: Monitoring progress of a subroutine that is using a file stream as an input

    Quote Originally Posted by bigc++ View Post
    separate thread, monitor the distance between filestream's position and length to determine how much of the file stream has been used up,
    Unless you resort to OS specific functions to read files, how are you going to know where you are in the file if you are blocked from doing so? In other words, if the file read is synchronous (no return until finished or error), you are out of luck.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Oct 2008
    Posts
    1,102

    Re: Monitoring progress of a subroutine that is using a file stream as an input

    if you are allowed modifying the "sub()" function, then I think the best solution is to allow users of the function to pass a callback that would be invoked at "progress" points accordingly. Then, the user would choose to employ a simple synchronous callback or an asynchronous notification to a monitor thread, or something in between.

    if you can't modify the function, then the multiple thread solution you suggest won't work as it is, because access to the stream position must be synchronized somehow.

    IMO, a better solution would be to subclass the fstream, injecting the callback mechanism directly in its overridden methods.

  4. #4
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,012

    Re: Monitoring progress of a subroutine that is using a file stream as an input

    Quote Originally Posted by bigc++ View Post
    Because the function is blocking, there is no way to access the file streams position and length in this thread. So naturally it seems like the best thing to do to monitor the progress of that function is to make another thread and pass it the file stream object as a parameter, and in this separate thread, monitor the distance between filestream's position and length to determine how much of the file stream has been used up, and every second or so output the amount of file stream used onto the console.

    Is this how things are traditionally done?
    No, you don't pass a file stream object to something that is merely responsible for reporting progress. You don't need a file stream to report progress, all you need is a position and a range. What if later you need to get the information from a socket instead of a file? Are you going to rewrite all your progress reporting code?

    Your reading function should not be concerned with how progress is reported to the user. It should just have an interface that it can call to set the range and the current progress value. Possibly, you may want the add the option to cancel the operation.

    This way you can implement different progress reporting strategies without affecting the file reading code. E.g. you can serially report progress for a command line application. Or you can handle inter-thread communication for a GUI application.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center