Click here to Skip to main content
15,907,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a service code something like:
C#
[ServiceContract]
public interface IService1
{
    [OperationContract]
    void MyOperation1();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession,ConcurrencyMode=ConcurrencyMode.Multiple)]
public class service1 : IService1
{
    public service1()
    {
        Console.WriteLine("creating instance");
    }
    public void MyOperation1()
    {
        Console.WriteLine("starting..");
        Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId);
        Thread.Sleep(1000);
        Console.WriteLine("Ending..");
    }
}


Client code :

C#
static void Main(string[] args)
       {
           Thread[] t = new Thread[10];
           NetTcpBinding myBinding = new NetTcpBinding();
           EndpointAddress myEndpoint = new EndpointAddress("net.tcp://localhost:8000/MyService");
           ChannelFactory<IService1> myChannelFactory = new ChannelFactory<IService1>(myBinding, myEndpoint);
           IService1 instance = myChannelFactory.CreateChannel();
           for (int i = 0; i < 10; i++)
           {
               t[i] = new Thread(new ThreadStart(delegate()
               {
                   instance.MyOperation1();
               }));
           }
           for (int i = 0; i < 10; i++)
           {
               t[i].Start();
           }
       }


I am getting output like this(calls being processes one by one):

creating instance
starting..
3
Ending..
starting..
3
Ending..
starting..
12
Ending..
starting..
3
Ending..
starting..
12
Ending..
starting..
3
Ending..
starting..
12
Ending..
starting..
3
Ending..
starting..
12
Ending..
starting..
3
Ending..

Why are the calls not processes parallely by service?

I want the result to be something like(all threads should start parallely):

creating instance
starting..
starting..
starting..
starting..
3
Ending..
4
6
Ending..
Posted
Updated 6-Aug-13 7:28am
v2
Comments
debdeep Bhattacharyya 6-Aug-13 14:15pm    
I want the wcf to process calls concurrently ... not one by one ..

1 solution

Believe or not, but in your case the thread actually work concurrently. Only you don't get a chance to notice that, just because each thread executes its body method faster then the thread switching activity can come into play.

That said, your use of the threads totally defeats the purpose of multithreading. Having such short thread functions hardly makes any practical sense. You only waste some OS and CPU resources, because creation and termination of each thread impose considerable overhead, without getting any benefits of threading.

—SA
 
Share this answer
 
v2
Comments
debdeep Bhattacharyya 6-Aug-13 14:01pm    
My question is the output received from WCF .. how do i get the wcf to act concurrently?
Sergey Alexandrovich Kryukov 6-Aug-13 14:18pm    
There is no such problem. It will work concurrently without any extra efforts. The problem is not that you are doing anything wrong.

You are just using overly simplified code sample, which does not take benefit of threading. If you do the same thing having really time consuming threads, chances are, you will get right functionality.
—SA
debdeep Bhattacharyya 6-Aug-13 14:24pm    
You can increase the time consumption in the thread.sleep part of service and you will see that the service still reacts one by one although the client has sent the calls parallely...
Sergey Alexandrovich Kryukov 6-Aug-13 14:30pm    
If you advise is just for learning purposes, you are right. For any practical purposes other than that, it would be a really bad idea...
I've written it just to avoid confusing OP.
—SA
debdeep Bhattacharyya 6-Aug-13 14:36pm    
I understand your concern here .. however i will need some help to get the service act concurrently .. that is my main goal

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900