首页 新闻 会员 周边

WindowsService中的线程

0
悬赏园豆:50 [已解决问题] 解决于 2012-09-28 16:02

我新建了一个web服务,我想实现一个效果:当服务运行的时候,每隔几分钟发一次邮件和收一次邮件,在每隔几分钟停止,收发邮件这两个方法已经写好啦,就是不知道 在下面两个方法中该怎么写啊!receive是我的收邮件的方法,sendmail是我写好的发送邮件的方法,下面该怎么写,请帮帮忙,在线等待中!

protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。

Thread thd = new Thread(new ThreadStart(receive));
Thread thd2 = new Thread(new ThreadStart(sendmail));
}

protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。

}

 

待重逢的主页 待重逢 | 初学一级 | 园豆:10
提问于:2012-09-28 11:53
< >
分享
最佳答案
1

bool Running;

protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。

Running = true;

while(Running){

Thread thd = new Thread(new ThreadStart(receive));
Thread thd2 = new Thread(new ThreadStart(sendmail));

Thread.Sleep(分钟数 * 60 * 1000);

}
}

protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
Running = false;
}

收获园豆:50
I,Robot | 大侠五级 |园豆:9783 | 2012-09-28 12:23

我按照你的这种写法,服务就启动不了了,是怎么回事啊!错误是:服务没有及时响应启动或控制请求

待重逢 | 园豆:10 (初学一级) | 2012-09-28 14:48

改一下

protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。

        Running = true;

        new Thread(()=>{

                while(Running){

                        Thread thd = new Thread(new ThreadStart(receive));
                        Thread thd2 = new Thread(new ThreadStart(sendmail));
                        Thread.Sleep(分钟数 * 60 * 1000);
                }
        }).Start();
}
I,Robot | 园豆:9783 (大侠五级) | 2012-09-28 15:03

@待重逢: 

对了,你自己创建的那两个thread没用调用start方法

I,Robot | 园豆:9783 (大侠五级) | 2012-09-28 15:07

@狼Robot: 

new Thread((这里该写啥啊)=>
待重逢 | 园豆:10 (初学一级) | 2012-09-28 15:07

@狼Robot: 调用啦

待重逢 | 园豆:10 (初学一级) | 2012-09-28 15:09

@待重逢: 

啥都不用写

I,Robot | 园豆:9783 (大侠五级) | 2012-09-28 15:11

@狼Robot: 报错啊!委托未采用0个参数

待重逢 | 园豆:10 (初学一级) | 2012-09-28 15:12

@待重逢: 

Running = true;
new Thread(()=>{
while(Running){
Thread thd = new Thread(new ThreadStart(receive));
thd.Start();
Thread thd2 = new Thread(new ThreadStart(sendmail));
thd2.Start();
Thread.Sleep(1 * 60 * 1000);
}).Start();
}

待重逢 | 园豆:10 (初学一级) | 2012-09-28 15:13

@狼Robot: 是这样写吗

待重逢 | 园豆:10 (初学一级) | 2012-09-28 15:14

@待重逢: 

==我看看

I,Robot | 园豆:9783 (大侠五级) | 2012-09-28 15:14

@待重逢: 

protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。

        Running = true;

        new Thread(DoWork).Start();
}

protected void DoWork(){
{

        while(Running){

            new Thread(new ThreadStart(receive)).Start();
            new Thread(new ThreadStart(sendmail)).Start();
            Thread.Sleep(分钟数 * 60 * 1000);
        }
}
I,Robot | 园豆:9783 (大侠五级) | 2012-09-28 15:17

@待重逢: 

就这样了,再试试看吧

I,Robot | 园豆:9783 (大侠五级) | 2012-09-28 15:19

@狼Robot: 

while (Running)
{
Thread thd = new Thread(new ThreadStart(receive));
thd.Start();
Thread thd2 = new Thread(new ThreadStart(sendmail));
thd2.Start();
Thread.Sleep(1 * 60 * 1000);
}

那两个start还需要吗?

待重逢 | 园豆:10 (初学一级) | 2012-09-28 15:19

@狼Robot: 还有你是不是多写了一对{}啊

待重逢 | 园豆:10 (初学一级) | 2012-09-28 15:21

@待重逢: 

当然需要,不Start就不干活了

I,Robot | 园豆:9783 (大侠五级) | 2012-09-28 15:21

@狼Robot: 

好像是,多了一个{,你删掉吧

I,Robot | 园豆:9783 (大侠五级) | 2012-09-28 15:21

@狼Robot: 我试试啊,你==啊

待重逢 | 园豆:10 (初学一级) | 2012-09-28 15:22

@狼Robot: 我写的那两个方法怎么没有执行成功啊!这是怎么回事啊

待重逢 | 园豆:10 (初学一级) | 2012-09-28 15:28

@待重逢: 

那要看你那两个方法是怎么写的了,在其它地方调用能成功吗?

I,Robot | 园豆:9783 (大侠五级) | 2012-09-28 15:30

@狼Robot: 在form窗体中是可以的,

待重逢 | 园豆:10 (初学一级) | 2012-09-28 15:31

@待重逢: 

或者你调试看看是哪里出问题了

I,Robot | 园豆:9783 (大侠五级) | 2012-09-28 15:33

@狼Robot: 还在吗?服务走到连接数据库的时候就不走啦,是怎么回事啊

待重逢 | 园豆:10 (初学一级) | 2012-09-28 17:15

@待重逢: 

报什么错误呢

I,Robot | 园豆:9783 (大侠五级) | 2012-09-28 18:05
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册