delegates

What is Difference in assign an event...the correct method?

Hi Can you tell me what the difference is between these methods of attaching an event handler? //Method 1 this.button4.Click += new RoutedEventHandler(button4_Click); //Method 2 this.button4.Click += button4_Click; ... void button4_Click(object sender, RoutedEventArgs e) { } ...

Many many-to-many filters

Today we faced a quite simple problem that were made even simpler by the dear predicates. We had a kind of event log and wanted to filter it client side (Windows Forms) using a list of criterias. We began by implementing to filter by a number of categories. private List<Events> FilterEventsByCategory(List<Events> events, ...

What constitutes 'redundant delegate creation'?

I was registering to an event in my class, and as per usual I was lazy and just use the autocomplete feature built into Visual Studio 2008 Pro which auto creates the delegate creation and it's associated method. public abstract class FooBase { protected event EventHandler<MyValueChangedArgs> MyValueChanged; protected FooBase() ...

Can I ignore delegate parameters with lambda syntax?

I am curious why C# allows me to ignore delegate parameters in some cases but not others. For instance this is permitted: Action<int> action = delegate { Console.WriteLine("delegate"); }; but this is not: Action<int> action = () => Console.WriteLine("lambda"); Is there a way to initialize a delegate and ignore the parameters using...

.NET: How to talk to a form during BeginInvoke callback?

i have a long running function¹: public string FindPasswordFromHash(String hash) { ... } which is called like: private void Button1_Click(object sender, EventArgs e) { PasswordTextBox.Text = FindPasswordFromHash(HashTextBox.Text); } Now i want to convert it into the asynchronous BeginInvoke/EndInvoke delegate pattern: priv...

Why won't my textbox update when I am doing the exact same thing just with a different function?

My TextBox won't update! I am using it as a Log to update what other things are doing... Form 1 code: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; using System.Data.OleDb; using System.Collections.Specialized; using System.Tex...

Checking to see if an optional protocol method has been implemented

Hey there, Does anyone know the best way to check to see if an optional protocol method has been implemented. I tried this: if ([self.delegate respondsToSelector:@selector(optionalProtocolMethod:)] ) where delegate is: id<MyProtocol> delegate; However - I get an error saying that the function respondsToSelector is not found in th...

C# compile error: "Invoke or BeginInvoke cannot be called on a control until the window handle has been created."

I just posted a question about how to get a delegate to update a textbox on another form. Just when I thought I had the answer using Invoke...this happens. Here is my code: Main Form Code: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using Syst...

raw function pointer from a bound method

I need to bind a method into a function-callback, except this snippet is not legal as discussed in demote-boostfunction-to-a-plain-function-pointer. What's the simplest way to get this behavior? struct C { void m(int x) { (void) x; _asm int 3; }}; typedef void (*cb_t)(int); int main() { C c; boost::function<void (int x...

C# Redundant Delegate

public delegate T GetObject<T>(SqlDataReader reader); public delegate KeyValuePair<TKey, TValue> GetObject<TKey, TValue>(SqlDataReader reader); Is it possible for removing that 2nd line (the two lines are on consecutive lines) to ever make any difference at all, superficial or not? ...

in c# what is the difference between strategy pattern and delegates?

I've been looking at strategy pattern implementation examples and it seems to me like they are very similar to c# delegates. The only difference I see is that strategy pattern implementations don't need to explicitly declare a delegate. But other than that, they both seem to point to functions that need a specific signature and they ca...

C#: Triggering an Event when an object is added to a Queue

I need to be able to trigger a event whenever an object is added to a Queue<Delegate>. I created a new class that extends Queue: public delegate void ChangedEventHandler(object sender, EventArgs e); public class QueueWithChange<Delegate> : Queue<Delegate> { public event ChangedEventHandler Changed; protected virtual void OnC...

Is EndInvoke() optional, sort-of optional, or definitely not optional?

I've read conflicting opinions as to whether every BeginInvoke() has to be matched by an EndInvoke(). Are there any leaks or other problems associated with NOT calling EndInvoke()? ...

What's the difference between QueueUserWorkItem() and BeginInvoke(), for performing an asynchronous activity with no return types needed

Following on from my BeginInvoke()/EndInvoke() question, are there major differences in performance/anything else between Delegate.BeginInvoke() and using QueueUserWorkItem() to invoke a delegate asynchronously? ...

How bad is the following snippet?

My question is simple: how bad is the following snippet of code? How would you do it? CancelEventHandler _windowClosing; private CancelEventHandler WindowClosing { set { clearEventHandlerList(); this.Closing += value; _windowClosing = value; /* * if calling the method with null parameters, * it will set ...

Problem with delagates after migrating .Net 1.1 project to 3.5

I've migrated a .Net legacy .Net project from .Net 1.1 to .Net 3.5 and when I try and run it I'm getting the following error: Cross-thread operation not valid: Control 'grpLogin' accessed from a thread other than the thread it was created on. When the a thread (Which I assume is not the UI thread) changes the enabled property...

Testing a C# Function Pointer (Delegate) for Null

I am testing a function pointer called ErrorLoggingMethod in a C# DLL to prevent exceptions due to a null value, as below, but it seems that at runtime, the function is actually being executed when I really mean to test its delegate for null. I am inadvertently getting a null exception as a result--just what I am trying to avoid! My or...

Iphone: TabView + TableView

I think I'm missing something simple, but I can't figure out exactly what it is. I'm trying to set up an App with a UITabViewController, and one of the Tabs will have a UITableView and UISearchBar (but no Navigation Controller). I set up the UITabViewController with all the tabs in interface builder, and the views are in their own xib ...

C#: Difference between ' += anEvent' and ' += new EventHandler(anEvent)'

Take the below code: private void anEvent(object sender, EventArgs e) { //some code } What is the difference between the following ? [object].[event] += anEvent; //and [object].[event] += new EventHandler(anEvent); [UPDATE] Apparently, there is no difference between the two...the former is just syntactic sugar of the latte...

How do i exit a List<string>.ForEach loop when using an anonymous delegate?

In a normal loop you can break out of a loop using break. Can the same be done using an anonymous delegate? Example inputString and result are both declared outside the delegate. blackList.ForEach(new Action<string>( delegate(string item) { if(inputString.Contains(item)==true) { result = true; ...