我为Kofax创建了一个自定义模块。该模块是一个WinForms应用程序,具有运行时表单和安装表单(管理模块)。
对于运行时应用程序,我使用以下代码
Program.cs -启动运行时表单
internal static class Program
{
[STAThread]
private static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FrmMain());
}
}
FrmMain.cs -初始化UI并创建所需的实例
public partial class FrmMain : Form
{
private BatchProcessor batchProcessor;
private BatchManager batchManager;
private SessionManager sessionManager;
public FrmMain()
{
InitializeComponent();
}
private void FrmMain_Load(object sender, EventArgs e)
{
try
{
batchProcessor = new BatchProcessor();
sessionManager = new SessionManager();
batchManager = new BatchManager(batchProcessor, sessionManager);
// UpdateUI();
timerBatchPolling.Enabled = true;
}
catch (Exception exception)
{
throw exception;
}
}
private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
{
timerBatchPolling.Enabled = false;
try
{
sessionManager.Logout();
}
catch (Exception exception)
{
throw exception;
}
}
private void timerBatchPolling_Tick(object sender, EventArgs e)
{
timerBatchPolling.Enabled = false;
batchManager.BatchPolling();
// UpdateUI();
timerBatchPolling.Enabled = true;
}
}
BatchManager.cs -请求下一个要处理的批次
internal class BatchManager
{
private BatchProcessor batchProcessor;
private SessionManager sessionManager;
public IBatch CurrentActiveBatch { get; private set; }
public BatchManager(BatchProcessor batchProcessor, SessionManager sessionManager)
{
this.batchProcessor = batchProcessor;
this.sessionManager = sessionManager;
this.sessionManager.LoginToRuntimeSession();
}
public void BatchPolling()
{
CurrentActiveBatch = sessionManager.GetNextBatch();
if (CurrentActiveBatch != null)
{
batchProcessor.ProcessBatch(CurrentActiveBatch);
}
else
{
sessionManager.Logout();
}
}
}
BatchProcessor.cs -处理批处理
internal class BatchProcessor
{
public void ProcessBatch(IBatch batch)
{
// ...
}
}
我刚刚看到也可以将自定义模块作为服务运行。我不知道如何注册它们或如何设置代码,所以我想问是否有办法更改我的代码以将该模块转换为服务而不是表单应用程序。
感谢帮助!
答案 0 :(得分:1)
创建一个从ServiceBase派生的新类。两种相关的方法是OnStart
和OnStop
,请进一步了解它们here。这是一个示例,请注意CustomModule
是我的自定义类之一,负责登录KC,处理批处理等。
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
cm = new CustomModule();
cm.Login("", "");
if (CustomModule.BatchNotificationEnabled == true)
{
cm.ListenForNewBatches();
}
else
{
cm.PollForNewBatches();
}
}
要安装该服务,可以添加另一个从Installer派生的类,但这纯粹是可选的。但是,我建议您加倍努力,因为这将允许您以与所有Kofax模块相同的样式来安装和卸载CM:MyCustomModule.exe -install
。
最后,在您的Program.cs
中,检查用户是否以交互方式启动该模块:
if (Environment.UserInteractive)
{
// run as module
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new RuntimeForm(args));
}
else
{
// run as service
ServiceBase.Run(new CustomModuleService());
}