I would set up an event handler in the same way, but in the Calls_Calls_MouseUp method Calls_Calls_MouseUp you can start the thread to do the work:
private void Calls_Calls_MouseUp(object sender, MouseEventArgs e) { ThreadPool.QueueUserWorkItem(state => {
However, as a rule, I try to ensure that my event handlers are not as aware as possible, simply by calling some other method, often based on some condition:
private void Calls_Calls_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { DoSomething(); } } private void DoSomething() { ThreadPool.QueueUserWorkItem(state => {
This gives you the ability to trigger the same behavior from something other than the MouseUp event on a specific control (so that you can have the same behavior in a menu item, a toolbar button, and possibly a regular control button). It may also open up the possibility of conducting unit tests of functionality (although this is somewhat more complicated with asynchronous code).
source share