How do I unsubscribe event?

patrick

Well-known member
Joined
Dec 5, 2021
Messages
302
Programming Experience
1-3
hello
How do I unsubscribe?


public delegate void UIChangedEventHandler(object sender, object e);
public event UIChangedEventHandler UIChanged;

UIChanged?.Invoke(sender, message);

How do I unsubscribe?
==> UIChanged -= UIChangedEventHandler(object sender, object e)
Is that correct?
 



Keyboard input does not work.
It's because of threads...
KeyPress Event does not work...
Its becasue of thread invoke

C#:
public string str;
public delegate void UIChangedEventHandler(object sender, object e);
public event UIChangedEventHandler UIChanged;

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
           UIChanged?.Invoke(sender, str);
}


C#:
private void listBox1_KeyPress(object sender, KeyPressEventArgs e)
{
           KeyPress Event does not work...
           becasue of thread Invoke
}




Please help me enter KeyPress event in Invoke state.


I can't actually code.

Please Help me... Please...
 
Last edited:
What does this question have to do with unsubscribing an event? It sounds like your problem right now is that you are either not getting any event notifications, or you are not able to fire your own UIChanged event?

How is listbox1_Keypress() related to listbox1_SelectedIndexChanged()? What does one have to do with the other?

Is the problem that listbox1_KeyPressed() is not being called?

How did you come to the conclusion that the problem has to do with threads? You said that you are not getting any errors, but Visual Studio would have thrown an exception if you were trying to call across threads.

As an aside, there is a difference your own UIChanged?.Invoke() vs trying listbox1?.Invoke(). The former is just a regular invocation of a delegate. The latter does a postback into Windows message queue so the delegate is run on the UI thread later.

I highly recommend putting together a complete and minimal code and sharing it with us. And then follow that up with a clear and complete explanation of what you are trying to achieve, and what behavior you are seeing. Tell us about what things you have tried.

Just saying "X doesn't work" doesn't provide enough information. I could equally say "my friend John doesn't work". It would be better to say that "my friend John just sits on the couch watching TV all day, eating all my snacks, and drinking all my beer".
 
Last edited:
Keyboard input does not work.
It's because of threads...
Do not enter here KeyPress.
KeyPress Event is not called.
becasue of thread Invoke

C#:
If there is [ UIChanged?.Invoke(sender, str); ]  in  [ listBox1_SelectedIndexChanged ]
1.  After clicking the first index in the ListBox, I pressed the keyboard arrow key.
2,. KeyPress Event is not called.
3.  BUT,  I pressed the keyboard arrow ↑ , ↓  key, 
but  [ listBox1_KeyPress]  was not called, but [ listBox1_SelectedIndexChanged ]  was called.

C#:
If you delete [ UIChanged?.Invoke(sender, str); ] from listBox1_SelectedIndexChanged,
1.  After clicking the first index in the ListBox, I pressed the keyboard arrow key.
2. KeyPress Event is called.
3. After, [ listBox1_SelectedIndexChanged ]  is called.

C#:
public string str;
public delegate void UIChangedEventHandler(object sender, object e);
public event UIChangedEventHandler UIChanged;

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
             str = "abc";
            UIChanged?.Invoke(sender, str);
}

C#:
private void listBox1_KeyPress(object sender, KeyPressEventArgs e)
{
            Do not enter here KeyPress.
           KeyPress Event is not called.
           becasue of thread Invoke
}



Please help me enter KeyPress event in Invoke state.


I can't actually code.

Please Help me... Please...
 
Last edited:
You are still showing us incomplete code. Show us the code of the delegate that you are setting UIChanged to. You might be accidentally setting yourself up for re-entrancy bugs.
 
Back
Top Bottom