473,699 Members | 2,550 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 1325

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

Similar topics

3
4405
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
9020
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
3211
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
1722
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
8623
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
9192
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...
0
9054
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8940
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
8895
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7781
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...
0
4390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2015
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.