在Windows服务中使用Threading异步执行C#方法

时间:2012-01-26 14:39:29

标签: c# multithreading windows-services

我有一个每隔10秒运行一次的Windows服务来执行read方法。 Read方法使用构造函数中提供的连接URL连接到远程服务器。 如果远程服务器无法响应,则抛出错误并继续捕获。我们如何让线程重新开始?

class PMTicketsService
{
    private Timer _serviceTimer;
    private TimerCallback _timerDelegate;

    public PMTicketsService()
    {   
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        // Set the method to execute when the timer executes.
        _timerDelegate = new TimerCallback(Receive);

        // Create timer and attach our method delegate to it
        _serviceTimer = new Timer(_timerDelegate, null, 1000, 10000);
    }

    public void Receive(object state)
    {
        ABC abc = new ABC(Url);
        ABC abc1 = new ABC(Url1);

        /* Create the thread object, passing in the abc.Read() method
        via a ThreadStart delegate. This does not start the thread. */
        Thread oThread = new Thread(new ThreadStart(abc.Read());
        Thread oThread1 = new Thread(new ThreadStart(abc1.Read());

        // Start the thread
        oThread.Start();
        oThread1.Start();

        oThread.Join();
        oThread1.Join();
    }
}

class ABC
{
    public string strUrl;

    public ABC(string url)
    {
        strUrl = url;
    }

    public void Read()
    {
        try
        {
            // Code to use the connectionurl to access a remote server
        }
        catch (Exception ex)
        {
            // If the connection url fails to respond, how do we make thread start again?
        }
    }
}

2 个答案:

答案 0 :(得分:1)

你为什么要开始另一个线程?启动/停止线程是一项昂贵的操作,只需保持现有线程打开并不断尝试连接(可能在两者之间进行休眠),您就会好得多。你已经有了try / catch来防止线程崩溃。只需将try / catch包装好一段时间(!done),并在成功连接后将done设置为true。

您可能还想添加一些代码,这样如果您连续X次连接(可能是5?),那么您将停止尝试,或者增加连接尝试之间的超时。

答案 1 :(得分:1)

将来,您应该提交实际编译的示例代码。我把你所拥有的东西清理干净,取出了不必要的计时器,并以一种能够满足你需要的方式构建它。在下面的代码中,您的Read方法将继续运行,直到您将done设置为true

    protected override void OnStart(string[] args)
    {
        try
        {
            ABC abc = new ABC("www.abc.com");

            // Create the thread object, passing in the abc.Read() method
            Thread oThread = new Thread(new ThreadStart(abc.Read));

            // Start the thread
            oThread.Start();
        }
        catch (Exception)
        {

        }
    }

    public class ABC
    {
        string strUrl = "";

        public ABC(string url)
        {
            strUrl = url;
        }

        public void Read()
        {
            bool done = false;

            while (!done)
            {
                try
                {
                    //Code to use the connectionurl to access a remote server
                    //Use strUrl in here
                }
                catch (Exception)
                {
                    //Wait 10 seconds before trying again
                    Thread.Sleep(10000);
                }

                //On success, set done to true
                done = true;
            }
        }
    }