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

问题描述

您好Code Code编码器



我有一个WCF服务,由Silverlight应用程序使用,出于测试目的,处理回调和 CallbackChannel 一个小型测试聊天应用程序的客户端列表。



我在我正在处理的主应用程序中遇到了一些问题强大的谷歌告诉我,我需要实现多线程



好​​的,它是多线程的!让我们测试吧。



在我的服务发生一些变化后,我有一个稳定的解决方案,可以在我的开发环境中运行,如下所示:



Hi fellow Code Project coders

I have a WCF service, consumed by a Silverlight application that, for testing purposes, handles callbacks and a CallbackChannel client list for a small testing chat application.

I was running into some problems in the main application I’m working on and mighty Google told me that I needed to implement multithreading.

Ok, multithreading it is! Let’s test it.

After some changes on my service I’ve got a stable solution working on my developing environment that goes like this:

[OperationContract]
public void Publish(string message)
{
    lock (clients)
    {
        foreach (IDBNotificationCallbackContract channel in clients)
        {
            Thread t = new Thread(new ParameterizedThreadStart(this.notifyClient));
            t.Start(new Callback_info { CallBackContract = channel, Message = DateTime.Now.ToString() + "\n" + message });
        }
    }
}

void notifyClient(Object n)
{
    Callback_info notif = (Callback_info)n;
    try
    {
        if ((notif.CallBackContract as ICommunicationObject).State == CommunicationState.Opened)
            notif.CallBackContract.Notify(notif.Message);
    }
    catch
    {
    }
}





虽然线程初始化是在 foreach 循环中进行的,但我线程完成后我是否真的得到了处置,或者我是否遇到内存和/或处理器加载问题,如果这样的话就要生产了?



如果是这样,我该如何处置线程?在Thread类中没有Dispose方法,也没有一个Completed事件可以异步挂接。



事情就是在我的开发环境中我尝试了一个小的压力测试并打开运行测试应用程序的40个浏览器窗口。



当按下按钮开始轰炸消息时,机器在此期间以100%负载疯狂20分钟而且我不确定负载是来自服务还是40个打开的浏览器窗口。



对我的这个疑问表示赞赏。



Although the thread initialization is made inside the foreach cycle, I wondered if it really gets disposed after the thread is completed or am I running into memory and/or processor load troubles if this ever goes to production like this?

If so, how do I dispose the thread? There is no Dispose method in the Thread class neither there is a Completed event to hook up to asynchronously.

The thing is that on my development environment I tried a small stress test and opened 40 browser windows running the test application.

When pressed the button to start bombing messages the machine went nuts for 20 minutes with a 100% load during that time and I’m not sure that the load was from the service or the 40 opened browser windows.

Appreciate any light on this doubt of mine.

推荐答案

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession,
                 ConcurrencyMode = ConcurrencyMode.Multiple)]


这篇关于WCF服务mutithread问题 - 如何处理线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:17