0

I have a method that runs for a very long time and writes to log files. It's set up like this

      public class PSOHelper
      {

        public PSOHelper(string[] params)
        {
            //set up params here
        }  
        public async void RunApp(string runType)
        {
               //long running application that writes to a text file
        }
      }

Then in the main program I call this method like so:

 public async Task<bool> PreparePSOAndRunPSO()
    {
        string[] params;
        //code to fetch the parameters
        PSOHelper psoH = new PSOHelper (params)
        try
        {
            await Task.Run(() =>
            {
                psoH.RunApp(RunConfig.RunMode);
            });
            return true;
        }
        catch( Exception ex)
        {
            Helper.log.Error("exception starting PSO", ex);
            return false;

        }
    }

Now, in my Main method I want to call PreparePSOAndRunPSO and then, in a while loop read from the log that is being written to in RunApp until PreparePSOAndRunPSO finishes. What is the right way for me to do this?

svick
  • 236,525
  • 50
  • 385
  • 514
Matt Kagan
  • 611
  • 1
  • 12
  • 24
  • What is the reason you are reading from the file as it is being written to? – Jacob Roberts Aug 13 '15 at 17:46
  • Yah, you can't read and write to the same file simultaneously ... – Daniel I Aug 13 '15 at 17:48
  • @JacobRoberts I am launching a third party application in RunApp and the only way for me to monitor its progress is to read its debug log. – Matt Kagan Aug 13 '15 at 17:49
  • I'm not sure if Daniel is correct as I've never read/write to the same file at the same time. This is something that you will have to test and make sure when you open your stream, you are doing it as 'read-only' so it doesn't lock the file where the 3rd party app can't write to it. I'll write an answer with the assumption that you can successfully read/write without locking the file. – Jacob Roberts Aug 13 '15 at 17:53
  • Thanks! I have been able to read from the file if the third party application is already running. So all I need to do now is be able to launch it in the background and read from the file until it exits. – Matt Kagan Aug 13 '15 at 17:57

1 Answers1

1

One thing is to change your async void RunApp(string runType) method to async Task RunApp(string runType).

Now something like this should work.

public async Task<bool> PreparePSOAndRunPSO()
{
    string[] params;
    //code to fetch the parameters
    PSOHelper psoH = new PSOHelper (params)
    try
    {
        var task = psoH.RunApp(RunConfig.RunMode); //no need to use Task.Run considering the method returns a task.

        while (!task.IsCompleted)
        {
            /* open stream as readonly, read the log, close the stream */
            /* if the log isn't to big, you can read to end so you close the stream faster and then parse the entire log on each iteration.  If the log is big, you'd need to read it line by line and parse each line */                 
            /* maybe do a await Task.Delay(100); if you have any race conditions */

        }

        return true;
    }
    catch( Exception ex)
    {
        Helper.log.Error("exception starting PSO", ex);
        return false;

    }
}
Jacob Roberts
  • 1,725
  • 3
  • 16
  • 24