delegates

Invoke() and BeginInvoke() behaving differently when executing an overridable method via a delegate

Can anyone tell me why this code behaves the way it does? See comments embedded in the code... Am I missing something really obvious here? using System; namespace ConsoleApplication3 { public class Program { static void Main(string[] args) { var c = new MyChild(); c.X(); Conso...

Wrapping StopWatch timing with a delegate or lambda?

I'm writing code like this, doing a little quick and dirty timing: var sw = new Stopwatch(); sw.Start(); for (int i = 0; i < 1000; i++) { b = DoStuff(s); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); Surely there's a way to call this bit of timing code as a fancy-schmancy .NET 3.0 lambda rather than (God forbid) cutting ...

What is the best way to execute sequential methods?

Working on a project where a sequential set of methods must be run every x seconds. Right now I have the methods contained within another "parent method", and just sequentially call them right after another. class DoTheseThings() { DoThis(); NowDoThat(); NowDoThis(); MoreWork(); AndImSpent(); } Each method must ru...

How do I form a good predicate delegate to Find() something in my List<T>?

After looking on MSDN, it's still unclear to me how I should form a proper predicate to use the Find() method in List using a member variable of T (where T is a class) For example: public class Car { public string Make; public string Model; public int Year; } { // somewhere in my code List<Car> carList = new List<Car>();...

Delegate Array

I am experimenting with calling delegate functions from a delegate array. I've been able to create the array of delegates, but how do I call the delegate? public delegate void pd(); public static class MyClass { static void p1() { //... } static void p2 () { //... } //... static pd[]...

How do I safely populate with data and Refresh() a DataGridView in a multi-threaded application?

My app has a DataGridView object and a List of type MousePos. MousePos is a custom class that holds mouse X,Y coordinates (of type "Point") and a running count of this position. I have a thread (System.Timers.Timer) that raises an event once every second, checks the mouse position, adds and/or updates the count of the mouse position on t...

Multiple methods that need to be handled the same way

Hello, In a piece of C# that I am writing at the moment I need to handle several methods with the same signature in the same way. Also there might be more of these methods in the future. Instead of repeating the same kind of logic over and over I thought up the following: private delegate bool cleanStep(BuildData bd, out String strFail...

Interactions between windows

Hello there, what's the best/proper way of interacting between several windows in C# app? Recently, I've run into a problem where one of program windows has to call method modifying main window. My solution was to create factory-like class, that would arrange all underlying model-data and organize the communication between various wind...

C# callback from DLL

I'm writing Application A and DLL B, both in C#.NET. How do I do the following: A calls function in B Want B to use delegate/callback to update status in UI of A This is not about BackgroundWorker...that part works fine in A. What I can't see is how to let B know what function to call in A. ...

Are delegates and callbacks the same or similiar?

Hi, Are delegates the same thing as callbacks? Or are they related somehow? ...

How to correctly unregister an event handler

In a code review, I stumbled over this (simplified) code fragment to unregister an event handler: Fire -= new MyDelegate(OnFire); I thought that this does not unregister the event handler because it creates a new delegate which had never been registered before. But searching MSDN I found several code samples which use this idiom. So...

How do I fix this error: "MyEvent += expected" ?

I have a delegate, say: public delegate void MyDelegate(); I have an event, say: public MyDelegate MyEvent; While invoking the event I am receiving an error message: "MyEvent += expected ....." How do I resolve this? ...

C#: delegate keyword vs. lambda notation

Once it is compiled, is there a difference between: delegate { x = 0; } and () => { x = 0 } ? ...

Advantages of using delegates?

I'm looking to implement the Observer pattern in VB.NET or C# or some other first-class .NET language. I've heard that delegates can be used for this, but can't figure out why they would be preferred over plain old interfaces implemented on observers. So, Why should I use delegates instead of defining my own interfaces and passing arou...

Does using delegates slow down my .NET programs?

Does using delegates slow down my programs? I've been avoiding them because I really have no clue if they make my programs any slower. I know if I cause a (catch) exception, that uses quite a bit of CPU power but I don't know about Delegates and Events and what .NET does to them. ...

C# Delegates - How Often Do You Use Them, And When?

Delegates look like such a powerful language feature, but I've yet to find an opportunity to use them in anger (apart from in DALs I must say). How often do you use them, and under what circumstances do you find them most useful? ...

Do you use Template Method Pattern in programming languages with closures/delegates/function pointers ?

I have been going back and forth between C# and Java for the last 8 years. One thing that strikes me is that I have completely stopped using the "Template Method" design pattern in C#. Actually, in C# I Have come to think of this pattern as an anti-pattern. http://en.wikipedia.org/wiki/Template_method_pattern Coming back to Java, I ...

Where to put your delegates . . .

I am trying to determine the best directory structure of my application i have: UI Data Interfaces but i dont know where to put delegates.. should there be a seperate Delegates folder or should i store the delegates in the same classes where they are being used . . ...

What's so great about Func<> delegate?

Hi, Sorry if this is basic but I was trying to pick up on .Net 3.5. Question: Is there anything great about Func<> and it's 5 overloads? From the looks of it, I can still create a similar delgate on my own say, MyFunc<> with the exact 5 overloads and even more. eg: public delegate TResult MyFunc<TResult>() and a combo of various overl...

NSApplication delegate and Preference Panes

It seems that I can't control the NSApp delegate from within a System Preferences pane, which is understandable. Is there any other way I can have my object notified when the program becomes active? ...