Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Dear,

All

I have some problem in Windows Service. Am calling one Webservice in Windows Service with their reference. Here am putting a timer method for to run the Windows Service for every minute. Here am facing a problem, once start the Windows service, for suppose in that WebService method need to insert 10000 records in a table, So at that time it will take more than minute. But here we give 1 minute to run Windows service, this windows service runs every minute. So finally am going to restrict this, so once WebService method called upto that service complete, then Run the Windows Service. I mean that WebService is in running state, no need to run the Windows for every minute upto complete the WebService task.

See below code of Windows Service:

C#
namespace WinVJService
{
partial class WinVJService : ServiceBase
{
protected System.Timers.Timer timer;

public WinVJService()
{
InitializeComponent();
}

protected void InitializeTimer()
{
if (timer == null)
{
timer = new System.Timers.Timer();
timer.AutoReset = true;
timer.Interval = ReadAppSettingInterval();
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
}
}

private void timer_Elapsed(object source, System.Timers.ElapsedEventArgs e)
{
RunCommands();
}

protected void RunCommands()
{

RefVJService.VJServiceSoapClient objJob = new RefVJService.VJServiceSoapClient(); // WebService Reference Object
objJob.RunJob();                                         // Here calling Web Service method


EventLog.WriteEntry(ServiceName + "VJ Service ran ok");
}

protected double ReadAppSettingInterval()
{
double interval = 60000, tempInterval; //initialize to ten minutes.
if (ConfigurationSettings.AppSettings != null &&
ConfigurationSettings.AppSettings["IntervalMinutes"] != null)
{
string intervalMin;
intervalMin = ConfigurationSettings.AppSettings["IntervalMinutes"];
if (Double.TryParse(intervalMin, NumberStyles.Any,
NumberFormatInfo.InvariantInfo, out tempInterval))
interval = tempInterval * 60000;
}
return interval;
}

protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
EventLog.WriteEntry(ServiceName + "VJ Service started");
InitializeTimer();
timer.Enabled = true;
}

protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
EventLog.WriteEntry(ServiceName + "VJ Service stopped");
timer.Enabled = false;
}
}
}
Posted
Updated 8-May-12 19:34pm
v4
Comments
manognya kota 9-May-12 1:04am    
Added <pre> tag for the code.
Sandeep Mewara 9-May-12 1:09am    
Not sure what exactly your question is. What do you need hep with?
Sergey Alexandrovich Kryukov 9-May-12 1:33am    
"Here am putting a timer method for to run the Windows Service for every minute." Since that moment, the whole question already makes no sense. You cannot "run" it this way. An application can be run as a Windows Service only if it is designed as such and only if it is executed by the Service Controller.
--SA

1 solution

Hi Friends... I got the solution from google.

This may helpful to some other people those suffering with this problem.

protected void RunCommands()
{
timer.Enabled=false; // disable time before the operation starts
RefVJService.VJServiceSoapClient objJob = new RefVJService.VJServiceSoapClient(); // WebService Reference Object
objJob.RunJob(); // Here calling Web Service method
EventLog.WriteEntry(ServiceName + "VJ Service ran ok");
timer.Enabled=true; // enable time once the operation completed
}



Thanks & Regards,
Vinod Bheemisetty.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900