Home > OS >  Why isn't all of the data being logged?
Why isn't all of the data being logged?

Time:04-06

I have a List<Passenger> that contains information about their trips with my bus.
Each time I perform a trip I generate a random number of passengers (anywhere from 0 to 10) and I record them when they exit the bus.

I run the program anywhere between 10 to 100.000 times and record the passengers to the list only when they exit the bus. On the final stop, everyone gets kicked out.

I then use this code to write every passenger's trip to a text file.

Code

This code get called directly inside static void Main of Program.cs right after the cycles have completed running.

private static async Task LogData(StatManager statManager)
{
    await File.WriteAllLinesAsync("test_output_1.txt", statManager.LogAllData());
}

Passenger.cs

public int ID {get; private set;}
public int stops {get; private set;} = 0;
public TripInfo trip {get; private set;} 
public Passenger(int passenger_id, int stop_entered)
{
    ID = passenger_id;
    trip = new TripInfo();
    trip.first_stop = stop_entered;
}

public void ExitBus(int exit_stop)
{
    //get called once the passenger exits the bus
    //at this point, the passenger is added to the main list
    trip.exit_stop = exit_stop;
}

public void ArrivedAtStop()
{
    stops  ; //stop counter
}

public int[] GetStopsVisited()
{
    int[] stops = new int[trip.stop_count];
    for(int i = 0; i < trip.stop_count; i  )
    {
        stops[i] = trip.first_stop   i;
    }
    return stops;
}

public class TripInfo
{
    public int first_stop;
    public int exit_stop;
    public int stop_count
    {
        get
        {
            if (exit_stop == null) return -1;
            return exit_stop - first_stop;
        }
    }
}

StatManager.cs

public List<Passenger> LifetimePassengers = new List<Passenger>();
public int LifetimePassengerCount {get; private set;} = 0; //gets incremented in every cycle
private float mean;
private int LifetimeStops
{
    get
    {
        return busManager.CurrentStop   1; //gets an int that was incremented in every cycle
    }
}
public String[] LogAllData()
{
    List<String> toLog = new List<string>();
    toLog.Add("LifetimeStops:" LifetimeStops.ToString());
    toLog.Add("LifetimePassengers:" LifetimePassengerCount.ToString());
    toLog.Add("IndividualPassengerData");
    foreach(Passenger p in LifetimePassengers)
    {
        toLog.Add("-");
        toLog.Add($"PassengerID:{p.ID}");
        toLog.Add($"NumberOfStops:{p.stops}");
        toLog.Add($"FirstStop:{p.trip.first_stop}");
        toLog.Add($"ExitStop:{p.trip.exit_stop}");
        string stops = String.Join(",", p.GetStopsVisited());
        toLog.Add($"StopsVisited:[{stops}]");
    }
    return toLog.ToArray();
}

Output is as expected for the first lines but then I get this:

...
PassengerID:677
NumberOfStops:2
FirstStop:198
ExitStop:200
StopsVisited:[198,199]
PassengerID:678
NumberOfStops:2
FirstStop:198
ExitStop:200
StopsVisited:[198,199]
PassengerID:679
NumberOfStops:1
Fi

All data follows the above pattern of an ID and some trip information. But the data is incomplete. Above, the last passenger recorded is number 679 but the program ran for 1.000 stops and had a minimum of 1.000 passengers. Also the data stops abruptly with this unexpected Fi and does not complete the record of passenger number 679.

Furthermore, when I tried re-running the program, it did not output anything. I tried running it for amount of cycles: 5, 10 and 100, there was no difference and there was no output.

What can I do to solve this problem?

CodePudding user response:

I think you need to follow this pattern calling async method from the main:

public static void Main()
{
    MainAsync().GetAwaiter().GetResult();
}

private static async Task MainAsync()
{
     // 
    var statManager = new StatManager();
    // you code here ...
    await LogData(statManager);
}
private static async Task LogData(StatManager statManager)
{
    await File.WriteAllLinesAsync("test_output_1.txt", 
    statManager.LogAllData());
}
  • Related