/// <summary> /// ログファイルを開く /// </summary> /// <param name="file"></param> private void readLogFile(string file) { // コントロール初期化 this.initControl(); // ファイルチェック if (File.Exists(file)) { // カウント数 long size = new FileInfo(file).Length; if (size < 0) { MessageBox.Show(Properties.Resources.ErrorMessageFileReadError, Properties.Resources.ErrorMessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // ファイル履歴処理 if (this.setting.IsSaveHistory) { this.historyCntrol.IntoHistory(file); this.initHistoryMenu(); } // アラートメール有効の場合警告 if (this.setting.IsUseAlertMail) { if (MessageBox.Show(Properties.Resources.noticeAlertMailStart, Properties.Resources.QuestionMessageBoxTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } } // エンコーディング判定 this.setting.AutoCharacterEncoding = ReadTextBinary.GetEncoding(file); this.setEncodingStatus(); this.file = file; this.textBox1.Text = ""; // 読み込んだファイル名をタイトルに this.Text = Path.GetFileName(file) + " - LogLogViewer"; // 中断ボタンにし、有効にする this.isPause = false; this.changeReloadMenuIcon(false); this.reloadRToolStripMenuItem.Enabled = true; // 監視タスクの起動 this.threadTask(); } else { MessageBox.Show(Properties.Resources.ErrorMessageFileNotFound, Properties.Resources.ErrorMessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } }
/// <summary> /// 監視タスクの起動 /// </summary> private void threadTask() { this.isReading = true; TaskFactory taskFactory = new TaskFactory(); this.task = taskFactory.StartNew(() => { // タスクの開始 Debug.Print("* main task start."); // 初回フラグ bool firstRead = true; long beforeFileSize = 0; while (true) { try { if (isPause == false) { // ファイルサイズ取得 long nowFileSize = new FileInfo(this.file).Length; if (isReading && beforeFileSize < nowFileSize) { long readStartAdress = 0; bool isContinue = false; // 読み込める行が最大Byteを超えていないかチェック // もしくはアラートメール有効時は全部もれなく読む(初回は除く) if (nowFileSize - beforeFileSize < this.setting.MaxReadSize) { // 連続性を保証 isContinue = true; readStartAdress = beforeFileSize; } else { readStartAdress = nowFileSize - this.setting.MaxReadSize; } // TODO デバッグ Debug.Print("* readfile continue: {0}", isContinue.ToString()); // 所定のアドレスからファイルを読み込む(この瞬間もファイルサイズが変更している可能性もある) string readStr = ReadTextBinary.Read(this.file, readStartAdress, this.getPriorityEncoding(), out beforeFileSize); // ただし逆順で作られる List <TextBoxItems> itemlist = this.createTextBoxItems(readStr); // フィルタで空の場合 if (itemlist.Count == 0) { continue; } // 連続で読み込まない場合の注意 if (isContinue == false) { // 表示用アイテム TextBoxItems item = new TextBoxItems(); item.Text = string.Format("***** LogLogViewer read from {0:#,0} Bytes. *****" + System.Environment.NewLine, readStartAdress); itemlist.Add(item); } // 反転 itemlist.Reverse(); // テキストボックスの内容の全削除フラグ bool allDelFlag = false; // 表示がこえているか、それとも差分表示モードか if (itemlist.Count >= this.setting.MaxLineLength || this.setting.OnlyPostscriptMode) { allDelFlag = true; } // 文字を表示 this.textBox1.Invoke(new appendTextDelegate(appendText), new object[] { itemlist, allDelFlag }); // アラートメールが有効なら開始(初回だけ読込後 if (firstRead && this.setting.IsUseAlertMail) { this.StartAlertMailTask(beforeFileSize); } } else if (!isReading || this.IsDisposed) { break; } } else if (!isReading) { break; } // アラートメールの判定 有効ならスレッド開始、無効なら停止 if (firstRead == false && this.setting.IsUseAlertMail == true) { this.StartAlertMailTask(beforeFileSize); } else if (this.setting.IsUseAlertMail == false) { this.StopAlertMailTask(); } // 監視処理のメインループを待機 Thread.Sleep(100); // 初回読込フラグを下げる firstRead = false; } catch (Exception ex) { Debug.WriteLine("* ERROR:" + ex.ToString()); MessageBox.Show(ex.Message); isReading = false; break; } } // メール監視を停止 this.StopAlertMailTask(); // タスクの終了 Debug.Print("* main task end."); }); }