WordPress database error: [INSERT, UPDATE command denied to user '51213-2'@'10.10.20.76' for table 'wp_options']INSERT INTO `wp_options` (`option_name`, `option_value`, `autoload`) VALUES ('_transient_doing_cron', '1715819942.9731190204620361328125', 'yes') ON DUPLICATE KEY UPDATE `option_name` = VALUES(`option_name`), `option_value` = VALUES(`option_value`), `autoload` = VALUES(`autoload`)
<\/a>Memory leaks were, are and always will be a concern in an application. One of it’s classical origin is unsubscribed events handler. <\/p>\n The weak event pattern is here to the rescue but it is quite tedious to implement.<\/p>\n In this post we’ll see how the WPF teams ease up our life when using the WeakEventManager class.<\/strong> Prior to WPF 4.5 you had too create a weak event manger for every event you want to subscribe too. <\/p>\n Now it’s over and you can use a generic version of the WeakEventManager class. <\/strong><\/p>\n It takes as a generic parameters the type of the event’s source and the type of the dealed event arguments. It also exposes two statics methods on it:<\/p>\n It is done using reflection so you can have a little performance overhead using this object.<\/p>\n Prior to WPF 4.5, every subscriber to a weak-event must implements the IWeakEventListener. This is a very simple interface with this declaration: Even if it’s easy and fast to implement, it is quite tedious. Hopefully, implementing it is no more needed and you simply have to pass on a delegate when subscribing !<\/strong>. <\/p>\n Let’s said you have an application with a main window and sometimes you show up child ones. This children subscribe to the Activated event of the main window to know when the application is showed up. By using traditional event subscription, you could create a memory leak if you do not register from it. In the other hand, if you subscribe to this event using a WeakEventManager, you will never heard of it!<\/p>\n Here is an example of the code to use: public LeakingWindow() \/\/Traditional event subscription: memory leak ! void MainWindow_Activated(object sender, EventArgs e) To test this feature, I created a little demo app which create leaking window and dump the memory usage every 200ms. You can find this application on my dropbox folder<\/a> (do you want to register and give me more space ? use this link !!<\/a>)
\nThis post is a part of a serie about the new features of WPF 4.5<\/a>.
\n<\/p>\nGeneric Weak Event Manager<\/h3>\n
\n[csharp]
\n\/\/ Type parameters:
\n\/\/ TEventSource:
\n\/\/ The type that raises the event.
\n\/\/
\n\/\/ TEventArgs:
\n\/\/ The type that holds the event data.
\npublic class WeakEventManager<TEventSource, TEventArgs> :
\n WeakEventManager where TEventArgs : EventArgs
\n[\/csharp]<\/p>\n\n
No more interface for the subscriber<\/h3>\n
\n[csharp]public bool ReceiveWeakEvent(Type managerType, object sender, EventArgs e)[\/csharp]<\/p>\nAn example<\/h3>\n
\n[csharp]
\n\/\/Create 10 Mo to be more visible in the process explorer
\npublic byte[] data = new byte[10 * 1024 * 1024];<\/p>\n
\n{
\n InitializeComponent();
\n WeakEventManager<Window, EventArgs>
\n .AddHandler(App.Current.MainWindow, "Activated", MainWindow_Activated);<\/p>\n
\n App.Current.MainWindow.Activated += MainWindow_Activated;
\n}<\/p>\n
\n{
\n \/\/Do something here
\n}
\n[\/csharp]<\/p>\n
\n<\/a><\/p>\n