struck in launching .exe file created using c# using windows

Jan 1 2018 8:53 AM
hi all
I have created in windows application and also one windows service using c#. this windows application has the database connection and do some functions etc which i want to daily repeat. so i have created the windows service. when i debug windows service in debug mode everything works fine. but after installation of windows service it is created the processes of that windows application. but that windows application is not initiating..

My Service code is:

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.Timers;
using System.IO;
using System.Xml;

namespace OTMService
{
public partial class OTMService : ServiceBase
{
private Timer timer1 = null;
string tt1, tt2, tt3, tt4, tt5, tt6, thetime, time, tmp1, tmp0ch, basetmp, tmpAMPM;
int spp = 0, stime = 0;
public OTMService()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
timer1 = new Timer();
this.timer1.Interval = 60000; //for every 30 secs=30000 and for 1 minute 60000 and 300000
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
timer1.Enabled = true;
Library.WriteErrorLog("Test window service started--Just Khan");
}

private void timer1_Tick(object sender, ElapsedEventArgs e)
{
mymainProg();
}
protected override void OnStop()
{
timer1.Enabled = false;
Library.WriteErrorLog("Test window service stopped--Just Khan-Service got stopped");
}
public void mymainProg()
{
string dirpath = AppDomain.CurrentDomain.BaseDirectory;
//xml reading for getting time mentioned in xml file.
try
{
XmlDocument xmlDoc1 = new XmlDocument();
xmlDoc1.Load(dirpath+"OTMAppSetting.xml");
thetime = xmlDoc1.SelectSingleNode("Data/ServTime").InnerText;
//thetime = "02:04 PM";
tmp1 = thetime.Substring(3, 2);
tmp0ch = thetime.Substring(3, 1);
basetmp = thetime.Substring(0, 3);
tmpAMPM = thetime.Substring(6, 2);
spp = Convert.ToInt32(tmp1);
stime = checktime(spp);
if(tmp0ch=="0")
{
tt1 = basetmp + "0"+ stime.ToString() + " " + tmpAMPM;
stime = stime + 1;
tt2 = basetmp + "0" + stime.ToString() + " " + tmpAMPM;
stime = stime + 1;
tt3 = basetmp + "0" + stime.ToString() + " " + tmpAMPM;
stime = stime + 1;
tt4 = basetmp + "0" + stime.ToString() + " " + tmpAMPM;
stime = stime + 1;
tt5 = basetmp + "0" + stime.ToString() + " " + tmpAMPM;
stime = stime + 1;
tt6 = basetmp + "0" + stime.ToString() + " " + tmpAMPM;
}
else
{
tt1 = basetmp + stime.ToString() + " " + tmpAMPM;
stime = stime + 1;
tt2 = basetmp + stime.ToString() + " " + tmpAMPM;
stime = stime + 1;
tt3 = basetmp + stime.ToString() + " " + tmpAMPM;
stime = stime + 1;
tt4 = basetmp + stime.ToString() + " " + tmpAMPM;
stime = stime + 1;
tt5 = basetmp + stime.ToString() + " " + tmpAMPM;
stime = stime + 1;
tt6 = basetmp + stime.ToString() + " " + tmpAMPM;
}

}
catch (Exception g)
{
Library.WriteErrorLog("Xml loading error for time: Error" + g.ToString());
}
//Write code here to do some job depends on your requirement
DateTime dt = DateTime.Now;
time = dt.ToString("hh:mm tt");
/*if (time == "02:49 PM" || time == "02:52 PM" ||
time == "02:56 PM" || time == "02:57 PM" ||
time == "02:58 PM" || time == "02:59 PM")*/
if (time == tt1 || time == tt2 ||
time == tt3 || time == tt4 ||
time == tt5 || time == tt6)
{
//Library.WriteErrorLog("Timer ticked now inside time... and some job has been done successfully--JustKhan-Job Initiated :");
try
{
Library.WriteErrorLog("Inside time and under try and catch Before Using");
//killing already existing process....
foreach (var process in Process.GetProcessesByName("OTMBackendServiceExe"))
{
process.Kill();
}
//-------------killing completed...
using (Process pp = Process.Start(dirpath + "OTM_Plan_BackExe\\OTMBackendServiceExe.exe"))
{
int id = pp.Id;
//Library.WriteErrorLog("Process iD: "+id.ToString());
//Library.WriteErrorLog("Process Name: " + pp.ProcessName.ToString());
Process tempProc = Process.GetProcessById(id);
//tempProc.WaitForExit(60000);
//tempProc.WaitForExit();
Library.WriteErrorLog("Process completed all");
}
}
catch (Exception ex)
{
Library.WriteErrorLog("Main Exe cannot be started: " + ex.ToString());
}

}
}

private int checktime(int ss)
{
int theret = 0;
switch (ss)
{
case 54:
theret = ss - 2;
break;
case 55:
theret = ss - 3;
break;
case 56:
theret = ss - 4;
break;
case 57:
theret = ss - 5;
break;
case 58:
theret = ss - 6;
break;
case 59:
theret = ss - 6;
break;
default:
theret = ss;
break;
}
return theret;
}
}
}

and the windows application which has to be run through service :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Xml.Linq;
using System.Xml;
using System.IO;
using Oracle.DataAccess.Client;
using Excel = Microsoft.Office.Interop.Excel;
using System.Net.Mail;

namespace OTMBackendServiceExe
{
public partial class Form1 : Form
{
string oraCstr, qrystr, qrystr1, OraCon, malstatus;
bool chk1 = false, chk2 = false;
int lcount = 0, chkkedd=0;
string empname, email_id, sup_email, cc_maillst, fcc_maillst, skilltype, product, skilltrack, current_level, target_level, tomail_sub;
string mailfor, otmmailbody, mail_sent_date, mail_sent_status, mailerr_msg;
string repformpath, repoutputpath, repfilename;
StringBuilder mlmsg = new StringBuilder();

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
WriteToFile("Testing: App Started");
XmlDocument xmlDoc = new XmlDocument();
//xmlDoc.Load("d:\\product.xml");
xmlDoc.Load("OTMAppSetting.xml");
//string id = xmlDoc.SelectSingleNode("Data/Short_Fall").InnerText;
oraCstr = "";
oraCstr = xmlDoc.SelectSingleNode("Data/DBstr").InnerText;
frommail = xmlDoc.SelectSingleNode("Data/frommmail").InnerText;
cc_maillst = xmlDoc.SelectSingleNode("Data/ccmaillst").InnerText;
WriteToFile("Started @:"+DateTime.Now.ToString());
//--------------------------------------
//-----these are for checking purpose------------
//dbcheck();
//string dd = mailcheck();
//WriteToFile("MailStatus :" + dd);
//To Enable from here...
//--------------------------------------
nightqry();
allresettings();
skillupgrademail();
//--------------------------------------
//all functions End here.....
WriteToFile("Ended @:" + DateTime.Now.ToString());
Application.Exit();
//this.Close();
}

in task manager it shows the process of that backend exe.



Need help. this backend exe is not getting executed when i run through service. if i run directly without any problem it runs.

Answers (2)