Help regarding monitor in Asynchronous call.

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.
 
O

Ollie Riches

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
 
T

trialproduct2004

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.
 
O

Ollie Riches

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top