delegates

is it necessary to gchandle.alloc() each callback in a class?

限于喜欢 提交于 2020-04-10 11:56:10
问题 I have a .NT class which has multiple delegates for callbacks from native code. Is it necessary to allocate all the delegates? I mean does GCHandle.Alloc() protects just the delegate or the entire class that owns the delegate from being collected? 回答1: A delegate has two relevant properties, Method and Target. The Target will be non-null if the delegate was created for an instance method. And that keeps the object alive, as long as the garbage collector can see the delegate instance. Native

C# - Remove Event From Browser

本秂侑毒 提交于 2020-03-27 07:02:06
问题 I add an event to webBrowser as below code: webBrowser.DocumentCompleted += (s, e) => {}; Now How can I remove this event inside of it? Somthing like this: webBrowser.DocumentCompleted += (s, e) => { webBrowser.DocumentCompleted -= this; }; 回答1: You just need a reference to your delegate. Something like this: WebBrowserDocumentCompletedEventHandler complete = null; complete = (s, e) => { webBrowser.DocumentComplete -= complete; }; webBrowser.DocumentComplete += complete; 回答2: I'm putting this

Swift - Reusable URL Request with Delegates

孤人 提交于 2020-03-23 11:59:05
问题 Hi I'm new to Swift and I am trying to create a reusable generic Download Manager for URL Request that can be reused throughout my project in different View Controllers or reused within the same VC for a different URL Request calls. The problem that I have is how do I pass the Data Type from the Request into the Download Manager and then return the Downloaded Data back to the VC with the corresponding Data Type. I am able to pass the Data Type in a call to downloadRequest but I can't figure

Store Linq function into Variable & define on the fly?

。_饼干妹妹 提交于 2020-03-22 09:04:12
问题 I have a Linq query like this var results= StudentsList.GroupBy(x=> x.GroupID) .GroupBy(x=> x.Any(g=>g.IsQualified== true)) .Select(g=> g) .ToList(); I want to store the part x.Any(g=>g.IsQualified== true) into a variable so that I can change it on the fly (example: x.Any(g=>g.StudentName== "John") ) based on my requirement and without defining a new Linq query separately. Is that possible? Pseudo Code static void SomeFunction(Func<int, int> op) { var results= StudentsList.GroupBy(x=> x

type-safe Control.Invoke C#

混江龙づ霸主 提交于 2020-03-21 06:36:19
问题 I am programming a software in C# at work that contains 2 Threads a Thread that control a Form (Windows Forms) and interfaces with the user. a Thread that checks online data at the background. I need the second thread to print a massage on the form when the online data is irregular. because only the thread that created the control can change it, I am using delegates. the second thread calls the first thread to execute a delegate by the Control.Invoke method. Example: public partial class

Where do I find event declarations in Visual Studio

↘锁芯ラ 提交于 2020-03-04 02:40:28
问题 I know that VS will open an eventhandler stub by doubleclicking on an event. I found the underlying event declaration in InitializeComponent of the form on which the button is located. this.buttonWorkOn.Click += new System.EventHandler(this.buttonWorkOn_Click); Can I use this event declaration (of Visual Studio) and register another eventhandling method with it? Upon instantiation of that other form its eventhandling method would need to register itself with the click event of the button on

super respondsToSelector: returns true but actually calling super (selector) gives “unrecognized selector sent to instance”

旧时模样 提交于 2020-02-13 04:09:51
问题 OK, I am a little confused. I have a subclass of UIScrollView, which is my attempt at a horizontally scrolling "table view" like UI element. UIScrollView itself sets up UIGestureRecognizers it uses internally, and it appears to set itself up as the delegate for those UIGestureRecognizers. I also have my own UIGestureRecognizer setup on my horizontal table elements/cells and my own class set as delegate for my own UIGestureRecognizer. Since my class is a subclass of UIScrollView, at runtime,

super respondsToSelector: returns true but actually calling super (selector) gives “unrecognized selector sent to instance”

巧了我就是萌 提交于 2020-02-13 04:09:13
问题 OK, I am a little confused. I have a subclass of UIScrollView, which is my attempt at a horizontally scrolling "table view" like UI element. UIScrollView itself sets up UIGestureRecognizers it uses internally, and it appears to set itself up as the delegate for those UIGestureRecognizers. I also have my own UIGestureRecognizer setup on my horizontal table elements/cells and my own class set as delegate for my own UIGestureRecognizer. Since my class is a subclass of UIScrollView, at runtime,

Should I Create a New Delegate Instance?

回眸只為那壹抹淺笑 提交于 2020-02-11 13:33:35
问题 What are the implications of doing this... this.myButton.Click += new EventHandler(this.myButton_Clicked); ...versus this? this.myButton.Click += this.myButton_Clicked; I suspect that the compiler is creating a new instance for me in the second example. I'm sure this is a bit of a newbie question, but Google didn't turn up anything. Can anyone give me some insight? 回答1: The 2nd syntax is a shortcut for the 1st one introduced in C# 2.0. http://www.developer.com/net/csharp/article.php/3103031

Should I Create a New Delegate Instance?

假如想象 提交于 2020-02-11 13:33:06
问题 What are the implications of doing this... this.myButton.Click += new EventHandler(this.myButton_Clicked); ...versus this? this.myButton.Click += this.myButton_Clicked; I suspect that the compiler is creating a new instance for me in the second example. I'm sure this is a bit of a newbie question, but Google didn't turn up anything. Can anyone give me some insight? 回答1: The 2nd syntax is a shortcut for the 1st one introduced in C# 2.0. http://www.developer.com/net/csharp/article.php/3103031