I’m trying to use Rx Extensions in a Windows service and I’m stuck. The samples I’ve found don’t work. I’m going to explain with some code what I want to achieve. The very first class I’m creating is actually the class doing all the work:
class Worker : IDisposable {
public Worker() {
}
private void Run() {
}
public void Dispose() {
}
}
I want to create an Instance in OnStart and kill it 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;
}
}
So I went to doing this in the constructor of 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() );
In Dispose, I’m just Disposing _Events. Run is called once. I would suspect it being called every second or not at all.
What am I missing?
Edit:
To test my Main() looks like this:
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 );
}
}
You may want to use Observable.Interval (http://rxwiki.wikidot.com/101samples#toc14)