problem with static events

N

Nash

Hi,
I have a Timer in a static class which keeps examining a queue after
examining if it timer function finds something wrong with the object
in queue, it needs to inform an instance class about the result of the
object in the queue. For this requirement i have added an event in the
static class and i am subscribing to that event in my instance class.
everything works fine except that the event handler is called many
times.

pls tell me is this correct.

Thanks in advance.
 
H

Hans Kesting

Nash explained :
Hi,
I have a Timer in a static class which keeps examining a queue after
examining if it timer function finds something wrong with the object
in queue, it needs to inform an instance class about the result of the
object in the queue. For this requirement i have added an event in the
static class and i am subscribing to that event in my instance class.
everything works fine except that the event handler is called many
times.

pls tell me is this correct.

Thanks in advance.

You could start by showing some code.

How often does that timer-process find "something wrong"? Is it calling
that instance class once every time it "finds something wrong" or
multiple times?

How are you subscribing to that event and how are you UNsubscribing? I
think the problem might well be there: you are subscribing one class
multiple times.

Hans Kesting
 
N

Nash

Hans Kesting submitted this idea :


- Show quoted text -

Thanks for the reply.

I forget to tell you people that i am new to c#.

The timer will be invoked every 3 minutes. Whenver the timer is
invoked it will fire the event. in the instance class construcotr i am
subscribing to the event

CActionManager()
{
QueueManager.OnSendFailure += new QueueManager.SendFailure
(SendFailed);
}

i am not unsubscribing. i even tried that in SendFailed method but it
is not working.
 
N

Nash

Thanks for the reply.

I forget to tell you people that i am new to c#.

The timer will be invoked every 3 minutes. Whenver the timer is
invoked it will fire the event. in the instance class construcotr i am
subscribing to the event

CActionManager()
{
 QueueManager.OnSendFailure += new QueueManager.SendFailure
(SendFailed);

}

i am not unsubscribing. i even tried that in SendFailed method but it
is not working.

am i doing some thing worng.. should i just create an instance of the
CActionManager class and call the function directly in the timer
function of static class? is my understanding of events wrong?
 
H

Hans Kesting

Nash used his keyboard to write :
Thanks for the reply.

I forget to tell you people that i am new to c#.

The timer will be invoked every 3 minutes. Whenver the timer is
invoked it will fire the event. in the instance class construcotr i am
subscribing to the event

CActionManager()
{
QueueManager.OnSendFailure += new QueueManager.SendFailure
(SendFailed);
}

i am not unsubscribing. i even tried that in SendFailed method but it
is not working.

Maybe you need to implement IDisposable in that CActionManager class,
and unsubscribe from that event there:

public void Dispose()
{
QueueManager.OnSendFailure -= new QueueManager.SendFailure
(SendFailed);
}

(and make sure that Dispose() gets called, either by calling it
yourself or by using a "using" statement)

Why did that handler get invoked "many" times?
- your timer process invoked it multiple times
- per invoke of the OnSendFailure event (by the timer) your SendFailed
gets called multiple times

Hans Kesting
 

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