動易這套系統的資料擷取模組,是基於.Net2.0開發的,裡用到了Asp.net Ajax,WebService,線程式等技術.
具體實現思路是:
資料擷取類PowerEasy.Collection.CollectionProgress 的StartCreate()中啟動資料擷取線程
代碼如下:
private void StartCreate()
{
this.m_CreateThread = new Thread(new ThreadStart(this.Work));
this.m_CreateThread.Start();
}
接著將自己儲存到Appliction中
public void CreateCollectionProc()
{
this.m_CreateId = Guid.NewGuid().ToString();
if (HttpContext.Current != null)
{
this.m_PhysicalApplicationPath = this.PhysicalApplicationPath;
}
XmlManage manage = XmlManage.Instance("Config/CreateCollectionWork.config", XmlType.File);
manage.SetNodeValue("CollectionWork/WorkId", this.m_CreateId);
try
{
manage.Save("Config/CreateCollectionWork.config");
}
catch (FileNotFoundException)
{
throw new CustomException("CreateCollectionWork.config檔案未找到。");
}
catch
{
throw new CustomException("檢查您的伺服器是否給設定檔CreateCollectionWork.config或檔案夾寫入許可權。");
}
this.StartCreate();
if (HttpContext.Current != null)
{
HttpContext.Current.Application[this.m_CreateId] = this;
}
}
Application 的鍵,是一個產生的Guid 此Guid會儲存到 /Config/CreateCollectionWork.config 檔案中
進度顯示頁面\Admin\Collection\CollectionProc.aspx 採用Ajax技術調用Web服務GetCreateProgressService.asmx中的public ProgressInfo AcquireProgress()方法擷取CollectionProgress 執行個體的運行資訊並顯示給使用者,
具體的頁面資料截取類為System.Net下的 WebRequest ,WebClient ,WebResponse等,採用WebClient類來下載檔案,圖片等.
下面是Work方法的代碼架構:
try
{
try
{
//具體操作
}
catch (Exception exception)
{
this.m_ErrorInfo = exception.Message;
}
}
finally
{
if (this.CreateThread != null)
{
this.CreateThread.Abort();
}
}
通過new Thread(new ThreadStart(this.Work)); 啟動的線程是非池線程,即不是Asp.net服務進程線程池的線程