爆文采集系統後臺數據支撐:百度百家以及每天快報等等。。異步
簡單採集過程,再也不復述。ui
重點放在新學的線程以及桌面應用程序:this
一、線程部分, 開始、暫停、繼續、結束spa
1 public enum opState 2 { 3 none, //無操做 4 pause, //暫停 5 go_on, //繼續 6 stop //中止 7 } 8 private Thread myThread = null; 9 private bool myThreadRun = false; 10 private opState state = opState.none; 11 private delegate void ShowMsgHandler(string msg); 12 public Form1() 13 { 14 InitializeComponent(); 15 } 16 17 /// <summary> 18 /// 開始 19 /// </summary> 20 private void button1_Click(object sender, EventArgs e) 21 { 22 if (myThread == null || myThread.ThreadState == ThreadState.Stopped) 23 { 24 myThreadRun = true; 25 myThread = new Thread(new ThreadStart(runPro)); 26 myThread.IsBackground = true; 27 myThread.Start(); 28 } 29 else 30 { 31 myThreadRun = false; 32 } 33 } 34 35 public void runPro() 36 { 37 for (int i = 0; i < 10000; i++) 38 { 39 //狀態開關控制 40 switch (state) 41 { 42 case opState.pause: 43 i--; 44 continue; 45 case opState.go_on: 46 break; 47 case opState.none: 48 break; 49 case opState.stop: 50 return; 51 } 52 if (this.InvokeRequired) 53 { 54 this.Invoke(new ShowMsgHandler(ShowMsg), i.ToString()); 55 } 56 else 57 { 58 ShowMsg(i.ToString()); 59 } 60 //Thread.Sleep(100); 61 } 62 } 63 private void ShowMsg(string msg) 64 { 65 this.textBox1.AppendText(msg + "\r\n"); 66 } 67 68 /// <summary> 69 /// 暫停 70 /// </summary> 71 private void button2_Click(object sender, EventArgs e) 72 { 73 state = opState.pause; 74 } 75 76 /// <summary> 77 /// 繼續 78 /// </summary> 79 private void button3_Click(object sender, EventArgs e) 80 { 81 state = opState.go_on; 82 } 83 84 /// <summary> 85 /// 中止 86 /// </summary> 87 private void button4_Click(object sender, EventArgs e) 88 { 89 state = opState.stop; 90 myThreadRun = false; 91 }
二、窗體應用程序插件
2.一、異步加載輸出線程
1 this.Invoke(new Action(() => 2 { 3 label2.Text = subject_name;
textBox.AppendText("【" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "】 ----添加新做者:" + writer_name + "\r\n" + "\r\n"); 5 textBox.ScrollToCaret();//下拉框 6 }));
2.二、時間插件code
ps:記得修改插件屬性Interval的值爲1000orm
1 private void timer1_Tick(object sender, EventArgs e) 2 { 3 Second_++; 4 if (Second_ == 60) 5 { 6 Second_ = 0; 7 Minute_++; 8 } 9 if (Minute_ == 60) 10 { 11 Minute_ = 0; 12 Hour_++; 13 } 14 label7.Text = Hour_ + ":" + Minute_ + ":" + Second_; 15 }
-----------kylinblog