.NET Web服务和放大器; BackgroundWorker的线程 [英] .NET Web Service & BackgroundWorker threads

查看:105
本文介绍了.NET Web服务和放大器; BackgroundWorker的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一些异步的东西,在一个Web服务方法。让说我有以下API调用: http://www.mysite.com/api.asmx

I'm trying to do some async stuff in a webservice method. Let say i have the following API call: http://www.mysite.com/api.asmx

和该方法被调用的的GetProducts()

我这样的GetProducts方法,我做一些东西(如从数据库中获得的数据),那么就在我返回的结果,我想要做一些异步的东西(如:给我发送电子邮件)。

I this GetProducts methods, i do some stuff (eg. get data from database) then just before i return the result, i want to do some async stuff (eg. send me an email).

所以这就是我所做的。

[WebMethod(Description = "Bal blah blah.")]
public IList<Product> GetProducts()
{
    // Blah blah blah ..
    // Get data from DB .. hi DB!
    // var myData = .......
    // Moar clbuttic blahs :)  (yes, google for clbuttic if u don't know what that is)

    // Ok .. now send me an email for no particular reason, but to prove that async stuff works.
    var myObject = new MyObject();
    myObject.SendDataAsync();

    // Ok, now return the result.
    return myData;
    }
}

public class TrackingCode
{
    public void SendDataAsync()
    {
        var backgroundWorker = new BackgroundWorker();
        backgroundWorker.DoWork += BackgroundWorker_DoWork;
        backgroundWorker.RunWorkerAsync();
        //System.Threading.Thread.Sleep(1000 * 20);
    }

    private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        SendEmail();
    }
}

现在,当我运行该code中的邮件永远不会发送。如果我取消了Thread.sleep代码..然后发送电子邮件。

Now, when i run this code the email is never sent. If i uncomment out the Thread.Sleep .. then the email is sent.

所以......那为什么后台工作线程被拆除?它是依赖于父线程呢?这是错误的方式,我应该做的背景或分叉线程,在asp.net web应用程序?

So ... why is it that the background worker thread is torn down? is it dependant on the parent thread? Is this the wrong way i should be doing background or forked threading, in asp.net web apps?

欢呼;)

推荐答案

的BackgroundWorker 当你需要同步回(例如)UI *线程,例如,是非常有用的亲和的原因。在这种情况下,它似乎只是使用线程池将绰绰有余(和简单得多)。如果你有大容量,那么生产者/消费者队列可能能够更好地节流(这样你就不会在线程淹没) - 但我怀疑线程池将被罚款这里..

BackgroundWorker is useful when you need to synchronize back to (for example) a UI* thread, eg for affinity reasons. In this case, it would seem that simply using ThreadPool would be more than adequate (and much simpler). If you have high volumes, then a producer/consumer queue may allow better throttling (so you don't drown in threads) - but I suspect ThreadPool will be fine here...

public void SendDataAsync()
{
    ThreadPool.QueueUserWorkItem(delegate
    {
        SendEmail();
    });
}

此外 - 我不太清楚你想睡觉达到什么样的?这只会占用一个线程(不使用CPU,但无论哪种做得没有好)。详细地谈一谈?它的看起来的就像你暂停您的实际网页(即睡眠发生在Web页主题,而不是邮件线程)。什么是你想在这里做什么?

Also - I'm not quite sure what you want to achieve by sleeping? This will just tie up a thread (not using CPU, but doing no good either). Care to elaborate? It looks like you are pausing your actual web page (i.e. the Sleep happens on the web-page thread, not the e-mail thread). What are you trying to do here?

* =实际上,它会使用任何同步上下文是代替

*=actually, it will use whatever sync-context is in place

这篇关于.NET Web服务和放大器; BackgroundWorker的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆