I have created windows service by using following code. I am able to install and start process successfully. It also record event log record. which i can see from server explorer but it is not serving my intended task.
I guess i am wrong somewhere in connection string calling and declaring.I have stored my connection string in app.config file as below:
I want to run service after every 15 mins. But my code is not working properly.
When i start service Event log message-service started
After every 15 mins is shows - service running
When i stop service it shows -service stopped
when its shows running app it should execute following code to retrieve data from database , process on it and again store it kind of.....
My code is below. Please help me i am stuck in it. I try to debug it also but it is not giving any error. and my below code is working OK in asp.net web application.
Please help me in this regards. Thanks in advance.
="1.0"="utf-8"
<configuration>
<connectionStrings>
<add name="b1" connectionString="data source=server;Trusted_Connection=False;database= nilam ;User ID=e64;Password=35ixtyF0ur" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Reflection;
using System.Configuration;
using DotNetHelpers.UltimateImageResizer;
using System.IO;
using System.Timers;
using System.Threading;
namespace ImageResizerService
{
public partial class Service1 : ServiceBase
{
private DirectoryInfo imgDir;
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["b1"].ConnectionString);
string ImgName;
byte[] rImg;
private System.Timers.Timer scheduleTimer1 = null;
public Service1()
{
InitializeComponent();
if (!EventLog.SourceExists("IResizeSource"))
{
EventLog.CreateEventSource("IResizeSource", "IResizeLog");
}
eventLog1.Source = "IResizeSource";
eventLog1.Log = "IResizeLog";
scheduleTimer1 = new System.Timers.Timer();
}
protected override void OnStart(string[] args)
{
scheduleTimer1.Interval = 15 * 60 * 1000;
scheduleTimer1.Enabled = true;
scheduleTimer1.Start();
scheduleTimer1.Elapsed += new ElapsedEventHandler(ResizeImages1);
eventLog1.WriteEntry("Service Started");
}
protected override void OnStop()
{
scheduleTimer1.Stop();
scheduleTimer1.Enabled = false;
eventLog1.WriteEntry("Service Stopped");
}
public void ResizeImages1(object sender, ElapsedEventArgs e)
{
eventLog1.WriteEntry("Running");
cn.Open();
string sPro = "select * from Properties";
SqlDataAdapter adptPro = new SqlDataAdapter(sPro, cn);
DataSet dsPro = new DataSet("Properties");
adptPro.Fill(dsPro, "Properties");
string sImg = "select img_no,img_name,create_date, modify_date,img,flag from propertyImages";
SqlDataAdapter adptImg = new SqlDataAdapter(sImg, cn);
DataSet dsImg = new DataSet("propertyImages");
adptImg.Fill(dsImg, "propertyImages");
int cntPro = dsPro.Tables[0].Rows.Count;
int cntImg = dsImg.Tables[0].Rows.Count;
for (int iPro = 0; iPro < cntPro; iPro++)
{
DataRow drPro = dsPro.Tables[0].Rows[iPro];
for (int jPro = 0; jPro < zPro; jPro++)
{
mid = mid + "0";
}
int fileNo = 10;
for (int i = 1; i <= fileNo; i++)
{
if (i <= 9)
{
stamp = "p" + mid + drPro["propertyid"] + "_0" + i;
}
else
{
stamp = "p" + mid + drPro["propertyid"] + "_" + i;
}
string ImgExist = Path.GetFullPath("~/Images/") + stamp + ".jpg";
if (File.Exists(ImgExist))
{
bool flag = false;
DateTime cdateData = new DateTime();
DateTime mdateData = new DateTime();
int ImgNo = 0;
for (int iImg = 0; iImg < cntImg; iImg++)
{
DataRow drImg = dsImg.Tables[0].Rows[iImg];
ImgNo = int.Parse(drImg["img_no"].ToString());
ImgName = drImg["img_name"].ToString();
cdateData = DateTime.Parse(drImg["create_date"].ToString());
mdateData = DateTime.Parse(drImg["modify_date"].ToString());
if (ImgName == stamp)
{
flag = true;
}
else
{
flag = false;
}
if (flag == true)
break;
}
if (flag == true)
{
DateTime cdateFile = File.GetCreationTime(Path.GetFullPath("~/Images/" + ImgName + ".jpg"));
DateTime mdateFile = File.GetLastWriteTime(Path.GetFullPath("~/Images/" + ImgName + ".jpg"));
if (cdateFile1 != cdateData1 || mdateFile1 != mdataData1)
{
ResizeImagesToDB();
DateTime modify_date = mdateFile;
DateTime create_date = cdateFile;
UpdateToDB(create_date, modify_date, rImg, ImgNo);
string LImgName = "";
byte[] LImg = null;
SqlCommand cmd = new SqlCommand("Select img_name,img from propertyImages where img_name='" + ImgName + "'", cn);
SqlDataReader br = cmd.ExecuteReader();
while (br.Read())
{
LImgName = br["img_name"].ToString();
LImg = (byte[])br["img"];
}
br.Close();
FileStream fs = new FileStream(Path.GetFullPath("~/Large/") + LImgName + ".jpg", FileMode.Create, FileAccess.ReadWrite);
fs.Write(LImg, 0, LImg.Length);
fs.Flush();
fs.Close();
ResizeImageToDisk();
}
}
else
{
ResizeImagesToDB();
SqlCommand cmd = new SqlCommand("Select img_name,img from propertyImages where img_name='" + ImgName + "'", cn);
SqlDataReader br = cmd.ExecuteReader();
while (br.Read())
{
LImgName = br["img_name"].ToString();
LImg = (byte[])br["img"];
}
br.Close();
}
}
}
}
cn.Close();
cn.Dispose();
}
}
}