如何在Windows服务中编写大量代码 [英] how to write bulk of code in windows service

查看:49
本文介绍了如何在Windows服务中编写大量代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

actully我做了Windows服务。只有我测试启动methord时执行然后创建一个文件并写入一些文本,并且还停止方法另一个方法是生成和相同的。这段代码完美执行。但是当我写一些代码而不是小代码时它没有执行.becoz间隔时间很短。但是当我增加时间间隔然后它没有安装到我的电脑上它显示错误安装后开始方法不是执行所以请评论如何在窗口服务中写大量的代码请...



我的时间间隔是1000



它完全符合这个代码



string sfilepath = @filepath / sample.txt;

string sdatetime = DateTime.Now.ToString();

System.IO.StreamWriter ofilewriter = new System.IO.StreamWriter(sfilepath,true);

ofilewriter .WriteLine(\ n+开始执行);

ofilewriter.Close();





////当我把这段代码

con.Open();

string str =select from tablename where field = 1 and field = 0;

MySqlCommand cmd = new MySqlCommand(str,con);

MySqlDataReader sdr = cmd.ExecuteReader();

while(sdr.Read())

{

mailaddress = sdr [field]。ToString();

destinationpath = sdr [field]。ToString();

extnid = sdr [field]。ToString();



string path = destinationpath.Replace('@','\\');

MailMessage mail = new MailMessage();



SmtpClient SmtpServer = new SmtpClient(smtp.gmail.com);



mail.From = new MailAddress(source mail address);

mail.To.Add(目的地邮件地址);

mail.Subject =配置文件;

mail.Body =报告;

附件附件=新附件(路径);

mail.Attachments.Add(附件);

SmtpServer.Port = 25;

SmtpServer。凭证=新的System.Net.NetworkCredential(源邮件,passwo0rd);

SmtpServer.EnableSsl = true;



SmtpServer 。发送(邮件);

MessageBox.Show(发送消息);



//表格消息中的更新已发送。< br $>


}

con.Close();



}

它无法正常工作,只发送了消息,但表中没有更新,有时消息不会像这样发送。







提前感谢....

actully i did windows service. only i test when start methord is executed then one file created and writing some text and also for stop methord another methord is creatcued and same. this code perfectly executed.but when i write some bulk of code instead of small code it is not executing .becoz the interval time is low.but when i increase the time interval then it is not install to my computer it show error "after install start methord is not excute" so please comment how to write bulk of code in window service please please...

my time interval is 1000

it is perfectly working in this code

string sfilepath = @"filepath/sample.txt";
string sdatetime = DateTime.Now.ToString();
System.IO.StreamWriter ofilewriter = new System.IO.StreamWriter(sfilepath, true);
ofilewriter.WriteLine("\n" + "Start Excuting");
ofilewriter.Close();


////when i put this code
con.Open();
string str = "select filed from tablename where field= 1 and field= 0";
MySqlCommand cmd = new MySqlCommand(str, con);
MySqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
mailaddress = sdr["field"].ToString();
destinationpath = sdr["field"].ToString();
extnid = sdr["field"].ToString();

string path = destinationpath.Replace('@', '\\');
MailMessage mail = new MailMessage();

SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

mail.From = new MailAddress("source mail address");
mail.To.Add(destination mail address);
mail.Subject = "Configuration File";
mail.Body = "Report";
Attachment attachment = new Attachment(path);
mail.Attachments.Add(attachment);
SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("source mail", "passwo0rd");
SmtpServer.EnableSsl = true;

SmtpServer.Send(mail);
MessageBox.Show("message sent");

//update in table message was send.

}
con.Close();

}
it does not work properly,only message sent but no update in table and sometime message is not send like this.



thanks in advance....

推荐答案

请看我对这个问题的评论:你什么都不说。 />


很抱歉没有给你 一个办法;根据您的信息,这可能是不可能的。只有一件事情或多或少是明确的:你在时间间隔上创建了不正确的依赖关系,我不知道为什么。它与大量代码无关。所以,这里的暗示可能会让你有更好的表现:你的大量代码只是表现出你的烂设计,这在所有情况下都是不正确的,即使代码很少;并且只有偶然(时间间隔的组合)你才会产生工作的错觉。粗略地说,如果你使用较慢的CPU,你的小代码应用程序将停止工作。换句话说,你根本没有工作申请。



对于最一般的想法,熟悉竞争条件的概念。我面对更明确和明确的术语,意思相同:对执行时间的不正确依赖: http://en.wikipedia。 org / wiki / Race_condition [ ^ ]。



你的情况很可能更加微不足道。正如你提到的间隔时间,我可以假设你使用了一些计时器。定时器可能不可靠,应谨慎使用,或根本不使用。一个简单的想法是:假设您的下一个计时器事件在前一个计时器调用的事件处理程序即使尚未执行时到来。它可以造成很大的混乱。因此,安全的替代方案是使用在一个周期中工作的单独线程,可能在每个周期中有一些休眠时间;在更高级的方法中,您可以根据前一次迭代消耗的时间间隔的实时测量来调整休眠时间。这只是一个非常基本的想法。 真正的解决方案需要更详细的知识或问题。



-SA
Please see my comment to the question: you are talking about nothing.

Sorry for not giving you a solution; this is probably impossible, based on your information. Only one thing is more or less clear: you created incorrect dependency on time interval, I have no idea why. It has nothing to do with "bulk of code". So, here is the hint which may lead you to something better: your "bulk of code" just manifests your rotten design, which is incorrect in all cases, even with little code; and only by chance (combination of time intervals) you have an illusion that it "works". Roughly speaking, should you use slower CPU, and your application with "small code" will stop working. In other words, you don't have working application at all.

For the most general idea, get familiar with the concept of race condition. I faced with more clear and definitive term meaning the same: "incorrect dependency on the time of execution": http://en.wikipedia.org/wiki/Race_condition[^].

It's likely that your case in more trivial. As you mentioned "interval time", I can assume you use some timer. Timers are potentially unreliable and should be used with great care, or not used at all. One simple idea is: imagine that your next timer event comes when the event handler invoked by previous timer even is not yet executed. It can create a great mess. So, the safe alternative would be using a separate thread working in a cycle, possibly with some sleep time in each cycle; in more advanced approach, you can adjust the sleep time based on real time measurement of time interval consumed by previous iteration. This is just one very basic idea. The "real" solution would need much more detailed knowledge or your problem.

—SA


我通过reasearch得到了这个问题的解决方案。实际上这个问题是时间间隔很小,以便在时间结束时再次重复。但是整个代码在这个时间内执行(ex 1000ms).so如果增加时间间隔然后它没有安装,并提供错误服务是不及时的时尚。对于这个问题,我写了一个代码

这就像当计时器对象启动然后服务执行ex-otimer.start();

它将再次发生时时间间隔结束。在otimer.start()

之后,它将转到计时器已用事件,在此事件中存在整个代码。此事件在计时器启动时执行()所以在这个事件中我手动停止到计时器编写整个代码之后的ex-otimer.stop()之后再次启动otimer ex-otimer.start。所以整个代码写在otimer.stop()和otimer.start()之间这样一个大块的代码将执行。
i got solution of this problem by reasearch.actually this problem is dou to time interval is small so that it repeat again again when the time is elapsed.but whole code is executing in this time(ex 1000ms).so if increase the time interval then it is not installed and gives error service is not timely fashion. for this problem i wrote a code
this is as when timer object will start then service execute for ex- otimer.start();
it will going on again again when the time interval is finished. after otimer.start()
it will go to timer elapsed event in this event whole code is present.this event is execute when the timer is start() so in this event i stop manually to the timer ex-otimer.stop() after that whole code is written and after that again start the otimer ex-otimer.start .so whole code is written in between otimer.stop() and otimer.start() in this way a bulk of code will execute.


这篇关于如何在Windows服务中编写大量代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆