By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,559 Members | 1,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,559 IT Pros & Developers. It's quick & easy.

Skeleton code for windowless C# 2.0 app?

P: n/a
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.
Sep 3 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Homer J. Simpson wrote:
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
Sep 3 '07 #2

P: n/a
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.
Sep 3 '07 #3

P: n/a
Homer J. Simpson wrote:
[...]
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
Sep 3 '07 #4

P: n/a
Makes sense. Thanks for the advice, I'll look into doing it this way.
Sep 3 '07 #5

P: n/a
Homer J. Simpson wrote:
>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.
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
Sep 3 '07 #6

P: n/a
Take a look at:'
http://www.eggheadcafe.com/articles/20041204.asp
I like Peter's implementation of a multi service Windows Service.

"Homer J. Simpson" <ro**@127.0.0.1wrote in message
news:Ow*************@TK2MSFTNGP06.phx.gbl...
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.

Sep 4 '07 #7

P: n/a
You could try SharpDevelop, an open-source free IDE. It does have a template
for Windows Services. You can always reload the project back into C# Express.
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"Arne Vajhøj" wrote:
Homer J. Simpson wrote:
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.

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
Sep 4 '07 #8

P: n/a
Peter Bromberg [C# MVP] wrote:
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 Vajhøj" wrote:
>Homer J. Simpson wrote:
>>>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.
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
Sep 5 '07 #9

P: n/a
>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().
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.
Sep 5 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.