Bobris / Nowin

Owin Web Server in pure .Net
MIT License
536 stars 78 forks source link

Is it possible to provide a background worker sample? #72

Open robertkraig opened 7 years ago

robertkraig commented 7 years ago

I've written an application which is form-based, it does some interval updates, I wanted to know if it's possible to provide an example of what it would look like to configure this application to use a BackgroundWorker such that the webserver could run in background while in a WinForm application.

robertkraig commented 7 years ago

My objective is to host some simple binary image file for an IoT Node Application.

Bobris commented 7 years ago

Just look at NowinSample/Program.cs it cannot be much easier than that.

marius-klimantavicius commented 7 years ago

If I understood your question correctly - you don't even need to use BackgroundWorker. Once you've created and started a server (see NowinSample/Program.cs as @Bobris mentioned) it will run until either stopped or disposed (so keep a reference alive, stop+dispose when no longer needed). Nowin uses external threads (simplified a bit, don't recall if there is an option to flow execution context) to handle requests. This however might require additional care when dealing with forms and controls - namely something like this:

if (control.InvokeRequired) 
{
    control.Invoke(()=>...action...);
}
else 
{
    ...action...
}