4

Can I put a thread on a mouse event handler?

Calls_Calls.MouseUp += new MouseEventHandler(Calls_Calls_MouseUp);

How to add a thread over this?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
elisa
  • 743
  • 2
  • 13
  • 31
  • And why in the world do you want to do this? 9 times out of 10, all of your GUI work should be done on a single thread. What problem are you trying to solve by moving the `MouseUp` event handler to a separate thread? – Cody Gray - on strike Mar 02 '11 at 10:27
  • @CodyGray I think since the mouse events are in the same thread as UI, in intense graphical programs when you are panning or zooming, the mouse events are left behind. or maybe I'm wrong. I'm currently struggling with this problem http://stackoverflow.com/questions/27584324/slow-pan-and-zoom-in-wpf?noredirect=1#comment43602704_27584324 – Vahid Dec 21 '14 at 15:28

3 Answers3

3

I would set up the event handler in the same way, but in the Calls_Calls_MouseUp method you can launch a thread to do the work:

private void Calls_Calls_MouseUp(object sender, MouseEventArgs e)
{
    ThreadPool.QueueUserWorkItem(state => {
       // do the work here
    });
}

However, I typically try to have my event handlers as unaware as possible, just 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 => {
       // do the work here
    });
}

This gives you the ability to trigger the exact same behavior from something else than the MouseUp event on a certain control (so that you can have the same behavior on a menu item, a toolbar button and perhaps a regular command button). It may also open up the possibility to have unit tests on the functionality (even though that is somewhat trickier with asynchronous code).

Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
  • I'm getting `The calling thread cannot access this object because a different thread owns it.` error. What could be the problem? `ThreadPool.QueueUserWorkItem(state => { MousePos.Current = e.GetPosition(Window); if (!Window.IsMouseCaptured) return; var tt = GetTranslateTransform(Window); var v = Start - e.GetPosition(this); tt.X = Origin.X - v.X; tt.Y = Origin.Y - v.Y; });` – Vahid Dec 21 '14 at 15:34
  • 1
    @Vahid The problem is that you are trying to manipulate UI components from a non-UI-thread. Here is how to solve that: http://stackoverflow.com/a/872815/93623 – Fredrik Mörk Dec 22 '14 at 12:05
  • Thanks Fredrik, I've encountered a very difficult problem, that's why I asked the above a question in the above comment, perhaps you may know what it is.http://stackoverflow.com/questions/27584324/slow-pan-and-zoom-in-wpf/27592246?noredirect=1#comment43611472_27592246 – Vahid Dec 22 '14 at 12:08
1
Calls_Calls.MouseUp+= new MouseEventHandler(delegate(System.Object o, System.EventArgs e) { new Thread(Calls_Call_MouseUp).Start(); });

should work for you. If you get brackets errors, fix them since I handwrote the code :) :)

usr-local-ΕΨΗΕΛΩΝ
  • 26,101
  • 30
  • 154
  • 305
  • i have identifier expected error for sender and e:). Can you tell me how to solve them? – elisa Mar 02 '11 at 10:31
1

you can also use BackgroundWorker for this in case you require any updation on the UI for progress and completion.

Mubashir Khan
  • 1,464
  • 1
  • 12
  • 17