473,749 Members | 2,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

My C# windows service hangs

1 New Member
My issue is similar to a previous post that you have answered in

Windows Service written in c# Hangs


We use only 1 service which fetches files(*.xlsx) from a folder and inserts into a table of a Database.Once the details are updated into the database, the file will be deleted by the service. The folder from which the service fetches is shared and new files get dumped then and then. (Average 5 files per day). If the content in the .xlsx file has some error or if it doesn't match with the sql constraints , it will be moved to another folder which has an error log specifying the type of error.

My issue is that my service is in Started mode but the files are not consumed into the database.


But if i RESTART the service, the files get consumed.

Pls help me with this

Code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. using System.IO;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Diagnostics;
  8. using System.Linq;
  9. using System.ServiceProcess;
  10. using System.Text;
  11. using System.Data.SqlClient;
  12. using System.Data.OleDb;
  13. using System.Data.Common;
  14.  
  15. namespace Impexp_Service
  16. {
  17. public partial class Service1 : ServiceBase
  18. {
  19. System.Timers.Timer T1 = new System.Timers.Timer();
  20. public Service1()
  21. {
  22. InitializeComponent();
  23. }
  24.  
  25. protected override void OnStart(string[] args)
  26. {
  27. ///start
  28. ///
  29.  
  30. {
  31. SqlConnection strconnection = new SqlConnection();
  32. strconnection.ConnectionString = @"Data Source=XXXXX;Initial Catalog=YYYY;User ID=sa;Password=*****;";
  33. strconnection.Open();
  34. // To get the all files placed at the shared path
  35. DirectoryInfo directory = new DirectoryInfo(@"D:\YY\Data\");
  36. FileInfo[] files = directory.GetFiles("*.xlsx");
  37.  
  38.  
  39.  
  40. foreach (var f in files)
  41. {
  42. string path = f.FullName;
  43.  
  44. // TO establish connection to the excel sheet
  45. string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";";
  46. //Create Connection to Excel work book
  47. OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
  48.  
  49.  
  50.  
  51. excelConnection.Open();
  52. //Create OleDbCommand to fetch data from Excel
  53. OleDbCommand cmd = new OleDbCommand("Select * from [Report$]", excelConnection);
  54.  
  55. DbDataReader dr = cmd.ExecuteReader();
  56. // OleDbDataReader dReader;
  57. // dReader = cmd.ExecuteReader();
  58. SqlBulkCopy sqlBulk = new SqlBulkCopy(strconnection);
  59. //Give your Destination table name
  60. sqlBulk.DestinationTableName = "TableXYZ";
  61. sqlBulk.WriteToServer(dr);
  62.  
  63. excelConnection.Close();
  64.  
  65. File.Delete(path);
  66.  
  67.  
  68.  
  69.  
  70. // To move error files to the error folder
  71.  
  72.  
  73.  
  74. /// end
  75.  
  76.  
  77. T1.Interval = 20000;
  78. T1.Enabled = true;
  79. T1.Start();
  80.  
  81. T1.Elapsed += new System.Timers.ElapsedEventHandler(T1_Elapsed);
  82. }
  83. }
  84. }
  85.  
  86.  
  87. void T1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  88. {
  89. T1.Enabled = false;
  90. try
  91. {
  92. SqlConnection strconnection = new SqlConnection();
  93. strconnection.ConnectionString = @"Data Source=XXXX;Initial Catalog=YYY;User ID=conapt;Password=****;";
  94. strconnection.Open();
  95. // To get the all files placed at the shared path
  96. DirectoryInfo directory = new DirectoryInfo(@"D:\YYY\Data\");
  97. FileInfo[] files = directory.GetFiles("*.xlsx");
  98.  
  99.  
  100.  
  101. foreach (var f in files)
  102. {
  103. string path = f.FullName;
  104.  
  105. // TO establish connection to the excel sheet
  106. string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";";
  107. //Create Connection to Excel work book
  108. OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
  109.  
  110. try
  111. {
  112. excelConnection.Open();
  113. //Create OleDbCommand to fetch data from Excel
  114. OleDbCommand cmd = new OleDbCommand("Select * from [Report$]", excelConnection);
  115.  
  116. DbDataReader dr = cmd.ExecuteReader();
  117. // OleDbDataReader dReader;
  118. // dReader = cmd.ExecuteReader();
  119. SqlBulkCopy sqlBulk = new SqlBulkCopy(strconnection);
  120. //Give your Destination table name
  121. sqlBulk.DestinationTableName = "TableXYZ";
  122. sqlBulk.WriteToServer(dr);
  123.  
  124. excelConnection.Close();
  125.  
  126. File.Delete(path);
  127.  
  128.  
  129.  
  130. }
  131. // To move error files to the error folder
  132. catch (Exception exp)
  133. {
  134.  
  135. excelConnection.Close();
  136. File.Move(path, Path.Combine(@"D:\YYY\error\", f.Name));
  137. string path1 = @"D:\YYY\error\error.txt";
  138. if (File.Exists(path1))
  139. {
  140. // Create a file to write to.
  141. using (StreamWriter sw = File.AppendText(path1))
  142. {
  143. sw.WriteLine("File : " + path + " : " + exp.Message);
  144. sw.Flush();
  145.  
  146. }
  147. }
  148.  
  149.  
  150. T1.Enabled = true;
  151. T1.Start();
  152.  
  153. }
  154. }
  155. strconnection.Close();
  156.  
  157. // End of TRY 1
  158.  
  159. }
  160. catch (UnauthorizedAccessException UAEx)
  161. {
  162. string path1 = @"D:\YYYYY\error\error.txt";
  163. if (File.Exists(path1))
  164. {
  165. // Create a file to write to.
  166. using (StreamWriter sw = File.AppendText(path1))
  167. {
  168. sw.WriteLine(UAEx.Message);
  169. sw.Flush();
  170.  
  171. }
  172. }
  173. T1.Enabled = true;
  174. T1.Start();
  175. }
  176. catch (PathTooLongException PathEx)
  177. {
  178. string path1 = @"D:\YYYY\error\error.txt";
  179. if (File.Exists(path1))
  180. {
  181. // Create a file to write to.
  182. using (StreamWriter sw = File.AppendText(path1))
  183. {
  184. sw.WriteLine(PathEx.Message);
  185. sw.Flush();
  186.  
  187. }
  188. }
  189. T1.Enabled = true;
  190. T1.Start();
  191. }
  192. T1.Enabled = true;
  193. T1.Start();
  194.  
  195.  
  196. }
  197.  
  198. protected override void OnStop()
  199. {
  200. }
  201. }
  202.  
Mar 31 '14 #1
0 1330

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

Similar topics

3
4414
by: Jacob Crossley | last post by:
Hello all. We have about 10 Window's services that we wrote in c#. We use them to process row's that we have queued up in various SQL tables. The services seem to hang at least once in any given 24 hour period. Once we reset the service, it goes about its processing business until the next time it hangs. We are wondering if the hanging problem is due to a load issue (too many windows services on a single machine), a code flaw, or an...
4
9022
by: Kris | last post by:
I have a Windows Service in C# talking to a serial port and using Remoting. It also uses several COM objects. On customer's computer the service will occassionally hang somewhere - the service still shows on a Task Manager list. In Services it says it is still running. However there is no way to stop it other than by rebooting the whole computer. No exception (including non-CLS) is ever generated. I added a separate System.Timers.Timer...
7
3215
by: Ashish Khandelwal | last post by:
I have a Windows Service in C# talking to a serial port and using Remoting. It also uses several COM objects. On server the service will occassionally hang somewhere - the service still shows on a Task Manager list. In Services it says it is still running. However there is no way to stop it other than by rebooting the whole computer. No exception (including non-CLS) is ever generated. I added a separate System.Timers.Timer
3
1724
by: mohithmohith | last post by:
Hello All, We have around 3 windows services written in C#. We use them to process files from a folder into database and further to process the data into various SQL Tables. All the services hang atleast once in a week stopping the entire process. The files are queued up in the folders waiting to be processed. The services in the Service Control Manager seems to be running but they do not process any files. But when the processes are...
0
8996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9566
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8256
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6800
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.