using System;
using System.ComponentModel;
using System.Diagnostics;
namespace Funique
{
///
/// Use seperate process to access third party application
/// ------------------------------------------------
/// 使用額外的執行序還存取第三方執行檔
///
public abstract class ThirdPartyApplication : IDisposable
{
protected Process MainProcess;
protected BackgroundWorker BackgroundWorker;
///
/// Default progress react is not enable
/// But if enable this value will reflect current background worker's progress
/// ------------------------------------------------
/// 預設進度條: 關閉
/// 如果打開, 這個數字會顯示目前執行的進度背景作業的
///
protected int progress = -1;
///
/// Effect debug message tag
/// ------------------------------------------------
/// 影響 Debug 資訊輸出的標籤
///
protected virtual string WorkerName => "Unknown";
public ThirdPartyApplication()
{
FuniqueLogger.Log($"{WorkerName} initialization", WorkerName);
MainProcess = new Process();
BackgroundWorker = new BackgroundWorker();
BackgroundWorker.WorkerSupportsCancellation = true;
BackgroundWorker.DoWork += DoWork;
BackgroundWorker.RunWorkerCompleted += Completed;
BackgroundWorker.ProgressChanged += ProgressChanged;
}
///
/// Run background worker
/// ------------------------------------------------
/// 執行背景工作序
///
public void Run()
{
FuniqueLogger.Log($"{WorkerName} start running", WorkerName);
BackgroundWorker.RunWorkerAsync();
}
///
/// Background worker progress update event
/// ------------------------------------------------
/// 背景工作序進度更新事件
///
///
///
protected virtual void ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progress = e.ProgressPercentage;
}
///
/// Background do worker event
/// Default, this will activate main process
/// ------------------------------------------------
/// 背景工作序 執行事件
/// 預設, 這個會執行副執行序註冊的事件
///
///
///
protected virtual void DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bw = sender as BackgroundWorker;
Open(bw);
}
///
/// When background process finished
/// ------------------------------------------------
/// 當背景執行序結束執行
///
///
///
protected virtual void Completed(object sender, RunWorkerCompletedEventArgs e)
{
FuniqueLogger.Log($"{WorkerName} finished running", WorkerName);
}
///
/// Main process pre-activate event
/// ------------------------------------------------
/// 在開始執行副執行序註冊的事件前置序
///
///
protected virtual void Open(BackgroundWorker bw)
{
MainProcess.Start();
}
///
/// Cancel the process running but resource will keep it
/// It can running again
/// ------------------------------------------------
/// 取消執行序執行 但是會保留資源
/// 可以再度啟動
///
public virtual void Close()
{
MainProcess.Close();
BackgroundWorker.CancelAsync();
FuniqueLogger.Log("Process got cancel", WorkerName);
}
///
/// Kill the process and background process to release the memories
/// ------------------------------------------------
/// 殺死執行序與背景作業來釋放資源
///
public virtual void Dispose()
{
FuniqueLogger.Log($"{WorkerName} disposing", WorkerName);
Close();
BackgroundWorker.Dispose();
FuniqueLogger.Log($"{WorkerName} process killed", WorkerName);
MainProcess.Dispose();
}
}
}