Members
Technology Zones
IBM Learning Center
Articles
Hosted By
Info
|
[3441] Creating a Windows Service in VB.NET
Last post 05-23-2008 10:35 AM by mekaushik. 102 replies.
-
Advertisement
|
|
-
-
-
dragonsteve


- Joined on 09-16-2005

- Points 15
|
Hi,
in the article you say it is possible to create a service with a user interface.
I have tryed this but for some reason my service doesn't want to run.
First I added a windows form to my project.
In my service I have a global variable that is my form.
Code:Public oForm as FormUI
In the onStart I set the oForm as new instance.
Code:oForm = new FormUI
After building my service and installing it with success I try to start the service but I get the following message:
Code:...service on local computer started and then stopped...
I can't find any article that describes the implementation of a UI for a service.
Could somebody please help me out?
Kind regards,
Steve
|
|
-
-
-
TheToad


- Joined on 09-19-2005

- Points 10
|
When you build the service (create the exe) it is placed usually under a \bin sub-folder of the project, I have found that you must place every file in that folder in the exactly same directory and drive letter if installing it to another machine. eg c:\myservice\bin for this example.
remember that you can't be in the services view, if you want it to uninstall properly.
Below is a batch file that will install, and start the service you create via the "Creating a Windows Service in VB.NET" topic.
c:
cd \WINNT\Microsoft.NET\Framework\v1.1.4322\
net stop "MyService"
pause
installutil /u "C:\MyService\bin\MyService.exe"
pause
installutil "C:\MyService\bin\MyService.exe"
pause
net start "MyService"
pause
|
|
-
-
-
nagesh555


- Joined on 11-29-2005

- Points 5
|
Quote:[1]Posted by akansha_kesarwani on 10 Oct 2005 02:47 PM[/1]
hi all
There is a small problem, cud u all help me.
I created a windows service and when i tried to install it using InstallUtil from .net framework it gives me system.io.filenotfound exception, although the service has been created free of errors n warnings.
Regards
Akansha
Hi evrybody....
We r also getting the same error as above.....kindly help us.....
|
|
-
-
-
-
-
-
-
idawannanoe


- Joined on 01-27-2006

- Points 15
|
So far, not so good. I made my service with two forms that I added to the project once the service class was set up. My goal was the same as yours -- to have the forms pop up only when they're needed. One piece of the mystery was to go into Services from the Control Panel, double click my service, then under the second tab, tick the box for "Allow service to interact with the desktop".
After that, the service would start. When it found that it was running for the first time and wanted configuration, it dutifully attempted to show the configuration form. All I got was just the window frame, but none of the controls would display. The entire application stopped accepting any event except for the shutdown. At least that worked.
I read further on MS's site and they suggest that we create two pieces: the service part that runs in the background and the UI part that runs as a typical desktop app. They suggested that we use named pipes or some other form of IPC to communicate between the two pieces.
Decidedly, this is a weird and nastily inconvenient way to create a service with a UI. Me, I'm a *nix kinda guy; I'm used to a service being a program or a program being a service as the situation dictates. So far, VS2005 and VB.Net are well short of Eclipse, but it's what I've got to work with for a particular small set of customers.
If you guys find a workaround or decide to use the two-sided approach, would you be so kind as to repost here? Thanks!
Bill
PS: OH! By the way: if you use InstallUtil.exe to install the service, then wish to use InstallUtil.exe /U to uninstall the service, make sure that you have the Services window of the control panel closed. Otherwise, the service will be marked for deletion and will remain in the Services window until you reboot. Nasty, but that's the way it works.
|
|
-
-
grim234


- Joined on 02-02-2006

- Points 10
|
OK, I got it to work. Here's what I have so far: I created a thread to handle the UI, so basically it creates the form, and runs it
Code:
//coding in thin air, likely to have a number of bugs, but I'm sure you'll get the jist of things
private Form1 myForm = null;
private Thread myThread = null;
public override void OnStart()
{
myThread = new Thread(new ThreadStart(ThreadEntry)); //or something like that
myThread.Start();
}
public override void OnStop()
{
myThread.Abort();
}
private void ThreadEntry()
{
myForm = new Form1();
Application.Run(myForm);
}
Then, I did some crappy stuff just to see if this works, so I used an overridden OnContinue to show the form
Code:
public override void OnContinue()
{
myForm.Show();
base.OnContinue();
}
...and I had a button that would hide the entire form.
I think the reason your controls weren't showing up was due to a threading issue (i.e. the service is probably busy doing it's own thing, and doesn't get around to refreshing the controls). Errm... other than that, yeah you basically have to have the "Interact with Desktop" thing turned on. I hope this helps. Let me know if you have any questions!
Oh, and thanks for the great tip about removing the service :) Saved me tons of restarting I bet.
-Z
|
|
-
-
idawannanoe


- Joined on 01-27-2006

- Points 15
|
Well done. I had considered threads, but since I'm not familiar with Windows' notion of environment containment, I wasn't sure if that would work. Glad you got ahead of me there. I'm going to create another service project and move bits into it from the desktop project.
Glad the tip worked for ya. I discovered it quite by accident whilst installing and uninstalling the original service project. For what it saved me in hair-tearing (and I haven't much left!) I thought it might be worth a mention ;-)
BTW, I was pleasantly surprised with the new tickbox in class projects that will expose a Dot Net component to COM. In VS2003 it was a PITA to create a COM wrapper for classes you'd like to use with IIS. Well, boyhowdy, in VS2005, just tick the box and the next time the class compiles, you can take that resulting DLL, plonk it in your \inetpub\www folder and use it as if really had belonged there all along.
I tried a Hello World ASPx form using my database transport DLL that I created in VS2005 as described and miracle of miracles, it worked perfectly the first time.
After that, I think I'll quit for the week; not to press my good luck :-D
Best,
Bill
|
|
|
Search
Code Samples
New Members
|