我有一個List<Passenger>包含他們乘坐我的公共汽車旅行的資訊。
每次我執行一次旅行時,我都會生成亂數量的乘客(從 0 到 10),并在他們下車時記錄下來。
我在任何地方運行該程式 10 到 100.000 次,并且僅在乘客下車時將乘客記錄到串列中。在最后一站,每個人都被踢了出去。
然后,我使用此代碼將每位乘客的行程寫入文本檔案。
代碼
此代碼在回圈完成運行后static void Main直接在內部呼叫。Program.cs
private static async Task LogData(StatManager statManager)
{
await File.WriteAllLinesAsync("test_output_1.txt", statManager.LogAllData());
}
乘客.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;
}
}
}
統計管理器.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();
}
第一行的輸出與預期的一樣,但后來我得到了這個:
...
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?
uj5u.com熱心網友回復:
我認為您需要遵循這種模式,從主要呼叫異步方法:
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());
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/457516.html
下一篇:嘗試將檔案名傳輸到其他函式
