windows service

  • Thread starter Thread starter Peter Jausovec
  • Start date Start date
I need a windows service that listens to a directory , if any xml file is
placed in the directory it reads it and calls a class and receives a
string.. this is the code I came up with. it works but it kills my processor
after a few min. what should i do??

Thanks

private void button6_Click(object sender, System.EventArgs e)

{

NewClaims.clsNewClaims objNewClaimI = new NewClaims.clsNewClaims();



while (true)

{

//Get the File in the directory

DirectoryInfo dir = new DirectoryInfo(@"C:\test4");

FileInfo[] xmlfiles = dir.GetFiles("*.xml");

foreach( FileInfo file in xmlfiles)

{

string strHMI="";

//Opens an existing UTF-8 encoded text file for reading.

StreamReader sr = File.OpenText(file.FullName);

string data= sr.ReadToEnd();


strHMI = objNewClaimI.ConvertNewClaimsXmlUICIToHMItxt(data);

int intExtension=file.Name.IndexOf(".");

FileInfo outputFile = new
FileInfo(@"c:\test5\"+file.Name.Substring(0,intExtension)+".txt");

StreamWriter strWriter =new StreamWriter(File.Open(@"c:\test5\" +
file.Name.Substring(0,intExtension) +".txt",FileMode.Create,
FileAccess.Write ));

strWriter.Flush();

strWriter.Write(strHMI.ToString());


strWriter.Close();

sr.Close();

file.Delete();

}


}

}
 
Yes, I agree. A separate thread will be the solution.

The problem is in the while (true) loop. Since there is no special criteria
for exiting the loop, the loop is infinitely traversed, thereby hanging the
application.

In a separate thread, this logic will run fine.

Regards
Vipul


Peter Jausovec said:
Hi,

I think you should run it in a separate thread.

--
Regards,
Peter Jausovec
(http://blog.jausovec.net)
CK said:
I need a windows service that listens to a directory , if any xml file is
placed in the directory it reads it and calls a class and receives a
string.. this is the code I came up with. it works but it kills my
processor after a few min. what should i do??

Thanks

private void button6_Click(object sender, System.EventArgs e)

{

NewClaims.clsNewClaims objNewClaimI = new NewClaims.clsNewClaims();



while (true)

{

//Get the File in the directory

DirectoryInfo dir = new DirectoryInfo(@"C:\test4");

FileInfo[] xmlfiles = dir.GetFiles("*.xml");

foreach( FileInfo file in xmlfiles)

{

string strHMI="";

//Opens an existing UTF-8 encoded text file for reading.

StreamReader sr = File.OpenText(file.FullName);

string data= sr.ReadToEnd();


strHMI = objNewClaimI.ConvertNewClaimsXmlUICIToHMItxt(data);

int intExtension=file.Name.IndexOf(".");

FileInfo outputFile = new
FileInfo(@"c:\test5\"+file.Name.Substring(0,intExtension)+".txt");

StreamWriter strWriter =new StreamWriter(File.Open(@"c:\test5\" +
file.Name.Substring(0,intExtension) +".txt",FileMode.Create,
FileAccess.Write ));

strWriter.Flush();

strWriter.Write(strHMI.ToString());


strWriter.Close();

sr.Close();

file.Delete();

}


}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top