本文介绍了暂停线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void PauseCheck(int x)
{
    for (int i = 0; i < (x / 10); i++)
    {
      Thread.Sleep(10);
    }
}


这是我的线程在线程方法的某些部分中暂停的方式
x是暂停的时间.
我已经在自己的线程中创建了


This is the way my Thread pauses in some parts of thread methods
x-is time for a pause .
I`ve created in my thread

System.Timers.Timer timer;
timer = new System.Timers.Timer();
  timer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimer);
  timer.Interval = BlockTime;


当我的线程进入暂停方法暂停(x = 10000)或10秒时,BlockTime大约为5秒
只有在我的线程从睡眠中唤醒后,计时器才会过去.

我的问题是下一个:Thread.Sleep()也会暂停我的计时器吗?解决该问题的方法是什么?
如何在经过时间后中断PauseCheck方法?

P.S这是一个关于用餐哲学家的程序,线程中的每个暂停都是该程序的用户在启动程序之前设置的时间,例如
思考时间10000ms
进餐时间10000ms
和blocktime((经过计时器),尝试取2个fok的ph失败.他从头开始)
我开始测试我的编,发现此错误会影响结果.

我想我知道了,但是可以从主窗体同时运行3个System.Timers.Timer吗?

我有最后一个问题:我可以定义确切的计时器已经过去了吗?


And when my thread enters pausing method for a pause (x=10000) or 10seconds ,and BlockTime is about 5 seconds
timer will elapse only after my thread wakes from sleep.

My question is next :does Thread.Sleep() pause my timer too? and what is the way to get over this problem?
How can I interrupt PauseCheck method when time is elapsed?

P.S this is a programm about dining philosophers and each pause in the thread is time which the user of this programm set before starting the programm,for example
thinking time 10000ms
eating time 10000ms
and blocktime(( timer elapses)and the ph who attempts to take 2 froks fails.and he starts from the very begining)
i began testing my prog and found this error which influences on results.

I suppose ,i got it ,but can fot exmple 3 System.Timers.Timer work at the same time from main form?

I''ve got the last question: can I define what timer exactly has elapsed?

推荐答案

public class Philosopher
{
    Thread thread;
    Timer tmr;

    public void start()
    {
        thread = new Thread(doWork);
        thread.Start();
    }

    //This method runs in a new thread
    protected void doWork()
    {
        for (int i = 0; i < 100; i++)
        {
            Console.WriteLine("Doing Work for " + i.ToString() + "th time");
        }
        Console.WriteLine("Thread Finished work");

        //initiates the timer inside the created thread. The timer is running yet another new thread
        tmr = new Timer(onTimer, thread, 0, 5000);
        tmr.Change(0, 5000);

        Console.WriteLine("Thread is going to sleep");
        doSleep(50000);

    }

    protected void onTimer(object obj)
    {
        Thread t = obj as Thread;
        t.Interrupt(); // move this below the loop if you want to wake the thread after this work finished.
        for (int i = 0; i < 100; i++)
        {
            Console.WriteLine("Doing Work on Timer elapsed event for " + i.ToString() + "th time");
        }


    }

    protected void doSleep(int x)
    {
        try
        {
            //thread sleeps
            Thread.Sleep(x);
        }
        catch (ThreadInterruptedException e)
        {
            //Thread wakes up here
            tmr.Dispose();
            Console.WriteLine("Timer Elapsed");
            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine("Doing Work on after wakeup for " + i.ToString() + "th time");

                //have to call do work again? Will be in to a endless loop
                //doWork();
            }

        }
    }
}



并称其为



and call it like

Philosopher p1 = new Philosopher();
   p1.start();
   Philosopher p2 = new Philosopher();
   p2.start();
   Philosopher p3 = new Philosopher();
   p3.start();
   Philosopher p4 = new Philosopher();
   p4.start();



它将创建4个线程,并在线程内部调用计时器.



which creates 4 threads and the timer is called inside the thread.




这篇关于暂停线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:19