Skeleton code for windowless C# 2.0 app?

  • Thread starter Homer J. Simpson
  • Start date
H

Homer J. Simpson

Stupid question. I've been using C# 2.0 for the past few weeks, but mostly
in the context of ASP.NET. I have little to no experience with Win32 C#
apps.

My purpose is to write a windowless application to constantly monitor a COM
port (the one my modem is using), and write what it intercepts to a log
file. I've managed to do it (to a certain extent) in a console app--I
create a SerialPort object, initialize it, set its DataReceived member so I
have my event handler...and now, of course, if I only do that my program's
gonna exit right away, so I put Thread.Sleep() in an endless loop in Main().
Kinda silly, maybe, but that's the best I could come up with for the moment.

Now I wanna get rid of the console window. I want this to work as a
windowless program--I don't care if I don't have a UI to shut it down, nor
do I need to present any sort of configuration option. This is a
quick-and-dirty app for myself only.

Can somebody show me the 'proper' skeleton code for a minimal windowless C#
2.0 program and have it set up so I can then add my code to create a
SerialPort object, with an event handler, and wait indefinitely for those
events? I'm using Visual C# 2005 Express, and I have a few options that,
because of my lack of familiarity with writing Win32 apps with C#, leave me
a little puzzled:

a) an empty project, which I wouldn't know how to structure properly for my
purposes
b) a console application, which is what I'm trying to get away from (I want
to get rid of the console window)
c) a Windows form application, which I'd then have to modify somehow to get
rid of the default form.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Homer said:
My purpose is to write a windowless application to constantly monitor a COM
port (the one my modem is using), and write what it intercepts to a log
file. I've managed to do it (to a certain extent) in a console app--I
create a SerialPort object, initialize it, set its DataReceived member so I
have my event handler...and now, of course, if I only do that my program's
gonna exit right away, so I put Thread.Sleep() in an endless loop in Main().
Kinda silly, maybe, but that's the best I could come up with for the moment.

Now I wanna get rid of the console window. I want this to work as a
windowless program--I don't care if I don't have a UI to shut it down, nor
do I need to present any sort of configuration option. This is a
quick-and-dirty app for myself only.

Can somebody show me the 'proper' skeleton code for a minimal windowless C#
2.0 program and have it set up so I can then add my code to create a
SerialPort object, with an event handler, and wait indefinitely for those
events? I'm using Visual C# 2005 Express, and I have a few options that,
because of my lack of familiarity with writing Win32 apps with C#, leave me
a little puzzled:

a) an empty project, which I wouldn't know how to structure properly for my
purposes
b) a console application, which is what I'm trying to get away from (I want
to get rid of the console window)
c) a Windows form application, which I'd then have to modify somehow to get
rid of the default form.

What about a Windows Service.

I would expect your IDE to be able to generate a stub for such.

Arne
 
H

Homer J. Simpson

What about a Windows Service.
I would expect your IDE to be able to generate a stub for such.

I'm a complete C# n00b. Unless Visual C# 2005 Express can give me a
template ready for me to hook my code into, I wouldn't know where to start.

The reason I'm trying to do this in C# to begin with (C++ is more my area)
is that I like how the .NET library hides all the ugly details you otherwise
would have to deal with.
 
P

Peter Duniho

Homer said:
[...]
Now I wanna get rid of the console window. I want this to work as a
windowless program--I don't care if I don't have a UI to shut it down, nor
do I need to present any sort of configuration option. This is a
quick-and-dirty app for myself only.

Personally, I think it's a little goofy to write an application that has
no direct way of telling it to exit. Even if all it has is a single
window with a button named "Exit" or an empty window you can close,
where's the harm in that? Even if it is just something you're going to
use as a one-off tool, why would you want to have to go to the Task
Manager to shut down the application?

That said...I think you want option c:
c) a Windows form application, which I'd then have to modify somehow to get
rid of the default form.

Just edit the Main() method in your Form-based application so that it
doesn't create a form. See Program.cs in your application's project for
the implementation. You may find that you don't even need to call
Application.Run().

And since you didn't ask but I have my opinions anyway:

In your handling of the serial port, IMHO you should do one of the
following:

* Don't use loop calling Sleep()...just call something that will
block indefinitely. For example, create a ManualResetEvent instance,
initialized to unset, and wait on it.

* Even better, don't use the async methods for the SerialPort
class. The only reason they exist is so that your other threads can go
do stuff while the serial port i/o is working. But since you don't have
any other threads, just using blocking i/o calls with the SerialPort
class in your main thread.

These apply whether you're in a console application or a form-less Forms
application.

Pete
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Homer said:
I'm a complete C# n00b. Unless Visual C# 2005 Express can give me a
template ready for me to hook my code into, I wouldn't know where to start.

There are plenty of "How to write a Windows Service in C#" guides
available on the net.

It looks as if VS Express does not come with a service template, but
other VS versions and SharpDevelop does.

Arne
 
G

Guest

Peter said:
You could try SharpDevelop, an open-source free IDE. It does have a template
for Windows Services.

Which is what I wrote ...

Top posting confusion ?

Arne
 
H

Homer J. Simpson

c) a Windows form application, which I'd then have to modify somehow to
Just edit the Main() method in your Form-based application so that it
doesn't create a form. See Program.cs in your application's project for
the implementation. You may find that you don't even need to call
Application.Run().

In the end, that's what I'm now doing--just calling Application.Run(),
without any parameter, after initializing the SerialPort object and setting
up the event handler. My app's been doing exactly what it should since I've
turned it into a windowless app, whereas as a console application, it'd die
after a couple of hours of inactivity (I forget the exact message--something
about a safe handle being closed, and the stack trace would include the
serial data event handler). Same code otherwise.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top