Real-World Learning

Events and event handling in C#


Stand up comedian Nishant Tanwar talks about cricket. for show queries: [email protected] or call / / Nishant Akshar liked this ghazal since his childhood. In fact, one of his real uncles used to sing it with harmonium. Here he is presenting the cover version in his own style.

The Program

The subject was related to performance and troubleshooting. I decided to write a summary of the presentation in two blog posts. In this post I will recap the performance thoughts. In addition it should provide a solution for the normalized data model where many tables are linked using a foreign key relation based on record IDs.

The source is now first loaded into a staging table without any business logic like events for validating data or add rules in insert methods. Then from the staging table the data is copied to the target table s where also business logic will be used. In an older version of Microsoft Dynamics AX, I had to deal with importing over fixed assets with each 4 value models, acquisition value and cumulated depreciation.

So in total there were over value models and almost journal lines to be posted for the opening balance. Usually within one flat Excel file with a certain template I used a script to read the Excel lines, create assets, post acquisition values, depreciation values and then correct value models for the number of remaining depreciation periods.

Then we did also create a staging table which contained the Excel columns. From within AX we could process the business logic using the batch framework which solved the problem and the transactions could be converted within the given timeframe.

So this architecture is a good implementation. Microsoft did a real amazing good job here. The next picture shows the flow in general. The setup of processing groups in AX is the base for SSIS packages which will pick up the source records and will put it with our without some basic transformations in the staging table. SSIS is very efficient and really a winner on performance for this type of tasks. However Microsoft seems to have invested a lot in performance for the Data Import Export Framework, you might encounter some performance issues.

You also need to be aware of some standard behavior and features to get the best performance on executing the import jobs. If you make changes on your environment based on this blog post, make sure you test it thoroughly. Changing your environment is at your own risk.

There can be more possibilities of improving performance. Some settings can be related to SQL server tuning, but also the hardware you are using. Also there are two settings in the DIXF parameters which could cause performance problems when you change it to the wrong settings. One field is Data access mode. This field is a setting for SSIS.

When it has NOT the fast load option, the records are committed one by one. So use the fast load setting. When a post man has 10 letters for the same address, he can insert them one by one or all at once. The last option is comparable with the fast load option. The Maximum insert commit size field is used to tell how many records will be inserted during before a commit command will be executed.

Then 2 inserts are needed to put in the 10 letters. The default value is which actually means there is no limitation and al records will be committed at once. When you have e. My next post will be related to troubleshooting operational issues when using Data Import Export Framework.

So watch the next post coming…. Hi Andre, In staging tips section of this post I have a similar staging table. And when I am trying to get staging data only records with positive line number values are getting inserted to staging table. I have few records where line number is negative. What can be the reason for it. Your question is not related to the topic of this blog post. This post was about performance.

The delegate defines the parameters sent to the event handlers. Thus any class that wants to handle this event must have a handler method which has the same return type and argument list as this delegate. Here as you can see, the first parameter is an object. In real-world cases event handlers are normally passed a reference to the sending object. I am not doing that in this program.

I am simply passing a new object to the event handler. Normally you can pass a this reference. The second parameter is a System.

EventArgs is the base class for encapsulating event related data. We use it to send information regarding the event to its handler. As you can see, it has a public read-only member which is used to store our generated number that is divisible by 7. Normally you should use properties but for the sake of simplicity I am using a public member variable. As you can see, it has a function ShowOnScreen that matches the delegate type we defined on top.

You can see how we use the passed DivBySevenEventArgs object to print out the number that is divisible by seven. Now, let's examine our Main containing class. We first declare the event as follows: An event is declared like a delegate type variable, except that the keyword event precedes the event declaration. EventSeven will be null if no client has hooked up a delegate to the event.

We need to check if it is null unless you want to see an exception raised. If it's not null we invoke the event, passing a dummy object [I have explained above why I passed a dummy] and the passed DivBySevenEventArgs object.

And all clients get notified. We use the for loop to iterate from 0 to 98, and in each case we check for divisibility by 7. If we find that the number is divisible by 7 we create a DivBySevenEventArgs object passing the culprit number to the constructor. We first create a DivBySevenListener object. Once we've done all that we call GenNumbers. Thus we've set everything up nice and proper.

GenNumbers will do its job like a faithful puppy by generating numbers from 0 to 98 just as we expected it to. Every time it generates a number divisible by 7 it will raise the event. Well you have just seen how you can create events and event handlers. You must remember that events can be invoked only from the class that declared them.

This causes problems with regard to inheritance. Thus if you have a class with an event you'd better make your OnWhateverEvent function protected so that the derived classed can call it.

Better still, make it virtual too, so they can override it in their class. Articles Quick Answers Messages.

Events and event handling in C. Nish Nishant , 16 Oct Please Sign up or sign in to vote. An introduction to using events and event handlers in C. Introduction An event is a mechanism via which a class can notify its clients when something happens. The Program I include the full program below.

WriteLine " divisible by seven event raised!!! Now, we define the EventArgs derived class as follows: Now we define our listener class which is the class that needs to be notified of the event. Now let's take a look at the function that invokes the event and thus notifies all clients. Let's look at the function GenNumbers now: Conclusion Well you have just seen how you can create events and event handlers. He has over 17 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager.

Nish is an industry acknowledged expert in the Microsoft technology stack. In addition, he has over published technology articles on CodeProject. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

If you are interested in hiring Nish as a consultant, you can reach him via his google email id voidnish. Core - An Alternative to Entity Framework. Event handling in C. To bubble or tunnel basic WPF events. Dejan Dimeski Oct 9: Member 6-May Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy.

Mico Perez 7-Aug Mark McClure Apr 9: Paul French Apr Albert Pascual 6-Apr Andrew Phillips May Nish Nishant May Article Alternatives Comments 28 Add your own alternative version Tagged as. Events and event handling in C Nish Nishant , 16 Oct Pro Building Reactive Apps. Events and event handling in C Aug 2: Thanks for posting a very nice article.

My vote of 3 chait Dec 1: Nice one but not for the beginners. Excellent article Dejan Dimeski Oct 9: Exactly mentioning what prerequisites and knowledge you need to understand the material.

It would we very good if there is also "in real world" example. Not quite as simple as I wished for a first example Timok1A 5-Aug Hello, This is a very good article with lots of explanations, but I feel it is is still a little bit more complex that it would need to as a first event example in C. I have written a simpler version here: Simplistic event example in C I suggest to come back here once you have tried the most simple version.

Thank you 8-Mar Nish, I got it running in Silverlight with Blend 4.


The last option is comparable with the fast load option. However Microsoft seems to have invested a lot in performance for the Data Import Export Framework, you might encounter some performance issues.

Closed On:

Time wait for none but wait for

Copyright © 2015

Powered By