Solved

C# Windows Service To Run & Stop .exe App

Posted on 2014-04-25
19
5,286 Views
Last Modified: 2014-05-02
Hello Experts,

I am planning to create Windows Service and through WS, I want to run exe application which is present in local path lets say "C:\Run.exe".

I have written some database operations in this exe app.

I want to know C# code which will run this exe by Windows Service and after completion of database operations, service must close this exe too.

I tried with some sample codes i got from Google but its creating issues.

Thanks in advance !
0
Comment
Question by:Md Shah
  • 8
  • 8
  • 2
  • +1
19 Comments
 
LVL 21

Expert Comment

by:Dale Burrell
Comment Utility
Why not just run it as a windows scheduled task? Very easy...
0
 
LVL 8

Author Comment

by:Md Shah
Comment Utility
My Client is expecting that he wants a Windows Service... So I have no other go..

Cheers
0
 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
If its a Windows service, do not use ASP.NET in your tags. ASP.NET is for web applications, and programmers that know about Windows services might not see your question.

What code do you have up to now, and what are the issues. Knowing that will help us help you.
0
 
LVL 21

Expert Comment

by:Dale Burrell
Comment Utility
Persuade your client? They don't know whats technically best... and it will be cheaper to create a scheduled task :) And as JamesBurger says thats not ASP.NET.
0
 
LVL 21

Expert Comment

by:Dale Burrell
Comment Utility
But if you truly need a windows service then I believe Visual Studio comes with template code. And MS have a walkthrough.
0
 
LVL 8

Author Comment

by:Md Shah
Comment Utility
Oh I am new to EE.

Anyways below is the code what I tried to use.

It was working fine when I have used it in Console App but not in WS.

Code
When used in WS, its making multiple entries in Task Manager after every timer interval but actual exe database operations is not being processed.

Questions:
1) Firstly is it good practice to use Process.Kill()  in C# code, will there be any risk in production ?
2) I have kept interval for 5 seconds, now lets say previous exe task has not been completed then will it break that and start a new one coz i dont want that to happen, it will break my project. What I want is at first time, exe must run completing all database operations and let say it has taken 7 seconds then the next process which starts after 5 seconds had to wait till earlier process gets completed and second process must start at 7 seconds consequent.

Hope i m clear, if not, let me know for further details...
0
 
LVL 21

Expert Comment

by:Dale Burrell
Comment Utility
I would suggest that if you feel the need to use Process.Kill() then you are doing something wrong.

I think you would solve most of your problems by including the EXE in your project and calling the method in your EXE which carries out the functionality that you require and then completes.

That has he advantage of being fully compiled in, rather than dynamically linked. By waiting for it to complete you will never have to kill it. And by waiting for it to complete before you restart your timer you will never get it running again before its completed.

I think linking it dynamically, and running it in its own process if overcomplicating a simple problem.
0
 
LVL 8

Author Comment

by:Md Shah
Comment Utility
Fine Dale.. As you say I will not go for Process.Kill()..

I don't know if its safe to describe about my project details in EE here..

Anyways this is what it is..

I need to create two things for my project.

1) Windows Form Application: This will read new messages from a HelpDesk email account and based on message body content sent by client, it will perform some database operations and send the DB result as CSV file by replying to clients message.

2) Windows Service: This will run the above exe file on timely interval say around after every 30 minutes. Also sometimes above exe operation of fetching the data from DB and sending email may take a time more than 30 minutes if exported data is huge. In this scenario, next process must wait until earlier one completes its task and second process must start consequently.

I have completed 1st task, Windows Form Application, and its working fine.

Now I am stuck up with second task, creating a Windows Service which runs my exe :(
0
 
LVL 21

Expert Comment

by:Dale Burrell
Comment Utility
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 8

Author Comment

by:Md Shah
Comment Utility
Yes. I have gone through that MSDN link before and have learnt how to create a WS.

The above WS was created based on this.

But now I want to know C# code to be written in my WS so as to Run & Stop my EXE app.
0
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 400 total points
Comment Utility
OK - thats make more sense. Easy...

1/ In your exe file (WinForms project) make sure you have the code you need to run within a pure class (i.e. not within your WinForms class). e.g. MyEmailHandler.

2/ Reference your exe file in your windows service project. And add a 'using' statement in your windows service to reference the namespace of your WinForms project.

3/ In your service create an instance of said class. e.g. MyEmailHandler myHandler = new MyEmailHandler().

4/ Call the method which carries out the required actions on the class. e.g. myHandler.ProcessEmails();
0
 
LVL 8

Author Comment

by:Md Shah
Comment Utility
Hey Dale..

I have modified the code as what u have suggested and it has worked; though i need to sit for 3 hours in resolving the issues which rose while modifying my exe application for it to dwell properly with Windows Service.

Anyways, now my service is sending reply emails to client after receiving a new one.

But I have 2 questions here:

1) By above process, during testing, I have set a trigger time interval of 10 seconds and I have sent 2 new messages to my helpdesk email. Now WS has started running the process for 1st time and to complete its execution it has taken time more than 15 seconds and thus what i have observed is client has received 4 reply mails. He has received reply to his 2 emails twice each; which is not correct. Can u say why it is so and how i can fix this ?

2) If I change my EXE application, then do I need to add that reference in my Windows Service project and then I have to restart my service to find new changes ? Is it possible to have something like i don't have to touch my Windows Service but can merge my new changes in it?
Initially, I thought through WS i will write C# code to RUN & STOP .exe file. So that later if I change my exe project I will place my new .exe file at that location and WS will continue RUNNING my new .exe file with no need to touch my WS ?

Just in case if u want to my WS code :
 code
0
 
LVL 78

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 100 total points
Comment Utility
you could use the database mail properties to send the mail. Question: why export to csv?
I'm guessing you are checking if it is a new problem or a reply to the incident request with the incident # as part of the subject line and the customer is asked to keep using the incident #.  The helpdesk should also be using the same database. The service should be running continually, get new email, add to process queue, repeat, in the process queue you process each item and then remove the item from the queue, repeat until queue is empty

As for the duplicate(s) somewhere in your flow there is a problem.
0
 
LVL 8

Author Comment

by:Md Shah
Comment Utility
First part i cannot change. Client is expecting email with DB output as CSV file.
Though I have completed this code and its working fine.

Second part is interesting to know, actually i m new to Windows Service development, can you please be brief about 'PROCESS QUEUE', how can I achieve that, sample code or site links would be grateful.

Thanks.
0
 
LVL 78

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 100 total points
Comment Utility
Here is a good tutorial on queues and stacks
http://www.codeguru.com/columns/vb/working-with-queues-and-stacks.htm
0
 
LVL 8

Author Comment

by:Md Shah
Comment Utility
@David:

Thanks for sharing. Let me take some time now, I will check and let you know if it works for duplicating issue.

Cheers
0
 
LVL 21

Assisted Solution

by:Dale Burrell
Dale Burrell earned 400 total points
Comment Utility
To prevent multiple emails you need to disable the timer while you process emails, then re-enable it again once you finish processing, otherwise the timer will continue to timeout and start processing emails while you are processing the first time.
0
 
LVL 21

Assisted Solution

by:Dale Burrell
Dale Burrell earned 400 total points
Comment Utility
To late bind to your exe file call Type.InvokeMember - you don't need the reference then and can change the exe without restarting the service.

http://msdn.microsoft.com/en-us/library/system.type.invokemember(v=vs.110).aspx
0
 
LVL 8

Author Closing Comment

by:Md Shah
Comment Utility
Thanks Guys for your time & regret for delay, got stuck up with other things..!!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now