private void KeepAliveSink(LifeTimeMonitor sender, object obj) { if (IdleTimeout == false) { return; } ArrayList RemoveList = new ArrayList(); lock (SessionTable) { IDictionaryEnumerator en = SessionTable.GetEnumerator(); while (en.MoveNext()) { if (((HTTPSession)en.Value).Monitor.IsTimeout()) { RemoveList.Add(en.Value); } } } foreach (HTTPSession HS in RemoveList) { HS.Close(); } KeepAliveTimer.Add(false, 7); }
/// <summary> /// Gets the highscores, and resets them when erroneous or missing. /// </summary> /// <param name="difficulty">Easy, Medium, or Hard. (string)</param> /// <param name="difficultyHighScore">outputs High Score variable (string)</param> /// <param name="lblHighScoreDifficulty">Name of the label to set (Control)</param> public void GetHighScores(string difficulty, out string difficultyHighScore, Control lblHighScoreDifficulty) { double score; string scoreStr; // If no save file exists, create a new one with score = -1 if (!File.Exists(difficulty + "HighScore.txt")) { StreamWriter HS; HS = File.CreateText(difficulty + "HighScore.txt"); HS.WriteLine(-1); Thread.Sleep(50); HS.Close(); } // else if file is empty, add new score = -2 else if (new FileInfo(difficulty + "HighScore.txt").Length == 0) { StreamWriter HS; HS = File.AppendText(difficulty + "HighScore.txt"); HS.WriteLine(-1); Thread.Sleep(50); HS.Close(); } // if file contains non-numeric data, create new file with score = -3 StreamReader HSR; HSR = File.OpenText(difficulty + "HighScore.txt"); scoreStr = HSR.ReadLine(); Thread.Sleep(50); HSR.Close(); try { score = double.Parse(scoreStr); } catch { StreamWriter HS; HS = File.CreateText(difficulty + "HighScore.txt"); HS.WriteLine(-1); Thread.Sleep(50); HS.Close(); score = -3; } // if score <= 0, display "N/A" as the high score. if (score <= 0) { difficultyHighScore = "-1"; lblHighScoreDifficulty.Text = "N/A"; } // if score > 0, save as difficultyHighScore. else { difficultyHighScore = score.ToString(); lblHighScoreDifficulty.Text = score.ToString(); } }
/// <summary> /// Resets the highscore save files, variables, and labels. /// </summary> private void ResetHighScores() { StreamWriter HS; HS = File.CreateText("EasyHighScore.txt"); HS.WriteLine(0); Thread.Sleep(50); HS.Close(); StreamWriter HS2; HS2 = File.CreateText("MediumHighScore.txt"); HS2.WriteLine(0); Thread.Sleep(50); HS2.Close(); StreamWriter HS3; HS3 = File.CreateText("HardHighScore.txt"); HS3.WriteLine(0); Thread.Sleep(50); HS3.Close(); StreamWriter HS4; HS4 = File.CreateText("CustomHighScore.txt"); HS4.WriteLine(0); Thread.Sleep(50); HS4.Close(); LoadHighScores(); }
/// <summary> /// Saves the new highscore to the text file /// </summary> /// <param name="difficulty">"Easy" etc (string)</param> /// <param name="playTime">start - stop (TimeSpan)</param> public void UpdateHighScore(string difficulty, TimeSpan playTime) { // if new score is better (lower) than previous score: // overwrite save file StreamWriter HS; HS = File.CreateText(difficulty + "HighScore.txt"); HS.WriteLine(playTime.TotalSeconds.ToString("N0")); Thread.Sleep(100); HS.Close(); }