您的位置:程序门 -> .net技术 -> c#



windows服务,如何单线程运行?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


windows服务,如何单线程运行?[已结贴,结贴人:hsb19820706]
发表于:2007-01-09 11:04:04 楼主
我在windows服务中调用system.timers.timer每隔一段时间执行一段代码。现在的问题是在执行这段代码的时候windows服务还是会触发timer的elapsed事件,如何让在执行elapsed事件里的代码的时候不让外面的线程再运行?
发表于:2007-01-09 11:08:141楼 得分:0
暂停或终止线程?
发表于:2007-01-09 11:11:202楼 得分:0
让外面的线程与触发timer的elapsed事件同步就行了
发表于:2007-01-09 11:13:593楼 得分:0
好辦,在elapsed中寫
timer.stop();
//your   work
timer.start();
发表于:2007-01-09 11:15:334楼 得分:0
thread   mythread   =   new   thread(new   threadstart(threadproc));

mythread.start();

mythread.join();

我在elapsed事件中这么写,不知道可不可以把主进程阻止掉?
发表于:2007-01-09 11:36:285楼 得分:0
没太明白,你是在timer的elapsed事件中启动一个线程?

发表于:2007-01-09 11:37:276楼 得分:20
不行,这样只是在线程结束时完成此次的elapsed事件,并不能使elapsed事件不再次被触发,elapsed事件是到时间就触发,并不是等到上一次的事件完成后再触发,每次被触发都是在一个新的线程中执行,与上一次的触发之间没有任何关系。
发表于:2007-01-09 11:37:307楼 得分:30
你是想等启动的线程结束后,再启动另外一个线程对吗?

即线程是依次进行的,对吗?
发表于:2007-01-09 11:39:558楼 得分:0
try..

可以这样,用个互斥量..
private   mutex   mx=new   mutext();


timer的elapsed事件下:
mx.waitone();
thread   mythread   =   new   thread(new   threadstart(threadproc));
mythread.start();
mythread.join();
mx.releasemutex();


发表于:2007-01-09 11:43:179楼 得分:0
当然用timer.stop()和timer.start()也是可以的..

只不过效果有点不一样..
发表于:2007-01-09 12:37:1310楼 得分:0
至于有什么不一样,写两个例子,你就清楚了..
发表于:2007-01-09 12:39:4411楼 得分:0
使用timer.stop和start方法:

public   static   system.timers.timer   timer   =   new   system.timers.timer();
                public   static   mutex   mx   =   new   mutex();

                static   void   main(string[]   args)
                {
                        timer.elapsed   +=   new   elapsedeventhandler(timer_elapsed);
                        timer.interval   =   3000;
                        timer.start();
                        console.readline();
                }

                static   void   timer_elapsed(object   sender,   elapsedeventargs   e)
                {
                        timer.stop();
                        //mx.waitone();
                        debug.writeline(system.datetime.now.tostring()   +   ": "   +   system.datetime.now.millisecond);
                        system.threading.thread.sleep(1000);
                        //mx.releasemutex();
                        timer.start();
                }

输出如下:
2007-1-9   12:37:35:515
2007-1-9   12:37:40:390
2007-1-9   12:37:44:406
2007-1-9   12:37:48:406
2007-1-9   12:37:52:421
2007-1-9   12:37:56:437
2007-1-9   12:38:00:437
2007-1-9   12:38:04:468
2007-1-9   12:38:08:468
2007-1-9   12:38:12:484
2007-1-9   12:38:16:500
2007-1-9   12:38:20:500
2007-1-9   12:38:24:500
2007-1-9   12:38:28:500
2007-1-9   12:38:32:500
2007-1-9   12:38:36:562
2007-1-9   12:38:40:750
发表于:2007-01-09 12:40:4512楼 得分:0
使用mutext:

public   static   system.timers.timer   timer   =   new   system.timers.timer();
                public   static   mutex   mx   =   new   mutex();

                static   void   main(string[]   args)
                {
                        timer.elapsed   +=   new   elapsedeventhandler(timer_elapsed);
                        timer.interval   =   3000;
                        timer.start();
                        console.readline();
                }

                static   void   timer_elapsed(object   sender,   elapsedeventargs   e)
                {
                        //timer.stop();
                        mx.waitone();
                        debug.writeline(system.datetime.now.tostring()   +   ": "   +   system.datetime.now.millisecond);
                        system.threading.thread.sleep(1000);
                        mx.releasemutex();
                        //timer.start();
                }

输出:
2007-1-9   12:39:22:375
2007-1-9   12:39:25:468
2007-1-9   12:39:28:531
2007-1-9   12:39:31:531
2007-1-9   12:39:34:546
2007-1-9   12:39:37:546
2007-1-9   12:39:40:546
2007-1-9   12:39:43:546
2007-1-9   12:39:46:546
2007-1-9   12:39:49:546
2007-1-9   12:39:52:546
2007-1-9   12:39:55:546
2007-1-9   12:39:58:546
2007-1-9   12:40:01:546
2007-1-9   12:40:04:546
2007-1-9   12:40:07:562
2007-1-9   12:40:10:562
发表于:2007-01-09 12:41:0213楼 得分:0
相信楼主能够看明白..
发表于:2007-01-09 13:08:5314楼 得分:0
现在的人噢~!问题都不会问哦~!其实大家都想帮你~!
可你语言描述的不好哦~!·!
别个不好帮你哦。。~!


快速检索

最新资讯
热门点击