473,320 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Service will not start

dbrewerton
115 100+
I was able to get the service to install just fine so that works good. When I start the service, it stops and I am getting no details on what is wrong. So here is my code:


Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Configuration;
  3. using System.Data;
  4. using System.Data.Common;
  5. using System.Data.OleDb;
  6. using System.Data.SqlClient;
  7. using System.IO;
  8. using System.ServiceProcess;
  9. using System.Text.RegularExpressions;
  10. using System.Threading;
  11.  
  12. namespace MyServiceImport
  13. {
  14.  
  15. public partial class MyServiceData : ServiceBase
  16. {
  17.     private System.Timers.Timer _timer;
  18.     private Double ReadTime = Convert.ToDouble(ConfigurationManager.AppSettings.Get("ReadTime")) * 1000 * 60;
  19.     private DateTime _lastRun = DateTime.Now;
  20.     Thread _thread;
  21.  
  22.     public MyServiceData()
  23.     {
  24.         InitializeComponent();
  25.     }
  26.  
  27.     // These lines run when the service starts up
  28.     protected override void OnStart(string[] args)
  29.     {
  30.         _timer = new System.Timers.Timer();
  31.         _timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
  32.         _timer.Enabled = true;
  33.         _timer.Interval = ReadTime;
  34.         _timer.Start();
  35.  
  36.         #region Write to Audit Log
  37.         string Event = "Update Service";
  38.         string DoneBy = "Automation User";
  39.         string Information = "Update Service has Started.";
  40.         string MyTimeStamp = DateTime.Now.ToString();
  41.  
  42.         SqlConnection conn = new SqlConnection(DBUtils.MyService_DBString);
  43.         string writeit = "INSERT INTO MyService_Activities (Event, DoneBy, Information, TimeStamp) values(@Event, @DoneBy, @Information, @TimeStamp)";
  44.         SqlCommand WriteLog = new SqlCommand(writeit, conn);
  45.         conn.Open();
  46.         WriteLog.Parameters.AddWithValue("Event", Event);
  47.         WriteLog.Parameters.AddWithValue("DoneBy", DoneBy);
  48.         WriteLog.Parameters.AddWithValue("Information", Information);
  49.         WriteLog.Parameters.AddWithValue("TimeStamp", MyTimeStamp);
  50.         WriteLog.ExecuteNonQuery();
  51.         conn.Close();
  52.         #endregion
  53.  
  54.     }
  55.  
  56.     // writes message into the activity log that the service has stopped.
  57.     protected override void OnStop()
  58.     {
  59.         #region Write to Audit Log
  60.         string Event = "Update Service";
  61.         string DoneBy = "Automation User";
  62.         string Information = "Update Service has Stopped.";
  63.         string MyTimeStamp = DateTime.Now.ToString();
  64.  
  65.         SqlConnection conn = new SqlConnection(DBUtils.MyService_DBString);
  66.         string writeit = "INSERT INTO MyService_Activities (Event, DoneBy, Information, TimeStamp) values(@Event, @DoneBy, @Information, @TimeStamp)";
  67.         SqlCommand WriteLog = new SqlCommand(writeit, conn);
  68.         conn.Open();
  69.         WriteLog.Parameters.AddWithValue("Event", Event);
  70.         WriteLog.Parameters.AddWithValue("DoneBy", DoneBy);
  71.         WriteLog.Parameters.AddWithValue("Information", Information);
  72.         WriteLog.Parameters.AddWithValue("TimeStamp", MyTimeStamp);
  73.         WriteLog.ExecuteNonQuery();
  74.         conn.Close();
  75.         #endregion
  76.     }
  77.  
  78.     void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  79.     {
  80.         // set up connection string 
  81.         string _csMyServiceDD = DBUtils.MyService_DBString;
  82.         string DoImportNow = string.Empty, TaskDay = string.Empty, TaskStartTime = string.Empty, TaskEndTime = string.Empty;
  83.  
  84.         SqlConnection conn = new SqlConnection(_csMyServiceDD);
  85.         // Get task variables from database for comparison
  86.         string query = "SELECT TaskDay, TaskStartTime, TaskEndTime FROM MyService_TaskSchedule where TaskName = 'DoImport'";
  87.         SqlCommand cmd1 = new SqlCommand(query, conn);
  88.         // opens the connection to read
  89.         conn.Open();
  90.         SqlDataReader rdr1 = cmd1.ExecuteReader();
  91.         while (rdr1.Read())
  92.         {
  93.             TaskDay = rdr1.GetValue(0).ToString();
  94.             TaskStartTime = rdr1.GetValue(1).ToString();
  95.             TaskEndTime = rdr1.GetValue(2).ToString();
  96.         }
  97.         // closing the connection
  98.         conn.Close();
  99.  
  100.         // setting _lastRun variable to this moment.
  101.         _lastRun = DateTime.Now;
  102.         // stop the timer 
  103.         _timer.Stop();
  104.         try
  105.         {
  106.             // check if the current time is within the time range for this service task.
  107.             if (DateTime.Now.DayOfWeek.ToString() == TaskDay && DateTime.Now > Convert.ToDateTime(TaskStartTime) && DateTime.Now < Convert.ToDateTime(TaskEndTime))
  108.             {
  109.                 _thread = new Thread(new ThreadStart(ImportData));
  110.                 _thread.Name = "ImportData";
  111.                 _thread.Start();
  112.             }
  113.             // decide if there is something to do
  114.             #region Write to Audit Log
  115.             string Event = "Update Service";
  116.             string DoneBy = "Automation User";
  117.             string Information = "Update Service has started importing data.";
  118.             string MyTimeStamp = DateTime.Now.ToString();
  119.  
  120.             string writeit = "INSERT INTO MyService_Activities (Event, DoneBy, Information, TimeStamp) values(@Event, @DoneBy, @Information, @TimeStamp)";
  121.             SqlCommand WriteLog = new SqlCommand(writeit, conn);
  122.             conn.Open();
  123.             WriteLog.Parameters.AddWithValue("Event", Event);
  124.             WriteLog.Parameters.AddWithValue("DoneBy", DoneBy);
  125.             WriteLog.Parameters.AddWithValue("Information", Information);
  126.             WriteLog.Parameters.AddWithValue("TimeStamp", MyTimeStamp);
  127.             WriteLog.ExecuteNonQuery();
  128.             conn.Close();
  129.             #endregion
  130.         }
  131.         catch (Exception ex)
  132.         {
  133.             // Catch error and write to audit log
  134.             #region Write to Audit Log
  135.             string Event = "Update Service";
  136.             string DoneBy = "Automation User";
  137.             string Information = "Error occurred in Import - Message: " + ex.ToString() + "";
  138.             string MyTimeStamp = DateTime.Now.ToString();
  139.  
  140.             string writeit = "INSERT INTO MyService_Activities (Event, DoneBy, Information, TimeStamp) values(@Event, @DoneBy, @Information, @TimeStamp)";
  141.             SqlCommand WriteLog = new SqlCommand(writeit, conn);
  142.             conn.Open();
  143.             WriteLog.Parameters.AddWithValue("Event", Event);
  144.             WriteLog.Parameters.AddWithValue("DoneBy", DoneBy);
  145.             WriteLog.Parameters.AddWithValue("Information", Information);
  146.             WriteLog.Parameters.AddWithValue("TimeStamp", MyTimeStamp);
  147.             WriteLog.ExecuteNonQuery();
  148.             conn.Close();
  149.             #endregion
  150.         }
  151.         _timer.Start();
  152.     }
  153.  
Oct 17 '14 #1

✓ answered by Frinavale

Check your windows event logs for any errors that may have been recorded.

-Frinny

2 1382
Frinavale
9,735 Expert Mod 8TB
Check your windows event logs for any errors that may have been recorded.

-Frinny
Oct 17 '14 #2
Frinavale
9,735 Expert Mod 8TB
Dbrewerton,

What was the problem with your service?

-Frinny
Oct 20 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Beth via .NET 247 | last post by:
I have written a service in VB.Net. I have installed the service using InstallUtil. I was able to start the service, but once I attach to and try to debug the service it will not step into my...
3
by: ThunderMusic | last post by:
Hi, I have a service that starts an application but the application always start in background (I can't see it's window) even if I set the application options to "Normal with focus". Is there a...
1
by: Next | last post by:
I have a new domain account to use with my Windows Services. The account name is WinServ. I am trying to test my Windows Service on my XP Pro machine. However the service won't start unless...
2
by: Andy Elmhorst | last post by:
We can reproduce this consistently. If we install Windows 2000 SP4, then install .Net 1.1 ( a soon-to-be typical customer scenario ). We then install ..Net 1.0 SP2, which is required by our product...
5
by: SiD` | last post by:
when starting a windows service writte in vb.net, a messagebox appears: cannot start service from the command line or a debugger. A windows service must first be installed using installutil.exe ...
3
by: illegal.prime | last post by:
Hi all, I have a service that needs to start a regular windows application. I'm running the service as ServiceAccount.LocalSystem. But, when it starts the process (using Process.Start) the GUI...
3
by: SR | last post by:
I have a requirement from a customer to run some command line applications from a service. I have most of my service coded and I've come across an issue with the important part of the application,...
4
by: carson | last post by:
I have written two windows services: - service A does some crunching of local data files and uploads them to a central processing computer via http. - service B monitors a manifest file on a...
1
by: aj | last post by:
A few service stop/start/restart questions on SQL Server 2005 SP2, which I'll call SQLS. It looks as if there are *potentially* 6 ways to start/stop SQLS Services like the engine itself,...
7
by: SHAIL1404 | last post by:
Hey dear, i am making a product and in which i use a timer. I want when the product is being start the timer will run and when the product is off the timer will stop. If i again start the product...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.