Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Manually Raising An Event

Posted on 2002-06-19
9
Medium Priority
?
893 Views
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?

thanks
dl
0
Comment
Question by:larockd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 22

Accepted Solution

by:
CJ_S earned 200 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());

CJ
0
 
LVL 4

Expert Comment

by:saar2
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
saar@bigfoot.com
0
 

Author Comment

by:larockd
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.

Darrell


0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 4

Expert Comment

by:saar2
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
0
 

Author Comment

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

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemeventargsclassctortopic.asp

What am I missing?  

dl
0
 
LVL 4

Expert Comment

by:saar2
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.

Saar
0
 

Author Comment

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

dl
0
 

Expert Comment

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

50=CJ_S
50=saar2

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

Regards,
ComTech
CS Admin @ EE

0
 

Expert Comment

by:ComTech
ID: 7099791
saar2, Ihave left your points for the split here: http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=c_sharp&qid=20314855 

Thanks to all,
CT
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

636 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