【问题标题】:How to add a new thread for executing particular function in ASP.NET website?如何添加一个新线程来执行 ASP.NET 网站中的特定功能?
【发布时间】:2011-06-19 21:35:09
【问题描述】:

在我的网站中,我想为需要在后台持续运行的进程创建一个单独的线程。我想使用线程,这样我的网站性能不会降低。

我想为下面的代码创建线程。

  [WebMethod]
    public void iterativeFunction()
    {
        int count = 0;
        DateTime date1 = new DateTime(2011,5,31);
        while (System.DateTime.Compare(System.DateTime.Now,date1)<0)
        {
            downloadAndParse();

            count++;
        }
    }

我应该如何为这个函数添加一个新线程?

【问题讨论】:

    标签: .net asp.net multithreading .net-3.5


    【解决方案1】:

    类似这样的:

    public static class BackgroundHelper
    {
        private static readonly object _syncRoot = new object();
        private static readonly ManualResetEvent _event = new ManualResetEvent(false);
        private static Thread _thread;
    
        public static bool Running { get; private set; }
    
        public static void Start()
        {
            lock (_syncRoot)
            {
                if (Running)
                    return;
    
                Running = true;
    
                // Reset the event so we can use it to stop the thread.
                _event.Reset();
    
                // Star the background thread.
                _thread = new Thread(new ThreadStart(BackgroundProcess));
                _thread.Start();
            }
        }
    
        public static void Stop()
        {
            lock (_syncRoot)
            {
                if (!Running)
                    return;
    
                Running = false;
    
                // Signal the thread to stop.
                _event.Set();
    
                // Wait for the thread to have stopped.
                _thread.Join();
                _thread = null;
            }
        }
    
        private static void BackgroundProcess()
        {
            int count = 0;
            DateTime date1 = new DateTime(2011, 5, 31);
            while (System.DateTime.Compare(System.DateTime.Now, date1) < 0)
            {
                downloadAndParse();
    
                // Wait for the event to be set with a maximum of the timeout. The
                // timeout is used to pace the calls to downloadAndParse so that
                // it not goes to 100% when there is nothing to download and parse.
                bool result = _event.WaitOne(TimeSpan.FromMinutes(1));
    
                // If the event was set, we're done processing.
                if (result)
                    break;
    
                count++;
            }
        }
    
        private static void downloadAndParse()
        {
            // Your download and parse logic here.
        }
    }
    

    然后在您的网络服务中:

    [WebMethod]
    public void iterativeFunction()
    {
        BackgroundHelper.Start();
    }
    

    【讨论】:

    • @Pieter :好的,谢谢,我会在尝试您的代码后尽快回复您。感谢您的输入。 :)
    • @Pieter:因此它可以工作,但我不确定一旦浏览器关闭,while 循环是否会继续执行。我希望即使没有活动的浏览器会话它也应该继续。我该怎么做?
    • @PARTH - 请求和线程之间根本没有关系。 ASP.NET 无法检测到您已经启动了一个线程。正因为如此,它只是继续运行。如果要进行测试,您可以确定,例如做一个File.WriteAllText("C:\\Users\\YourName\\Log.txt", DateTime.Now.ToString())
    • @Pieter:感谢您的帮助。 :) 我怎样才能停止这个线程或杀死其间的线程?
    • @PARTH - 这就是BackgroundHelper.Stop() 方法的用途。您可以为此创建第二个 Web 服务。
    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2011-03-11
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    • 2012-04-19
    • 2020-10-28
    相关资源
    最近更新 更多