Comment detail

手作業Grep (Nested Flatten)
C#で。標準入力とUIを別スレッドにしているので、追加入力可能です。
コマンドラインとUIの組み合わせって意外と新鮮だと思いました。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using System.Windows.Forms;
using System.Threading;

namespace HandGrep {
    // 行選択ダイアログクラス
    class Dialog : Form {
        ListBox list;
        public ListBox List {
            get { return list; }
            private set { list = value; }
        }
        public Dialog() {
            this.Width = 300; this.Height = 200;
            this.Text = "選択して×ボタンを押してください。";
            this.List = new ListBox();
            this.List.Dock = DockStyle.Fill;
            this.List.SelectionMode = SelectionMode.MultiExtended;
            this.Controls.Add(this.List);
        }
        public void AddLine(string line) {
            this.List.Items.Add(line);
        }
    }
    // メインクラス
    class Program {
        static object objLock;                  // 排他オブジェクト
        static AutoResetEvent evDialogLoad;     // ダイアログ初期化イベント
        static AutoResetEvent evDialogClose;    // ダイアログ終了イベント
        static Dialog dlg;                      // ダイアログ
        delegate void MyDelegate();
 
        static void Main(string[] args) {
            objLock = new object();
            evDialogLoad = new AutoResetEvent(false);
            evDialogClose = new AutoResetEvent(false);

            // ダイアログ作成
            dlg = new Dialog();
            dlg.Load += new EventHandler(       // ダイアログ初期化イベントハンドラ登録
                delegate(object o, EventArgs e) {
                    evDialogLoad.Set();
                });
            // ダイアログを別スレッドで表示
            new MyDelegate(
                delegate() {
                    dlg.ShowDialog();
                }
            ).BeginInvoke(new AsyncCallback(OnDialogClose), null);
            evDialogLoad.WaitOne();             // ダイアログの初期化待ち
            // 標準入力ループ
            string line;
            while ((line = Console.ReadLine()) != null) {
                // 標準入力から1行読み込み、ダイアログに追加
                dlg.Invoke(new MyDelegate(
                    delegate() {
                        lock (objLock) {
                            if (dlg == null) return;
                            dlg.AddLine(line);
                        }
                    }
                ));
            }
            evDialogClose.WaitOne();            // ダイアログの終了待ち
        }
        // ダイアログクローズ後のコールバック
        static void OnDialogClose(IAsyncResult result) {
            lock (objLock) {
                // 選択行を標準出力に書き込む
                foreach (string line in dlg.List.SelectedItems) {
                    Console.WriteLine(line);
                }
                dlg = null;
            }
            evDialogClose.Set();
        }
    }
}

Index

Feed

Other

Link

Pathtraq

loading...