Gadgeteer.WebServer - Define what's "good usage" to you

I wrote hundred code rows for html frontend using the WebServer and WebEvent classes.
But each project I wrote uses the WebServer class in a different way from the others:

In the first project I wrote, the web server is started in the ProgramStarted() method (same thing for all the WebEvents)…

After that, I found a topic that explains why is better to start the web server in a timer tick event (avoiding to start the web server in the ProgramStarted() method)… So in the second project I wrote, I did this.

After, I found another topic in which some user said that’s a good practice start the web server in a separated thread… And this is what I did in a third project!
After after after … :stuck_out_tongue:

Now, I think that there’s always more to learn here with you and I want learn!

How do you think is the better way to start/use a WebServer and WebEvents?
Or just good practices :slight_smile:

@ BigFoot - This is an easy question… The best way to do it is the way I do it. ;D

I would not do anything in the ProgramStarted method which takes any significant time. Since the Web server requires the network to be ready before it is started, I would not wait in the ProgramStarted method for the network to be ready.

I would use a separate thread to wait for the network to be ready and then start the web server. I don’t like the use of a timer, since you it may expire before the network is ready, or if it expires after, then the start of the server would be delayed.


@ Mike - Great, you convinced me! :stuck_out_tongue:

So don’t you wait for NetworkAddressChanged/NetworkAvailabilityChanged events to start web server?

You start a separate thread and wait for a valid ip address in it, correct?

@ BigFoot - In some cases the address changed event occurs before the address is actually available. I just start a thread and wait for the IP address to be available. You can see if the IP address is actually available on the interface in the event handler if you are curious

You could wait for the Address change event to occur and then start a thread which checks for the IP address and then starts the server. I generally never wait for something to happen in an event handler.

1 Like

@ Mike - Perfect. Thank you very very much!
I’m going to manage web server as you told me in all my projects. 8)