Quantcast
Channel: HTML Renderer
Viewing all articles
Browse latest Browse all 693

Commented Unassigned: Event overrides + race conditions [9030]

$
0
0
Hello,

Typically, if I inherit from a component, I will not bind to that components events unless I must. Instead, I'll override the relative method. For example, assuming I wanted to do some repainting when the `Text` property changed. I wouldn't hook into the `TextChanged` event, I'd simply override the `OnTextChanged` method.

I have created a component that inherits from `HtmlPanel`, and I want to override the link click handling. Unfortunately I found you don't expose the custom `On*` methods you've created (at least for `OnLinkClicked` and `OnImageLoaded`, I haven't dug too deeply into the code yet). In addition I notice that when you do raise events, you raise them in a fashion that exposes you (however unlikely) to a [race condition](http://msdn.microsoft.com/en-us/library/w369ty8x.aspx).

In the fork I've created, I changed your original `HtmlPanel.OnLinkClicked` method to

```
protected virtual void OnLinkClicked(object sender, HtmlLinkClickedEventArgs e)
{
EventHandler<HtmlLinkClickedEventArgs> handler;

handler = this.LinkClicked;

if (handler != null)
{
handler(this, e);
}
}
```

Which solves both my problems - I can now override this in order to do my custom handling without having to bind the events, and resolve the race condition.

I'm happy to submit a pull request which fixes how the events are raised to resolve the race condition, but what are your thoughts towards making the `On*` methods protected virtual to allow for inheritance scenarios?

Regards;
Richard Moss

Comments: Yep, you are absolutely correct on both points. Please do submit a pull request. If you have the time it will be great if you also submit pull request for the extensibility stuff. Thx man.

Viewing all articles
Browse latest Browse all 693

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>