At times it may be necessary for you to control windows services in your visual basic code. For instance, one of the applications I had written used various software modules to write to and read from a MySQL database. I then used IIS to connect to the database and display reports to users on the network in a web browser.
The problem came in when MySQL version 4 must have had a little bug, and it did not work very well with IIS. When there was a relatively large amount of data in the recordset, MySQL would fall over with the annoying little message: “MySQL has gone away”. Later on, when version 5 became available, I installed that and it solved my problems, BUT in the interim I had a problem. I couldn’t tell my clients to wait around for newer versions of MySQL, and I also didn’t want to rewrite components to use another database.
So, the solution was to trap the errors that MySQL crashes generated and automatically restart MySQL as a windows service. This could be any service though, I just happened to use it for MySQL.
If you go to control panel –> administrative tools –> Services, you will see a list of services there with their service names. Use this name with the code in the download below to stop, start or pause your windows services. I am not going to give a detailed description of every line of code (because honestly I don’t really know most of it, its alot of API calls!). Instead, by supplying the source code, I hope that you will be able to modify this and use it in your own code.
| Restarting Windows Services with Visual Basic |
|

