473,385 Members | 1,353 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,385 software developers and data experts.

Skeleton code for windowless C# 2.0 app?

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
9 3679
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
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
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
Makes sense. Thanks for the advice, I'll look into doing it this way.
Sep 3 '07 #5
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
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
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
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
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Edvard Majakari | last post by:
Greetings, fellow Pythonistas! I'm about to create three modules. As an avid TDD fan I'd like to create typical 'use-cases' for each of these modules. One of them is rather large, and I wondered...
0
by: ECVerify.com | last post by:
When I install my .NET app on a "fresh" installed machine with the ..NET installer I get this... Windowless ActiveX Controls not supported Well, Okay, I understand that...it seems pretty...
2
by: guy | last post by:
Here's a challange: Can anybody give me an example of how to implement Windowless RichEdit in vb.net? (for example: to create a dropdown list similiar to combobox)
3
by: Peter | last post by:
Can a label control be created that is windowless? (tank you Chris for the tip) Tank you
0
by: feng | last post by:
I am getting error between my local code, that's making call to a remote object, and the called remote code. The error occoured after my remote object returns (I can debug to that point. And it...
2
by: FelixLeung | last post by:
Dear all, Anyone know how to create windowless application without 3rd party controls? I want the application look like ICQ, MSN, etc. No window but custom min, close button. Thanks Best...
2
by: Darmac | last post by:
Hi, i'm new in this list so I don't know if anyone has asked this...but... I have a process (dos app instance) started like this: _procStartInfo = new ProcessStartInfo();...
0
by: Manoj Jangid | last post by:
Hi how do I create a Windowless exe in C++ or MFC? I dont want to show any window to user Regards Manoj Jangid
3
by: poopsy | last post by:
hello all, im trying out examples for java rmi but am having a problem.. the skeleton is not being generated, only the stub is generated. i have an example with the following classes:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.