Click here to Skip to main content
15,896,518 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
In Image whole scenario is explained. that the multiple number of clients calls a function name with call(), to service1. and each client call function simultaneously. so service1 identity that function call from which client, and each client calls a function (call()) at a time more than 10times on miliseconds difference, and my requirement is, at what order client call the function, in the same order call function should execute, and parallel this should work for all clients.

Image link. click here

current Implemetation working:

client 1
Client2

* * *

client N

client1 calls function 10 times at 10miliseconds difference.

client1.call(1);

client1.call(2);

client1.call(3);

client1.call(4);

client1.call(5);

client1.call(6);

client1.call(7);

client1.call(8);

client1.call(9);

client1.call(10);

All clients simultaneously call function to service 1

Service1 Call();

And then service1 calls the call() function whose implementation is in service2.

Service 2 (Wcf service)

public int Call(int number)
{
Console.WriteLine("The number in parameter pass :" + Number);
return number;

}
Output Comes

The number in parameter pass :1

The number in parameter pass :2

The number in parameter pass :5

The number in parameter pass :6

The number in parameter pass :7

The number in parameter pass :8

The number in parameter pass :3

The number in parameter pass :4

The number in parameter pass :10

The number in parameter pass :9
In output order disturb, as required output is

The number in parameter pass :1

The number in parameter pass :2

The number in parameter pass :3

The number in parameter pass :4

The number in parameter pass :5

The number in parameter pass :6

The number in parameter pass :7

The number in parameter pass :8

The number in parameter pass :9

The number in parameter pass :10
This is happening due to call() function simultaneously calls 10times and call(3) and call(4) takes time to insert data in database and call(5), call(6) complete first.


In Nutshell:

How do i make a queues for each client, so that order does not disturb for each client.

I tried like this ThreadPool.QueueUserWorkItem(state => Call(1)), but QueueUserWorkItem will return only Boolean vlaue, but i need to get in return specific custom list. which is return by call() function.
Posted
Updated 12-May-15 20:55pm
v2

1 solution

The hreadPool.QueueUserWorkItem(state => Call(1)) will queue the items in order. This does guarantee that the will start in that order but it does not guarantee that they will complete in that order.

How about using a standard procedural call for call 1-n and throw that procedure for each client into the threadpool.

Remember that Threadpool only needs to know what method to run
C#
public void RunCallsInSequence(Client client){

//Generate calls
//call(1)
//call(2)
//...
//call(n)

}

public void RunClientsInThreads(Client[] clients){

 foreach(Client client in Clients)
 {
  ThreadPool.QueueUserWorkItem(state => RunCallsInSequence(client))
 }
}


Or you can even use the lamda delegate to manage your calls in order:
C#
public void RunClientsInThreads(Client[] clients){

 foreach(Client client in Clients)
 {
  ThreadPool.QueueUserWorkItem(state => {
   //Generate calls
   //call(1)
   //call(2)
   //...
   //call(n)
  })
 }
}


I don't know how you generate calls so I can't be more specific

Please let me know if this approach doesn't suit your needs, and why, and I'll refine my solution

Thanks
Andy ^_^
 
Share this answer
 

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