delegates

How do I invoke an extension method using reflection?

I appreciate that similar questions have been asked before, but I am struggling to invoke the Linq Where method in the following code. I am looking to use reflection to dynamically call this method and also dynamically build the delegate (or lambda) used in the Where clause. This is a short code sample that, once working, will help to fo...

Best way to call a generic method multiple times (runtime return type) using a parameter-safe invocation

Hi, I feel like I'm so close to working this out and have read dozens of articles and existing questions. I have a method like this: public T DoStuff(object arg1, object arg2) { /* Do stuff and return a T */ } And I need to be able to pass this method to another class for callback purposes. However, when this other class calls bac...

Asynchronous Multicast Delegates

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 t...

Checking for a valid delegate object before sending it a message.

I am trying to implement the delegate Pattern in Objective-C, however I am experiencing a Bad Access exception when invoking the delegate sometimes. It seems this is caused by the delegate being released. Apple does not recommend to retain delegates. How can I check my delegate if is still valid before trying to send it a message? ...

returning from anonymous delegate in C# 3.0

If you are in a method and you pass in an anonymous delegate, does the 'return' key word return a value for the anonymous delegate, or does it return the function? I know in ruby, they use 'next' to achieve this type of functionality inside of a block. Here's an example: public bool X() { AList.Where(x => { if (x.val ==...

C# - As Delegates doing Asyn task still do i need System.Threading ?

As i can perform asynchronous operations using delegates,i suspect there is a thin chance to use System.Threading in my application.Is there any essential situation where i can not avoid System.Threading ? ( Just I am in learning Phase). Example : class Program { public delegate int Total (int a,int b); static void Main() { Tot...

async delegate new form

here code delegate void CheckNewsDelegate(); void CheckNews() { frmNews news = new frmNews(); news.Show(); } CheckNewsDelegate dlg = new CheckNewsDelegate(CheckNews); dlg.BeginInvoke(null, null); new form not create normal. how fix it? ...

Can I write a general purpose method to call other methods with a timeout?

I have a problem that is surely familiar to many: I'm making a call (to be specific it is Forest.GetCurrentForest()) which, in some circumstances, will fail to work and throw an exception. Not a huge problem, just catch it and deal appropriately. However the call, when it fails, is very slow; it takes 30 seconds to fully finish. I w...

WaitCallback and QueueUserWorkItem

When i implement WaitCallback waitCB = new WaitCallback(DisplayTime); for (int i = 1; i <= 5; i++) { ThreadPool.QueueUserWorkItem(waitCB, null); Thread.Sleep(2000); } public void DisplayTime(object state) { Console.WriteLine("Current Time {0} ", DateTime.Now); } ( 1 ) Does it mean ,my job is queued into to ...

Should I use Delegate Classes if I want items to be edited using a dialog in Qt?

All examples in Qt show that one should use delegate classes to provide editors that reside within QTreeView (QListView, etc). I want to have a separate non-modal dialog to edit item's attributes. Should I use delegate classes to do so? That is, no editing or special behavior is required within QTreeView. ...

How can I avoid having to pass around/store a delegate when using BeginInvoke and EndInvoke?

Edit: Moved the actual question to the top. Update: Found an example by Microsoft, tucked on some more code at the end. My questions are these: Is it safe to call multiple BeginInvoke calls on the same delegate instance, or do I have to construct a new delegate instance for each in-flight method call? If I have to construct new insta...

Not sure I'm doing this the best way...

I have app delegate.h & .m files linked to a Main.Nib. Nib has a window and a tabbar controller which has 4 navigation controllers. Each of these 4 do their own thing inside 4 more Nib's containing tableview controllers etc. The tableview controllers take their data from arrays manipulated in the app delegate. All work fine and i hav...

C# removing an event handler

Been doing this for a while but haven't noticed I have been using a "new" each time I remove an event handler. Am I supposed to be creating a new object? Basically is there a difference between 1 and 2? (1)ethernetdevice.PcapOnPacketArrival -= new SharpPcap.PacketArrivalEvent(ArrivalResponseHandler); (2)ethernetdevice.PcapOnPacketArri...

Why don't I add parantheses when assigning a method to a delegate?

What is the reason why I can't put parenthesis after my Method name when assigning it to a delegate type. Here is code: public delegate Simple Simple(); //Create a delegate that returns its own type. class Program { public class Exercise { public static Simple Welcome() { Console.WriteLine("Welcome!...

How does the compiler infer types in the delegate example?

In the following delegate example, how does the compiler infer what type the variable alpha is? delegate double Doubler(double x); public class Test { Doubler dbl = (alpha) => //How does it determine what type is alpha? { return alpha * 2 }; Console.WriteLine(dbl(10)); //Is it when the method is called? int he...

Can I have an Action<> or Func<> with an out param?

I have a method with an out parameter, and I'd like to point an Action or Func (or other kind of delegate) at it. This works fine: static void Func(int a, int b) { } Action<int,int> action = Func; However this doesn't static void OutFunc(out int a, out int b) { a = b = 0; } Action<out int, out int> action = OutFunc; // loads of comp...

Redirect method call within delegate

I have a protected method in a base class which accepts a Func<T> and then turns around and executes with some added goodness. Example usage: public MyResponse DoSomething(MyRequest request) { return base.Execute(() => this.Channel.DoSomething(request)); } What I'm looking to do is take the func delegate instance and redirect the ...

check C# Action/Lambda/Delegate contains any code/statements

Can anyone tell me if there a way to see if an action contains any code? Action x = new Action(()=> { }); should return false, while Action x = new Action(()=> { var x = "i am a string" }); should return true. Perhaps using reflection? ...

vb.net possible to monitor raise events across applications?

I may have gone crazy... but I am hoping there is a way to do this. I have a base class that has event handling in it. My console application is running my workflow. Part of that workflow is to raise events at specific intervals in a separate thread to broadcast the workers' current state (a heartbeat I have heard many call it). I also...

C# Winforms: Pass a function from one form to another?

Situation: C#, .NET 3.5, WinForms Objective: Form1 has a Button that can be enabled via the constructor. I.E. the button will only be shown if I call new Form1(true). We'd like to be able to pass a method via some param from Form2 to Form1 (doesn't have to be on Form1's constructor), and when Form1.Button1 is pressed, Form2.<SomeMethod...