我正在尝试在 Windows 服务中使用 Rx 扩展,但我被卡住了。我找到的样本不起作用。我将用一些代码解释我想要实现的目标。我正在创建的第一个类实际上是完成所有工作的类:
class Worker : IDisposable {
public Worker() {
}
private void Run() {
}
public void Dispose() {
}
}
我想在 OnStart 中创建一个实例并在 OnStop 中杀死它:
public partial class MyService : ServiceBase {
private Worker _Worker;
public MyService () {
InitializeComponent ();
}
protected override void OnStart ( string[] args ) {
_Worker = new Worker();
}
protected override void OnStop () {
_Worker.Dispose();
_Worker = null;
}
}
所以我在Worker的构造函数中这样做了:
/* _TimesEvents and _Events are class level variables */
_TimedEvents = Observable.Timer ( TimeSpan.FromSeconds ( 1 ), new EventLoopScheduler() ).Timestamp ();
_Events = ( from events in _TimedEvents select events ).Subscribe ( e => Run() );
在 Dispose 中,我只是在 Dispose _Events。运行被调用一次。我怀疑它每秒被调用一次或根本不调用。
我错过了什么?
编辑:
Main()
像这样测试我的外观:
static void Main ( ) {
var args = Environment.GetCommandLineArgs ();
if ( null != args && args.Length > 0 ) {
using ( BRWorker worker = new BRWorker () ) {
System.Windows.Forms.MessageBox.Show ( "Press ok to quit", "Test", System.Windows.Forms.MessageBoxButtons.OK );
}
} else {
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new BRService()
};
ServiceBase.Run ( ServicesToRun );
}
}