網(wǎng)頁抓取 wordpress西安自動seo
????????上位機(jī)開發(fā)中一定會用到的技術(shù)就是 設(shè)備的線程開始運(yùn)行執(zhí)行生產(chǎn)流程,在生產(chǎn)過程中會有要打開安全門或暫停設(shè)備動作,人為去排除設(shè)備小問題的時就要用到暫停功能,問題排除后設(shè)備繼續(xù)運(yùn)行,生產(chǎn)完成后設(shè)備停止。 這些操作是上位機(jī)開發(fā)中必須要實(shí)現(xiàn)的功能。下面是一個簡單的示例。
?1.界面
2.代碼
2.1主要用到的對象
//線程源
private CancellationTokenSource cts = new CancellationTokenSource();
//手動停止事件對象
private ManualResetEvent resetEvent = new ManualResetEvent(true);
//線程
private Task task;
2.2 開始
/// <summary>
/// 開 始
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStart_Click(object sender, EventArgs e)
{if (cts.IsCancellationRequested){cts = new CancellationTokenSource();}resetEvent.Set();task = Task.Factory.StartNew(() =>{int count = 1;Color clrInfo = Color.Blue;while (!cts.IsCancellationRequested){resetEvent.WaitOne();//阻止當(dāng)前線程var strInfo = "運(yùn)行日志[" + count + "]";AddListViewThread(null, strInfo, clrInfo);count++;Thread.Sleep(1000);}});
}
2.3 暫 停
/// <summary>
/// 暫 停
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPause_Click(object sender, EventArgs e)
{resetEvent.Reset();
}
2.4繼 續(xù)
/// <summary>
/// 繼 續(xù)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnContinue_Click(object sender, EventArgs e)
{resetEvent.Set();
}
2.5停 止
/// <summary>
/// 停 止
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStop_Click(object sender, EventArgs e)
{cts.Cancel();
}
全部代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;namespace TaskWindowsFormsApp
{public partial class Form1 : Form{public Form1(){InitializeComponent();}delegate void AddListViewCallback(string strTime, string strContent, Color textColor);//線程源private CancellationTokenSource cts = new CancellationTokenSource();//手動停止事件對象private ManualResetEvent resetEvent = new ManualResetEvent(true);//線程private Task task;/// <summary>/// 開 始/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnStart_Click(object sender, EventArgs e){if (cts.IsCancellationRequested){cts = new CancellationTokenSource();}resetEvent.Set();task = Task.Factory.StartNew(() =>{int count = 1;Color clrInfo = Color.Blue;while (!cts.IsCancellationRequested){resetEvent.WaitOne();//阻止當(dāng)前線程var strInfo = "運(yùn)行日志[" + count + "]";AddListViewThread(null, strInfo, clrInfo);count++;Thread.Sleep(1000);}});}/// <summary>/// 暫 停/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnPause_Click(object sender, EventArgs e){resetEvent.Reset();}/// <summary>/// 繼 續(xù)/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnContinue_Click(object sender, EventArgs e){resetEvent.Set();}/// <summary>/// 顯示(添加)日志/// </summary>/// <param name="strTime">時間</param>/// <param name="strContent">內(nèi)褲</param>/// <param name="textColor">顏色</param>public void AddListViewThread(string strTime, string strContent, Color textColor){if (this.IsDisposed){return;}if (this.listViewWorkLogs.InvokeRequired){//獲取一個值,該值指示調(diào)用方在對控件進(jìn)行方法調(diào)用時是否必須調(diào)用 Invoke 方法,因?yàn)檎{(diào)用方位于創(chuàng)建控件所在的線程以外的線程中AddListViewCallback d = new AddListViewCallback(AddListViewThread);this.Invoke(d, new object[] { strTime, strContent, textColor });}else{AddContent2ListView(strTime, strContent, textColor);}}/// <summary>/// 顯示(添加)日志/// </summary>/// <param name="strTime">時間</param>/// <param name="strContent">內(nèi)褲</param>/// <param name="textColor">顏色</param>public void AddContent2ListView(string strTime, string strContent, Color textColor){try{if (strTime == null){strTime = string.Format("{0}:{1}:{2}", DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);}int nCount = listViewWorkLogs.Items.Count;if (nCount > 600){for (int i = nCount - 1; i > 100; i--){listViewWorkLogs.Items.RemoveAt(i);}}}catch{int nCount = listViewWorkLogs.Items.Count;if (nCount > 600){listViewWorkLogs.Clear();}}ListViewItem lvItem = new ListViewItem();lvItem.ForeColor = textColor;lvItem.Text = strTime;lvItem.StateImageIndex = 0;lvItem.SubItems.Add(strContent);this.listViewWorkLogs.Items.Insert(0, lvItem);}/// <summary>/// 停 止/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnStop_Click(object sender, EventArgs e){cts.Cancel();}}
}