Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C# Windows Service To Run & Stop .exe App

Posted on 2014-04-25
19
5,752 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
ID: 40024145
Why not just run it as a windows scheduled task? Very easy...
0
 
LVL 8

Author Comment

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

Cheers
0
 
LVL 40
ID: 40024149
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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 21

Expert Comment

by:Dale Burrell
ID: 40024178
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
ID: 40024180
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
ID: 40024181
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
ID: 40024193
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
ID: 40024303
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
ID: 40024367
0
 
LVL 8

Author Comment

by:Md Shah
ID: 40024374
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
ID: 40024378
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
ID: 40024581
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 80

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 100 total points
ID: 40024871
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
ID: 40024880
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 80

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 100 total points
ID: 40024923
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
ID: 40024927
@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
ID: 40025176
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
ID: 40025180
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
ID: 40037886
Thanks Guys for your time & regret for delay, got stuck up with other things..!!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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