473,395 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Windows Service and StreamWriter

I am trying to create a windows service. The part I am having trouble with
is writing text to a log file. I am using a very basic StreamWriter function
to try to test this.

I have created a very basic service app to try to test this:

OnStart
timer1.enabled = True

OnStop
Timer1.enabled = Fales

Private Sub Timer1.elapsed (ByVal...blah blah blah) Handles blah

Dim fw as new StreamWriter("C:\LogFile", True)
fw.WriteLine("This is a test from the service")

End sub

but, of course, the text file is blank. Please help.

Also, what is the best way to debug a service. Is there a way to step
through procedures?

Thank you

MATT
Jul 3 '06 #1
2 1698
Hello MATT,
I am trying to create a windows service. The part I am having trouble
with is writing text to a log file. I am using a very basic
StreamWriter function to try to test this.

I have created a very basic service app to try to test this:

OnStart
timer1.enabled = True
OnStop
Timer1.enabled = Fales
Private Sub Timer1.elapsed (ByVal...blah blah blah) Handles blah

Dim fw as new StreamWriter("C:\LogFile", True)
fw.WriteLine("This is a test from the service")
End sub

but, of course, the text file is blank. Please help.
The problem is that StreamWriter buffers it's data in memory. You have to
force it to push the data onto disk by calling the Close method. A better
way is to use the "Using" statement which will force a call to Close (via
IDisposable) even in the presence of an exception

Using ( fw As New StreamWriter("C:\LogFile", True))
....
End Using

--
Jared Parsons [MSFT]
ja******@online.microsoft.com
All opinions are my own. All content is provided "AS IS" with no warranties,
and confers no rights.

Jul 3 '06 #2
Matt,

The best way to debug a service is to allow it to run as a console or
windows applications. There are a few ways of doing this. The
simplest might look like this.

Shared Sub Main()

If Environment.UserInteractive Then

' Run as a console application.
Dim service As Service1 = New Service1
service.OnStart(Nothing)
Console.WriteLine("Press ENTER to quit...")
Console.ReadLine()
service.OnStop()

Else

' Assume the Service Control Manager invoked the application.
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
ServicesToRun = New System.ServiceProcess.ServiceBase () {New
Service1}
System.ServiceProcess.ServiceBase.Run(ServicesToRu n)

End If

End Sub

There are some caveats about using this method, but for the most part
it works pretty well. The advantage here is that you can put a
breakpoint in the Main method and it will hit when launched from the
debugger. If you really want the application to run as a service
during a debugging session then you'll have to start it via the SCM and
attach the debugger manually. That can be done by clicking Debug |
Processes in Visual Studio. The downside to that is that you will miss
the entry point of the application.

Brian
MATT wrote:
Also, what is the best way to debug a service. Is there a way to step
through procedures?
Jul 3 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: redneon | last post by:
I'm trying to write a web service which, when run, writes a line to a file on the web server. I've created one under IIS and the method I'm calling looks like this... public string...
3
by: W Akthar | last post by:
Hi I am trying to create a windows service which queries SQL Server on timed intervals and depending on the results send appointments to Outlook. The problem lies when I try to create an...
3
by: Yves Royer | last post by:
Hi all, I have a little question about Windows Services. For my app i need 3 different Windows Services, each with its own functionality. I'm trying to make a test service to see what happens...
0
by: Seth | last post by:
For some reason my service works fine except that it will create the file in my c drive, but will not write to the file. Sorry if this is a duplicate post, i have found some that ask the same...
1
by: Remy De Almeida | last post by:
Hi , Sorry i am posting this again as no one seems have taken notice of this in the distributed appln forum I have a remote server which works fine. The Client ans server work with no problem....
7
by: GD | last post by:
Hi, I am trying to call a webservice from a windows service application. It works only if I launch the windows service app from VS.Net 2005 (Worked around from Main()) or from a winform test...
2
by: Andez | last post by:
I've wrote a windows service that performs simple functions within our application. To ensure safe running of the service - if it errors we want to know where when how - it logs to a text file - in...
0
by: iprogrammer | last post by:
i have a problem when i try to run my windows service ..which is "Error 1053: The service did not respond to the start or control request in a timely fashion" >after this i cannot anything with...
2
by: =?Utf-8?B?SnVzdCBjbG9zZSB5b3VyIGV5ZXMgYW5kIHNlZQ== | last post by:
Hello all i am trying to learn how to program a windows service application i had add an event log and a timer that used to write to file here is the code protected override void OnStart(string...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.