Help regarding monitor in Asynchronous call.

  • Thread starter Thread starter trialproduct2004
  • Start date Start date
T

trialproduct2004

Hi all
i am having C# application which is processing 5 urls asychronously
using webrequest and webresponse classes.
What i want is as soon as any url request processing is completed i
want to decrement one counter and increment another count.
How should i do this in call back.
Do i need to use monitor so that no two calls can modified that counter
at the same time or without using any sychronous object i can do this.

Because when i am using monitor on counter, some time even if i have
statment like following.

system.monitor.enter(counter);
counter--;
system.monitor.exit(counter);

counter is not getting decremented.

can some one help me.

thanks in advance.
 
It sounds as though you want your asynchronous process to fire an event back
to the controlling thread and inside the event handler use a lock statement
to synchronise access to the counters you wish to increment\decrement.

Look at events, delegates and event handlers on the MSDN site, this should
give you a start.

HTH

Ollie Riches
 
hi
thanks for you.
I think you are not getting my problem completly.
I want to modify global counter in callback function itself.
My application is working properly for nearly 15 minutes and then its
hanging.
I am not getting why this is happening.

Any help will be appreciated.

thanks in advance.
 
yes and put a lock inside the callback function so that only one thread can
execute the code at a time:


public class1
{
private int someCounter;
private int someOtherCounter;

private object myLock = new object();

private void SomeCallbackFunction()
{
lock(myLock)
{
// this will now be synchronised - only one thread
will have access to code below at any one time

someCounter++;
someOtherCounter--;
}
}
}


HTH

Ollie Riches
 
Back
Top