Link to home
Start Free TrialLog in
Avatar of razza_b
razza_b

asked on

C# polling program to monitor db entries

Hi

I'm trying to come up with a way to use a C# program(either console or windows service) that gets called from a sql job running every 10 mins that checks a DB table for an entry and if so it triggers of a notiification window on user machine with a message.

anyone any ideas what is best way to do this?
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Why not put the program into the 'scheduled tasks' on the user PC and let it be started from their instead of from the SQL Server job.  I'd think that could be rather easier.
If you're willing to run this as a service, why does the SQL job need to be involved?  Can't you just write a C# application and connect directly to the DB and look for the table/entry?  That kind of application would be fairly trivial to implement.

Set up a Timer in a windows service, every 10 minutes open a connection to the DB and check for the entry.  If it exists, fire off the user notification.  And of course, it would be even more trivial to not use a service, but just a windows forms application instead.

If you'd like examples, let me know I am sure there are tons out there.
Avatar of razza_b
razza_b

ASKER

i think a windows service would be simplest way to do this and let it poll every 10 mins checking the DB and fire off the notification,

any samples on this would be good thanks.
SOLUTION
Avatar of jmcmunn
jmcmunn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>>A note:  some people would argue that a standard windows form/console appication run using the Windows task scheduler would be a better approach, but...

Ermmm - does a service actually support displaying a message to the user ? (requirement specified by the asker).

ps.  Displaying a message to the user from a windows form app is trivial.
The service should not directly show a message (because a service should not technically use anything with UI), however it can easily call out to a windows form application with Process.Start()

You could also notify the user with any other form of communication such as email, a sound, or updating a web page etc...
OK - so the service way involves coding a service and an app.
Well, there is no technical reason it can't be one project in visual studio.  You can certainly add a windows form to a windows service project and launch an interface however you see fit.  You would just have to add the right references to the service project.

A "proper" design would probably include two projects:  a windows service, and a windows form/console app.
Avatar of razza_b

ASKER

to show a message to the user would need to be taskbar notification as the users dont check thier email to see any messages and the user is not to interact with any GUI.

Another point i should add is - can a windows service detect the machine user name?

if not then i would need a console app(as i can get username) being fired by a sql job every 10 mins.
Avatar of razza_b

ASKER

"A "proper" design would probably include two projects:  a windows service, and a windows form/console app. "

so would the windows service be firing the console?
yes, in this case the service would do something like:

Process.Start(pathToConsoleApplication);

Or, a windows form app if you desired.

To be more clear:  it does not have to launch the app to do the DB check, just when you are notifying the user if there is something they need to see.
Avatar of razza_b

ASKER

ok, so if i have visual studio project with a windows service that can get (with path to GUI) and GUI(get the enironment.username) and if username and message in DB exists, then i can use GUI to appear as the message?
I'm not sure I understand what you are asking...the service can do everything involved with the database, and then call out to the windows form if it finds what it needs.
Avatar of razza_b

ASKER

yes i thin get it now, how would i call the form?

just by doing this in the service code...

                        if (!msg.Value.Equals(DBNull.Value))
                        {
                            Form1 frm = new Form1(msg.ToString());
                        }
Avatar of razza_b

ASKER

how do i actually get the service installed?
>>yes i thin get it now, how would i call the form? ...

No.  A service does not support any user interface (as I mentioned earlier).  You need to follow the advice of jmcmunn and use are more roundabout route.
"yes i thin get it now, how would i call the form?"

If it is in the same project that would work...otherwise, you would need to do something like....

Process.Start("<path to program>"); //where the path to your exe is here...maybe in a config file or else in the same local path


"how do i actually get the service installed?"
http://msdn.microsoft.com/en-us/library/ddhy0byf.aspx

A Google search will provide tons of info on creating a service installer.
Avatar of razza_b

ASKER

i have the windows service and form in same project and in the onstart method i will call my SP and return my message value and if its not null i call the form...

Form1 frm = new Form1(msg.ToString());

but im having a problem trying trying to install exe its says windows cannot find 'InstallUtil.exe'
The onstart method is not where you will want to call your sp...you will want a timer to call it every 10 minutes.

It sounds like due to your unfamiliarity with services, you may want to use a simple Windows Form all and run it with windows task scheduler.  Unless you are willing to follow tutorials online for getting the service up and running, it is going to be more overhead than it is worth.
Avatar of razza_b

ASKER

i have this..

private Timer timer = new Timer();
        private double servicePollInterval;

        public ScheduledEquipService()
        {
            InitializeComponent();
            servicePollInterval = 120000;
        }

        protected override void OnStart(string[] args)
        {
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.Interval = servicePollInterval;
            timer.AutoReset = true;
            timer.Enabled = true;
            timer.Start();
        }

and in my timer event method im calling my sp.

i have managed to install my exe into services in admin tools but nothing is happening at all. and i have started the service.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial