<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>A K Chauhan's Blog &#187; C#</title>
	<atom:link href="http://www.akchauhan.com/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.akchauhan.com</link>
	<description>PHP, MySql, JavaScript, JQuery, Prototype, Drupal, HTML, CSS and more...</description>
	<lastBuildDate>Mon, 02 Jan 2012 03:29:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Create Windows Service to Schedule PHP Script execution</title>
		<link>http://www.akchauhan.com/create-windows-service-to-schedule-php-script-execution/</link>
		<comments>http://www.akchauhan.com/create-windows-service-to-schedule-php-script-execution/#comments</comments>
		<pubDate>Tue, 22 Sep 2009 15:02:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Service]]></category>
		<category><![CDATA[Job Schedule]]></category>
		<category><![CDATA[Windows Service]]></category>

		<guid isPermaLink="false">http://www.akchauhan.com/?p=560</guid>
		<description><![CDATA[If you want to schedule your PHP script or any other command on Windows Platform recursively after a fix interval of time then you can create a Windows Service for it. Windows service is very easy to create in Visual Studio.Net. I&#8217;m using C# language for this scheduler service you can also use VB.net for [...]]]></description>
			<content:encoded><![CDATA[<p>If you want to schedule your PHP script or any other command on <strong>Windows Platform</strong> recursively after a fix interval of time then you can create a <strong>Windows Service</strong> for it. Windows service is very easy to create in Visual Studio.Net. I&#8217;m using <strong>C#</strong> language for this scheduler service you can also use <strong>VB.net</strong> for it. Basically we need this type of service if we want to execute some PHP script in background in a specific interval of time. For example, If you want to import data in bulk from some web service.</p>
<p>You need to follow these steps: -</p>
<ul>
<li>
Create a project by using the Windows Service application template. This template creates a class for you that inherits from ServiceBase and writes much of the basic service code, such as the code to start the service.</li>
<li>
Write the code for the OnStart and OnStop procedures, and override any other methods that you want to redefine.
</li>
<li>
Add the necessary installers for your service application. By default, a class that contains two or more installers is added to your application when you click the Add Installer link: one to install the process, and one for each associated service that your project contains.
</li>
<li>
Build your project.
</li>
<li>
Create a setup project to install your service, and then install it.
</li>
<li>
Access the Windows 2000 Services Control Manager and start your service.
</li>
</ul>
<p>For more detail check &#8220;<strong>Walkthrough: Creating a Windows Service Application in the Component Designer</strong>&#8221; topic in <strong>MSDN</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">using <span style="color: #990000;">System</span><span style="color: #339933;">;</span>
using <span style="color: #990000;">System</span><span style="color: #339933;">.</span>Collections<span style="color: #339933;">.</span>Generic<span style="color: #339933;">;</span>
using <span style="color: #990000;">System</span><span style="color: #339933;">.</span>ComponentModel<span style="color: #339933;">;</span>
using <span style="color: #990000;">System</span><span style="color: #339933;">.</span>Data<span style="color: #339933;">;</span>
using <span style="color: #990000;">System</span><span style="color: #339933;">.</span>Diagnostics<span style="color: #339933;">;</span>
using <span style="color: #990000;">System</span><span style="color: #339933;">.</span>Linq<span style="color: #339933;">;</span>
using <span style="color: #990000;">System</span><span style="color: #339933;">.</span>ServiceProcess<span style="color: #339933;">;</span>
using <span style="color: #990000;">System</span><span style="color: #339933;">.</span>Text<span style="color: #339933;">;</span>
using <span style="color: #990000;">System</span><span style="color: #339933;">.</span>Timers<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">namespace</span> MyNewService
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> partial <span style="color: #000000; font-weight: bold;">class</span> MyNewService <span style="color: #339933;">:</span> ServiceBase
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">private</span> Timer syncTimer <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>  
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> MyNewService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            InitializeComponent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">protected</span> override void OnStart<span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            syncTimer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Timer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            this<span style="color: #339933;">.</span>syncTimer<span style="color: #339933;">.</span>Interval <span style="color: #339933;">=</span> <span style="color: #cc66cc;">180000</span><span style="color: #339933;">;</span>
            this<span style="color: #339933;">.</span>syncTimer<span style="color: #339933;">.</span>Elapsed <span style="color: #339933;">+=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #990000;">System</span><span style="color: #339933;">.</span>Timers<span style="color: #339933;">.</span>ElapsedEventHandler<span style="color: #009900;">&#40;</span>this<span style="color: #339933;">.</span>syncTimer_Tick<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            syncTimer<span style="color: #339933;">.</span>Enabled <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">protected</span> override void OnStop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            syncTimer<span style="color: #339933;">.</span>Enabled <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">private</span> void syncTimer_Tick<span style="color: #009900;">&#40;</span>object sender<span style="color: #339933;">,</span> EventArgs e<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #990000;">System</span><span style="color: #339933;">.</span>Diagnostics<span style="color: #339933;">.</span>Process<span style="color: #339933;">.</span>Start<span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span><span style="color: #0000ff;">&quot;C:<span style="color: #660099; font-weight: bold;">\xa</span>mpp\htdocs<span style="color: #000099; font-weight: bold;">\t</span>ask.bat&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>  
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The <strong>task.bat</strong> file is a batch file. We are executing our PHP script through this batch file. First we set the path of directory where php.exe is located in your windows. The <strong>import.php</strong> is basically importing data in bulk from some web and we need to execute this script file background in every 3 minutes.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">@</span><span style="color: #b1b100;">echo</span> off
cd\
set path<span style="color: #339933;">=</span>C<span style="color: #339933;">:</span>\xampp\php<span style="color: #339933;">;</span>
cd <span style="color: #0000ff;">&quot;C:<span style="color: #660099; font-weight: bold;">\xa</span>mpp\htdocs&quot;</span>
php import<span style="color: #339933;">.</span>php
<span style="color: #990000;">exit</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.akchauhan.com/create-windows-service-to-schedule-php-script-execution/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

