delegates

Asynchronous Multicast Delegates

。_饼干妹妹 提交于 2020-01-12 13:48:46
问题 I've been doing some work lately on a project that makes extensive use of events. One of the things that I need to do is asynchronously call multiple event handlers on a multicast delegate. I thought the trick would be to call BeginInvoke on each item from GetInvocationList, but it appears as though BeginInvoke doesn't exist there. Is there a way to do this or do I need to start using ThreadPool.QueueUserWorkItem and sort of roll my own solution that way? 回答1: GetInvocationList just returns

Asynchronous Multicast Delegates

一世执手 提交于 2020-01-12 13:48:45
问题 I've been doing some work lately on a project that makes extensive use of events. One of the things that I need to do is asynchronously call multiple event handlers on a multicast delegate. I thought the trick would be to call BeginInvoke on each item from GetInvocationList, but it appears as though BeginInvoke doesn't exist there. Is there a way to do this or do I need to start using ThreadPool.QueueUserWorkItem and sort of roll my own solution that way? 回答1: GetInvocationList just returns

Asynchronous Multicast Delegates

时光总嘲笑我的痴心妄想 提交于 2020-01-12 13:48:14
问题 I've been doing some work lately on a project that makes extensive use of events. One of the things that I need to do is asynchronously call multiple event handlers on a multicast delegate. I thought the trick would be to call BeginInvoke on each item from GetInvocationList, but it appears as though BeginInvoke doesn't exist there. Is there a way to do this or do I need to start using ThreadPool.QueueUserWorkItem and sort of roll my own solution that way? 回答1: GetInvocationList just returns

Using a Delegate to call a constructor

喜你入骨 提交于 2020-01-12 08:01:47
问题 I found this but tried to use it and failed. How can i create an object using reflections and make it fast by putting it in a delegate? DynamicMethod dm = new DynamicMethod("MyCtor", t, new Type[] { }); var ctor = t.GetConstructor(new Type[] { }); ILGenerator ilgen = dm.GetILGenerator(); ilgen.Emit(OpCodes.Ldarg_0); ilgen.Emit(OpCodes.Newobj, ctor); ilgen.Emit(OpCodes.Ret); var d = (Func<T>)dm.CreateDelegate(t); dm.Invoke(null, new object[] { }); Before putting it in a deleage i tried to at

not implemented delegate method leads to crash

删除回忆录丶 提交于 2020-01-12 07:00:11
问题 I created a protocol and assigned it to a delegate object @protocol AppBrainDelegate <NSObject> @optional - (void)didLocateUser; - (void)didFinishLoadingDataWithData:(NSDictionary *)fetchedData; @end @interface Brain : NSObject @property (strong, nonatomic) id <AppBrainDelegate> delegate; I thought the meaning of this @optional in the protocol declaration means, that controllers don't have to listen to the delegate method if they don't want to. Here's the crash log if do not implement the

Objective C delegate or C-style block callback?

[亡魂溺海] 提交于 2020-01-12 05:24:09
问题 I am designing a class that will "fire events" whenever something occurs. These events tend to be non-UI related. I'm wondering what the best method for doing so is. I've been exploring either: Delegates I'll define a delegate class, accept a delegate in the init function, and call the methods on the delegate class when an event occurs. C-style blocks I'll define a function pointer, and accept a function in the init function. I'll call it when an event occurs. In both cases, I may need to

Objective C delegate or C-style block callback?

戏子无情 提交于 2020-01-12 05:24:06
问题 I am designing a class that will "fire events" whenever something occurs. These events tend to be non-UI related. I'm wondering what the best method for doing so is. I've been exploring either: Delegates I'll define a delegate class, accept a delegate in the init function, and call the methods on the delegate class when an event occurs. C-style blocks I'll define a function pointer, and accept a function in the init function. I'll call it when an event occurs. In both cases, I may need to

Can I assign a method to multiple Form-based Events?

依然范特西╮ 提交于 2020-01-11 09:42:08
问题 I'm constructing a Form and it has several numericUpDown controls, several checkbox controls and some text boxes etc. Each control has a event method (CheckedChanged, ValueChanged etc) that is triggered that does something but my main quesiton is this: What I want to do is run a single method which will update a text field on my form but currently I have it just repeated 24 times. This works but I feel there must be a better way ... Below is an example of what I have so far. private void

C# why shall I use “new” keyword when subscribing for an event?

微笑、不失礼 提交于 2020-01-11 05:46:08
问题 What is the difference between following 2 ways of subscribing for an event? receiver.ConfigChanged += Config_ConfigChanged; receiver.ConfigChanged += new EventHandler(Config_ConfigChanged); It seems that both of them work the same way but if so, what is the point of using the second one? What about unsubscribing, will both following methods work also the same way? receiver.ConfigChanged -= Config_ConfigChanged; receiver.ConfigChanged -= new EventHandler(Config_ConfigChanged); 回答1: The

Delegates, Lambdas, Action, Func, Anonymous Functions

你离开我真会死。 提交于 2020-01-10 19:48:08
问题 I just want to verify my understanding about the following Delegate - a method signature Lambdas - anonymous function Anonymous Function - just that Action - An anonymous function that returns nothing Func - An anonymous function that returns something hmm... they all do similar things, how do you define & know when to use each? sorry, I don't explain well 回答1: Delegate - it is not a method signature. It is a type which encapsulates a method . Hence a delegate declaration should have a