我加入處理程序申請:如何從應用程序中刪除事件處理程序?
Application.ThreadException += (sender, a) => UnhandledExceptionsHelper.ApplicationOnThreadException(a, null);
這個我想添加另一個處理程序
Application.ThreadException += (sender, a) => UnhandledExceptionsHelper.ApplicationOnThreadException(a, param);
如何刪除以前的處理程序之後?
當我刪除控制處理程序,我只是用:
public void RemoveOnThreadException(SimpleButton b)
{
FieldInfo f1 = typeof(Control).GetField("EventClick",
BindingFlags.Static | BindingFlags.NonPublic);
object obj = f1.GetValue(b);
PropertyInfo pi = b.GetType().GetProperty("Events",
BindingFlags.NonPublic | BindingFlags.Instance);
EventHandlerList list = (EventHandlerList)pi.GetValue(b, null);
list.RemoveHandler(obj, list[obj]);
}
我怎樣才能做同樣的應用程序和應用程序域?
@Andrey有我嘗試用Button.Click:
public TestForm()
{
InitializeComponent();
simpleButton1.Click += (sender, a) => simpleButton1_Click(sender,a);
simpleButton1.Click -= simpleButton1_Click;
simpleButton1.Click += (sender, a) => simpleButton1_Click(sender, a);
}
private void simpleButton1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hi");
}
,當我點擊按鈕,我得到兩個消息。
[C#中取消匿名方法(可能的重複http://stackoverflow.com/questions/183367/unsubscribe-anonymous-方法在C - 尖銳) – Pasick
兩個問題:1)當你想添加另一個處理程序?即在一些用戶操作之後,或者在第一個處理程序被觸發後2)你在哪裏得到第二個處理程序的'param'? –
@Pasick這不是100%重複,因爲有一些關於Application.ThreadException的具體細節 – Andrey