| | |
windows service with threads
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2006
Posts: 399
Reputation:
Solved Threads: 14
HI guys,
I need to create a windows service that runs a constant ping request to a list of servers and puts the data back into a text file for each server. It will read in from a text file with ip addresses in. It will also need to do other things but this is where i want to start. Now i don't want it written for me i just would like some helpful tips. I have never programmed in c# before and so dont have a clue. Got this of a microsoft website understand it but its still not that helpful. Cheers in advance for any help. Also it gives me the impression that only one thread can run at a time i need to ping around 20 servers at the same time is this possible it must be?
I need to create a windows service that runs a constant ping request to a list of servers and puts the data back into a text file for each server. It will read in from a text file with ip addresses in. It will also need to do other things but this is where i want to start. Now i don't want it written for me i just would like some helpful tips. I have never programmed in c# before and so dont have a clue. Got this of a microsoft website understand it but its still not that helpful. Cheers in advance for any help. Also it gives me the impression that only one thread can run at a time i need to ping around 20 servers at the same time is this possible it must be?
C# Syntax (Toggle Plain Text)
using System; using System.Threading; public class Worker { // This method will be called when the thread is started. public void DoWork() { while (!_shouldStop) { Console.WriteLine("worker thread: working..."); } Console.WriteLine("worker thread: terminating gracefully."); } public void RequestStop() { _shouldStop = true; } // Volatile is used as hint to the compiler that this data // member will be accessed by multiple threads. private volatile bool _shouldStop; } public class WorkerThreadExample { static void Main() { // Create the thread object. This does not start the thread. Worker workerObject = new Worker(); Thread workerThread = new Thread(workerObject.DoWork); // Start the worker thread. workerThread.Start(); Console.WriteLine("main thread: Starting worker thread..."); // Loop until worker thread activates. while (!workerThread.IsAlive) ; // Put the main thread to sleep for 1 millisecond to // allow the worker thread to do some work: Thread.Sleep(1); // Request that the worker thread stop itself: workerObject.RequestStop(); // Use the Join method to block the current thread // until the object's thread terminates. workerThread.Join(); Console.WriteLine("main thread: Worker thread has terminated."); } }
If my post helped add to my rep!
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
No, you can have a large number of threads running at any time, although actual activity is limited and timeshared over the available CPU power of your box.
eg, if you have a single CPU with HT - thus pretending to be 2 CPUs directly technically it does only 2 things at a time, however with fancy timesharing by windows it will appear more than 2 threads run (for example take a look in task manager and see how many processes there are, let alone if you use something like sysinternals process explorer and see how many threads per process)
OK, as a rule a service should not make windows or write to consoles - as a lot of the server versions of windows are either set not to, or dont by default allow it
Other things to remember when making a service is that if you as a user map a drive, the service cant see it, even if it runs "as you" .. it has its own instance, and wont see any mapped drives, it should do its own.
As you're making some form of net monitoring system, a text file is possibly not a good place to keep things, as the more servers you get the more likely you are to run into file locking issues.
The key facts are however, for all the threads you start you must terminate them.. and handle any issues within them.
eg, if you have a single CPU with HT - thus pretending to be 2 CPUs directly technically it does only 2 things at a time, however with fancy timesharing by windows it will appear more than 2 threads run (for example take a look in task manager and see how many processes there are, let alone if you use something like sysinternals process explorer and see how many threads per process)
OK, as a rule a service should not make windows or write to consoles - as a lot of the server versions of windows are either set not to, or dont by default allow it
Other things to remember when making a service is that if you as a user map a drive, the service cant see it, even if it runs "as you" .. it has its own instance, and wont see any mapped drives, it should do its own.
As you're making some form of net monitoring system, a text file is possibly not a good place to keep things, as the more servers you get the more likely you are to run into file locking issues.
The key facts are however, for all the threads you start you must terminate them.. and handle any issues within them.
•
•
Join Date: Feb 2006
Posts: 399
Reputation:
Solved Threads: 14
hi,
basically i am getting unix servers to write to a shared drive, this needs to be read in by a windows service and added to a sql server database. This i think shld be done by a windows service that runs at startup. At the end it will need to check the database is up and check a shared drive and some other statup checks. Then runnin in threads it needs to check for new monitoring files read them add them to a sql server database and then delete them. It also needs to ping a list of servers from a database and add the ping response times bck into a databse. I dont want the windows service to display any consoles etc just run in the background. All info will be displayed in a c# front end that will read in from the sql server database.
So a windows service cant read or delete from a shared drive what about if it maps it itself as part of statup?
Thanks for your help
basically i am getting unix servers to write to a shared drive, this needs to be read in by a windows service and added to a sql server database. This i think shld be done by a windows service that runs at startup. At the end it will need to check the database is up and check a shared drive and some other statup checks. Then runnin in threads it needs to check for new monitoring files read them add them to a sql server database and then delete them. It also needs to ping a list of servers from a database and add the ping response times bck into a databse. I dont want the windows service to display any consoles etc just run in the background. All info will be displayed in a c# front end that will read in from the sql server database.
So a windows service cant read or delete from a shared drive what about if it maps it itself as part of statup?
Thanks for your help
If my post helped add to my rep!
![]() |
Similar Threads
- Windows service and threads and sockets (C#)
- Windows Service Problem (C#)
- SYMANTEC EMAIL PROXY! Please help me!!! (Viruses, Spyware and other Nasties)
- WIN98SE protection (Viruses, Spyware and other Nasties)
- Trouble with Antispylab spyware, malware (Viruses, Spyware and other Nasties)
- Tried to follow threads but messed up (Viruses, Spyware and other Nasties)
- Cannot access Yahoo/Hotmail (i've read other threads, but couldnt solve) (Networking Hardware Configuration)
- Possibility of mini servers (C)
Other Threads in the C# Forum
- Previous Thread: Insert Data Method
- Next Thread: How to communicate between cell Phone and web application
| Thread Tools | Search this Thread |
.net access algorithm array backup barchart bitmap box broadcast buttons c# check checkbox client clock combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees developer development draganddrop drawing dynamiccreation encryption enum excel file form format forms function gdi+ hospitalmanagementsystems httpwebrequest image index input install interface java label list listbox mandelbrot math microsystems mouseclick mysql operator password path photoshop picturebox pixelinversion post priviallages. programming property radians regex remoting richtextbox running... serialization server sleep soap socket sql sqlserver stack statistics stream string table temperature text textbox thread time timer update usercontrol validation visualstudio webbrowser windows windowsformsapplication winforms wpf write xml






but it should also have code in case that drive disconnects (such as something goes wrong)..