|
Greetings,
I am pretty new to console applications (I mostly used to do C# Windows Applications stuff) and I currently need to create a console application for a little personnal project, however, there are a couple of things that I do not know how to do with the console applications.
1- How do I keep a console alive even though the main method has completed?
2- How can I use a function similar to the timer object in a console application?
I really appreciate your time and replies, thank you.
-Vickie
|
|
|
|
|
Vickie,
1. I am not sure about what you called "alive", but I think you can start a process like windows service in main method.
2. as to the timer, here is an example from the help file:
using System;
using System.Threading;
class TimerExampleState
{
public int counter = 0;
public Timer tmr;
}
class App
{
public static void Main()
{
TimerExampleState s = new TimerExampleState();
// Create the delegate that invokes methods for the timer.
TimerCallback timerDelegate = new TimerCallback(CheckStatus);
// Create a timer that waits one second, then invokes every second.
Timer timer = new Timer(timerDelegate, s,1000, 1000);
// Keep a handle to the timer, so it can be disposed.
s.tmr = timer;
// The main thread does nothing until the timer is disposed.
while(s.tmr != null)
Thread.Sleep(0);
Console.WriteLine("Timer example done.");
}
// The following method is called by the timer's delegate.
static void CheckStatus(Object state)
{
TimerExampleState s =(TimerExampleState)state;
s.counter++;
Console.WriteLine("{0} Checking Status {1}.",DateTime.Now.TimeOfDay, s.counter);
if(s.counter == 5)
{
// Shorten the period. Wait 10 seconds to restart the timer.
(s.tmr).Change(10000,100);
Console.WriteLine("changed...");
}
if(s.counter == 10)
{
Console.WriteLine("disposing of timer...");
s.tmr.Dispose();
s.tmr = null;
}
}
}
|
|
|
|
|
Vickie Madigan wrote:
1- How do I keep a console alive even though the main method has completed?
- Don't create other threads as background. The app should stay open as long as at least one non-background thread is alive.
- Start a message loop. Either a form or a service, typically.
- Trick the Main method into not exiting until you want, like this:
class Class1
{
[STAThread]
static void Main(string[] args)
{
while (GlobalFlags.Running);
}
}
internal sealed class GlobalFlags
{
private static bool _running = true;
public static bool Running
{
get {return _running;}
set {_running = value;}
}
}
Vickie Madigan wrote:
2- How can I use a function similar to the timer object in a console application?
Use the System.Timers.Timer object, or a System.Threading.Timer object.
Grim (aka Toby) MCDBA, MCSD, MCP+SB
SELECT * FROM user WHERE clue IS NOT NULL
GO
(0 row(s) affected)
|
|
|
|
|
Well well, this is very CPU-hungry.
|
|
|
|
|
Geez, I assumed you'd know how to make a tight loop not melt the CPU.
Replace while (GlobalFlags.Running); with while (GlobalFlags.Running){System.Threading.Thread.Sleep(100);}
Grim (aka Toby) MCDBA, MCSD, MCP+SB
SELECT * FROM user WHERE clue IS NOT NULL
GO
(0 row(s) affected)
|
|
|
|
|
I need to gather data from an xml document and access it in these ways. I need to show all data in 1 window to start then the user can access each section of the form to view and edit the data via seperate forms. For example a customer has a "Details" section, a "Payment" section, and a "Comments" section. When the user pulls an account up it shows these sections in a window. The comments has many lines of comments, the other 2 have details in drop-downs and labels. When the user opens a menu item to view or edit one of these sections i want another window to open so they can view/edit the info. To sum up the problem i need to collaborate these sections so they are easily obtainable (possibly in a dataset) so i can use the data in both the main form and the sub-forms. any comments appreciated.
|
|
|
|
|
In my windows application,i embed a Com webbrowser control.In DocumentComplete
Event i add thid code:"Event= (HTMLDocumentEvents2_Event)axWebBrowser1.Document;Event.onclick += new mshtml.HTMLDocumentEvents2_onclickEventHandler(ClickEventHandler);" the ClickEventHandler function is defined as :
private bool ClickEventHandler(mshtml.IHTMLEventObj e)
{
MessageBox.Show(e.type + ":" + e.srcElement.tagName);
return true;
}
Now the problem is,when i first load a html file,it work correctly,but when i load a new file again,the MessageBox appear twice,load third file,it appear three time,and so on.So,What can i do to get rid of it,Let the message just appear one time.
I will appreciate you help!
|
|
|
|
|
Hello!
I just installed Microsoft SQL Server Express and it gave me one warning: That IIS was not installed.
I wonder, do I have to have IIS installed to be able to use Microsoft SQL Server Express with Visual studio and C#? I want to create and play with databases, use northwind and so on.
Thanks in advance!
~~~~~~~~~~~~~~
Martin Lundberg
Student, Sweden
I have to thank every member of the Code Project for making it such a great place for a beginner to learn!
|
|
|
|
|
I haven't tried SQL Express, because I already use SQL Server Enterprise, but the System Requirements don't list IIS as a requirement. My thoughts are you don't need it to use the Server. I can't say anything for the SQL Server Tools though. Those MIGHT be web based...
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
SQL Server Express is the Yukon version of MSDE, right?
If I'm not mistaken, SQL Server Reporting Services is built into Yukon, instead of being an add-on like it is for 2000. The Reporting Services require IIS to be installed for them to work properly.
Most likely this is the functionality that you won't have because you're missing IIS. There may be something else, I haven't been too worried about getting familiar with Yukon yet.
Grim (aka Toby) MCDBA, MCSD, MCP+SB
SELECT * FROM user WHERE clue IS NOT NULL
GO
(0 row(s) affected)
|
|
|
|
|
Thank you! =)
~~~~~~~~~~~~~~
Martin Lundberg
Student, Sweden
I have to thank every member of the Code Project for making it such a great place for a beginner to learn!
|
|
|
|
|
I am developing a project which features usercontrols like WinForms Controls. I have created controls like text box,picture,etc.,
Each control is a usercontrol and has its own properties.now the problem is when i select the usercontrol i have to get the rectangle that bounds the border of the control when you select a windows control. The user should know which control he/she has selected. Has anyone found a solution for this ?
Thanks in advance
Happy Coding
MankayarKarasi
|
|
|
|
|
You are making your own controls for textbox, picture, etc? Sounds like you're doing a LOT of work when you don't need to be. Either way, a user control should have a Bounds variable, and you can make your own rectangle object with the information the Bounds variable contains.
Mankayarkarasi wrote:
The user should know which control he/she has selected.
I don't understand what you mean here. Are you trying to highlight the controls, or are there overlapped controls where the selected one needs brought to the front? Explain this a little more.
If you need extra functionality for the controls that already exist in C#, it would be a LOT easier for you to create new classes that inherit the old controls...then just add your new functionality.
Hope I helped out at least a little.
Good luck
- D
|
|
|
|
|
Thanks. The project I am working inherits few properties from Windows control.
The User can create a report design of his own. so the controls will have the funcition similar to windows control.
When you drag drop a control from Windows forms. A rubberband like highlight will be given to the control.This will be done easily in MFC using crectracker.
In .NET I have dynamically calculated the corners for each control and with graphics has drawn rectangles to the edges. this provides a highlighted control.
Happy Coding
MankayarKarasi
|
|
|
|
|
I'm a budding programmer looking for vital threads. Learning language and using it are 2 different things although one MUST receive it before they can express it. Grasping the basic concepts of OOD and the languages (VB,C#,C++(concepts)), learning the system classes, .net and language functional constructs (gdi,reflection,diagnostics), etc. etc. , What is(are), the single best tools to use to convert ideas into code?
1. UML??
2. Keep reading??
3. Keep doing??
4. Usage of interfaces, events(delegates)??
5. A pad and a pencil??
Any thoughts to help steer me into coding effective design, architecture, OUTPUT!!! .....TIPS .. pLeAsE
Trane Lives (that's Coltrane)
THANX
ds
|
|
|
|
|
Hi
Your question is very trivial . only i can say is that keep doing practice that you read
Naveen
|
|
|
|
|
<per> Your question is very trivial . only i can say is that keep doing practice that you read
I do......
Let me drill down...
One of my problems is class contruction.
a. How many classes
b. How to lay out an effective model of structure and functionality per the project concept
I become a victim of endless trial and error
If I was to look at your development environment, what would I see. What tools and in what order do you apply them.
eg. Let's say I want to program a 'Code Project' Presentation Manager.
a. Gathers All articles (local)
b. Allows for thumbnail creation(any size)
c. Allows article rating system and sorting
d. Profiles words in all article, datamanaging for searching features
e. Tracking of article and user preferences
f. User Interface Controls for thumbnail generator
Basically I'm gather thoughts and ideas and laying them down in text
Must I lay it down architecturally before beginning to code.
Does Everyone in the 'KNOW' do it that way
What are the individual styles of coding that are acceptable
At the minimum, what is necessary
How much is too much
Just looking for advice
Thanx
ds
|
|
|
|
|
It depends on if it is a simple weekend project or something on a larger scale.
After over two decades of development, I usually start of drafting out a spec on what the project will require. A simple list (I usually use MS Word in Outline mode). Then I go to each of these requirements and break them down (using nesting in Word's outlines)and determine how they will be structured and how there structure will relate to the other requirements.
If it is a database based application, I will probably use Viso and layout the database.
After that, I start to construct usually with a white dry erase board first. Get all the flow laid out and implement it. Typically, in a database n-tier application, I will start with the lower tiers and work my way up. Usually, I will build tests and most functionality in each tier progrmatically to save time later chasing bugs throught he tiers.
I personaly never got my head wrapped around UML (never taken the time), so I do not bother. I have heard though, that there are some nice tools out there to help.
For web based applications it is much similar, but I draft out all the web page screens in the specs part so that I have an idea of what everything will look like before I start.
Rocky <><
www.HintsAndTips.com
www.GotTheAnswerToSpam.com
|
|
|
|
|
Hi,
I've created a Form that I use to print a receipt (prints Form controls).
I've placed the code
this.printDocument1.Print();
in the FormLoad Event because I want the Form to print right away. I also what the Form to close right away. It seems the only place that caused the Form to close right away was putting "this.Close()" in the FormPaint event. Is there a better way to do this? I would like to set the Form Opacity to "0", but then it stops my Close on the FormPaint.
Thanks,
Ron
|
|
|
|
|
Yes, there is. Your putting the print code in the wrong form. If this form is for nothing but printing, leave the printing code in the form that creates it. FormLoad should only be used for initializing the forms code and its look. You can't close the form in the FormLoad event, because, technically, it hasn't been created yet. Do something like this:
Form2 frmPrint = new Form2;
frmPrint.printDocument1.Print();
frmPrint = null;
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Thanks Dave!
Works great!
I'm new to programming so it took me a few minutes to realize that I needed to move my "assigning values to controls" to printDocument1_PrintPage event from the OnLoadForm event. The values are passed from Form1.
Ron
|
|
|
|
|
I have a need to write a service that deals with custom incoming/outgoing emails. I'm using the MS SMTP service on my server, without POP3 support.
The plan is to pick up incoming emails from the SMTP server's drop folder, process the contents, and if necessary create a new outbound message and drop it in the SMTP server's pickup folder.
The problem I'm having is that System.Web.Mail.MailMessage doesn't appear to have any methods that I can use to either create a MailMessage object from an existing file, nor does it have a method that will allow me to dynamically create an outgoing message and drop it into the pickup folder to be delivered.
So far I've had no luck searching the web for 3rd party open-source libraries that perform these functions, either. (Actually, I haven't found any at all, not even commercial.)
For my specific application, I don't need MIME support, or even HTML support. It's all plain text. What I do need is the ability to load a file into a mail object and have the headers automatically parsed out into a collection, with property access to From, To, Subject, etc.
Any suggestions?
Grim (aka Toby) MCDBA, MCSD, MCP+SB
SELECT * FROM user WHERE clue IS NOT NULL
GO
(0 row(s) affected)
|
|
|
|
|
Write one yourself; it's quite simple. You can read about SMTP in RFC 228[^], but it's really quite simple. SMTP headers use a Key: Value pair on separate lines. Headers that span multiple lines place whitespace before the Value (thus you should deduce that headers begin at the beginning of a line).
So, you could easily create a Hashtable and a TextReader derivative on the SMTP mail message. Call ReadLine and parse the text. If the line begins with white space, concatenate the line with the previous value. Put the key/value pairs into the Hashtable . Call ReadLine until you encounter a \r\n pair on a line by itself.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Umm, are you sure you don't mean RFC 821[^]? 228 seems to be a one-line update to RFC 70.
Anywho. I had considered writing it myself, but I was hoping to save a little time by find an existing one. I also need to generate outgoing e-mails as a text stream that I can save to file. I'd like to have a MailMessage class that automatically creates the other necessary headers that I don't want to have to manually provide.
See, that's the problem. I'm too much of a perfectionist when I do my own stuff. I suffer from "Speculative Generality" syndrome.
Grim (aka Toby) MCDBA, MCSD, MCP+SB
SELECT * FROM user WHERE clue IS NOT NULL
GO
(0 row(s) affected)
|
|
|
|
|
Yes, RFC 821. Sorry. I was trying to recall it from memory where I've packed several dozen RFCs. Numbers are just too hard to remember.
I'm also a perfectionist when it comes to extensibility and modularity, but honestly SMTP headers are easy to parse. It's much easier than parsing MIME headers (which really isn't that hard). That simple algorithm I gave you would work nicely.
You could check out IP!Works[^], though. They've been doing library development for IP-related technology for quite some time and are pretty good products. They do have SMTP and MIME components, as well as many, many other components for all your IP-related needs (SMTP, HTTP, POP, IMAP, SSH, etc.).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|