Manually Raising An Event

Posted on 2002-06-19
Last Modified: 2008-02-20
How do you manually raise an event.  For example I am trying to manually raise a timer event.  I have tried the following code thinking it would work, but unfortuantly it does not

this.checkSite( this, new System.Timers.ElapsedEventArgs() )

Any thoughts on how I can trigger the timer event manually?

Question by:larockd
  • 3
  • 3
  • 2
  • +1
LVL 22

Accepted Solution

CJ_S earned 50 total points
ID: 7094907
You raise an event by calling the event name with the correct parameters. The timer event for example:

Timer1.Tick(..., ....)

now the first parameter is the sender object and the second is the evnt argument.

this.timer1_Tick(this, new System.EventArgs());


Expert Comment

ID: 7095079
CJ_S is right. You just call the event name to raise the event.

1. Before you do this, you should check that the event has subscribes by comparing it to null:

if ( this.MyEvent != null )
    this.MyEvent (this, EventArgs.Empty);

2. It would be neater to use the static member of EventArgs (class Empty) instead of initializing an instance manually (using new).

3. Sometimes there is specific public method which raises the event. For example, the Button class has a method called Click that fires the event.

Good luck.

Saar Carmi
Israel .Net Developer

Author Comment

ID: 7097171
Saar / CJ

I had a question in regards to what you meant by checking to see that the even has subscribes?  What exactly does that mean?

Also, when you say it would be neater to use the static method of EventArgs instead of creating a new instance.  Did you mean neater in that it would be more efficient?  Or just cleaner style of coding?

Also, the code everyonce suggested has a caveat that I am not sure how to deal with previously I had this

       strLastCheckStatus = "Started - Waiting" ;
       // Update Listview
       myListView.Items[ iListViewIndex ].SubItems[9].Text = strLastCheckStatus ;

       if ( DateTime.Now >= dtNextCheck )
          //Manually Raise The Check Site
           Console.WriteLine("yup better check site") ;

Now when this ran before I raised the event manually the listview properly updated with the above status of "Waiting" Once I added the following to raise the event after the manually raise event comment

    this.checkSite( this, new System.EventArgs() ) ;

The ListView No Longer Updates with The "Started - Waiting" status...

Anythoughts on why?  Because I am asking another question in a question I will bump up the points.


Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Expert Comment

ID: 7097454
Each event has it's subscribers - those object instances that are doing to be notifed when the event is fired.

Before anyone subscribe to the event (using += operator in C#), the event value is Null and if you try to fire it - you'll get NullReferenceException.

The static method just calls the new operator - it's only a matter of cleaner code.

For your new problem, try to call Application.DoEvents
before you raise the event.

Good luck

Author Comment

ID: 7097627
I am unable to find the static method for the System.EventArg() class.  The only static method is empty

What am I missing?  


Expert Comment

ID: 7098252
I meant to the Empty property (it's not method but a property - my mistake)

This property value is just an instance of the EventArgs.


Author Comment

ID: 7099328
Thanks for the help, I am having the moderators split the points between both saar2 and CJ.  Thx again.


Expert Comment

ID: 7099789
Point total has been reduced to 50 points for a two way split.


I shall accept one here, and create another question for saar2, in this same Topic Area.

CS Admin @ EE


Expert Comment

ID: 7099791
saar2, Ihave left your points for the split here: 

Thanks to all,

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Greetings C# program 17 125
HTML - Color not displaying correctly in EMAIL. 6 49
Object Oriented Programming, C#, referencing, scoping. 13 69
IEnumerable<T> to a List<T> 8 40
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question ( here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question