Click here to Skip to main content
16,021,294 members
Home / Discussions / C#
   

C#

 
AnswerRe: While MouseDown Pin
Richard MacCutchan17-Oct-09 4:44
mveRichard MacCutchan17-Oct-09 4:44 
AnswerRe: While MouseDown PinPopular
Luc Pattyn17-Oct-09 5:08
sitebuilderLuc Pattyn17-Oct-09 5:08 
GeneralRe: While MouseDown Pin
Zap-Man17-Oct-09 5:43
Zap-Man17-Oct-09 5:43 
GeneralRe: While MouseDown Pin
Not Active17-Oct-09 6:17
mentorNot Active17-Oct-09 6:17 
AnswerRe: While MouseDown Pin
Not Active17-Oct-09 5:42
mentorNot Active17-Oct-09 5:42 
GeneralRe: While MouseDown Pin
Zap-Man17-Oct-09 5:46
Zap-Man17-Oct-09 5:46 
GeneralRe: While MouseDown Pin
Not Active17-Oct-09 6:15
mentorNot Active17-Oct-09 6:15 
AnswerRe: While MouseDown Pin
dojohansen18-Oct-09 0:49
dojohansen18-Oct-09 0:49 
First, the reason your original idea doesn't work is that the event handlers are all on the same thread. The mouseup handler cannot run until mousedown returns, and mousedown doesn't return until mouseup has run, so you're basically deadlocking yourself with only one thread! Big Grin | :-D

Depending on what you do in those handlers you may be able to do it in another thread instead. But if you want to do anything to the GUI itself, you aren't allowed to do so on any thread but the "gui thread", which is the same thread that is raising the events.

So you have to use another hook... to marshal the call back to the UI thread again!

Here's an example of how to do this. To run it, create a new windows forms application and replace Form1.cs with:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        long n;
        MouseButtons buttons;
        Label label1;

        public Form1()
        {
            label1 = new Label();
            label1.Text = "here";
            Controls.Add(label1);
            label1.MouseUp += new MouseEventHandler(label1_MouseUp);
            label1.MouseDown += new MouseEventHandler(label1_MouseDown);
        }


        void label1_MouseDown(object sender, MouseEventArgs e)
        {
            buttons = e.Button;
            new Thread(run).Start();
        }

        void label1_MouseUp(object sender, MouseEventArgs e)
        {
            buttons = MouseButtons.None;
        }

        void run()
        {
            var upd = new Action(update);
            while (buttons != MouseButtons.None) label1.Invoke(upd);
        }

        void update()
        {
            n = (buttons == MouseButtons.Left ? --n : ++n);
            label1.Text = n.ToString();
        }
    }
}

QuestionOwner drawn TabControl doesn't act as I want it Pin
WebMaster17-Oct-09 0:20
WebMaster17-Oct-09 0:20 
AnswerRe: Owner drawn TabControl doesn't act as I want it Pin
WebMaster17-Oct-09 21:28
WebMaster17-Oct-09 21:28 
GeneralRe: Owner drawn TabControl doesn't act as I want it Pin
Dave Kreskowiak18-Oct-09 2:33
mveDave Kreskowiak18-Oct-09 2:33 
QuestionIndexing a property that returns an array. [modified] Pin
CaptainSeeSharp16-Oct-09 17:15
CaptainSeeSharp16-Oct-09 17:15 
AnswerRe: Indexing a property that returns an array. [modified] Pin
Luc Pattyn16-Oct-09 17:49
sitebuilderLuc Pattyn16-Oct-09 17:49 
AnswerRe: Indexing a property that returns an array. Pin
dojohansen18-Oct-09 0:59
dojohansen18-Oct-09 0:59 
QuestionCouple of questions regarding Windows Service in C# Pin
Aryan S16-Oct-09 17:14
Aryan S16-Oct-09 17:14 
AnswerRe: Couple of questions regarding Windows Service in C# Pin
Dave Kreskowiak16-Oct-09 18:22
mveDave Kreskowiak16-Oct-09 18:22 
AnswerRe: Couple of questions regarding Windows Service in C# Pin
PIEBALDconsult17-Oct-09 5:40
mvePIEBALDconsult17-Oct-09 5:40 
QuestionForm flickering problem Pin
SimpleData16-Oct-09 9:55
SimpleData16-Oct-09 9:55 
AnswerRe: Form flickering problem Pin
Luc Pattyn16-Oct-09 10:25
sitebuilderLuc Pattyn16-Oct-09 10:25 
GeneralRe: Form flickering problem Pin
SimpleData16-Oct-09 11:24
SimpleData16-Oct-09 11:24 
GeneralRe: Form flickering problem Pin
Luc Pattyn16-Oct-09 12:40
sitebuilderLuc Pattyn16-Oct-09 12:40 
GeneralRe: Form flickering problem Pin
SimpleData16-Oct-09 23:19
SimpleData16-Oct-09 23:19 
GeneralRe: Form flickering problem Pin
Luc Pattyn17-Oct-09 2:18
sitebuilderLuc Pattyn17-Oct-09 2:18 
GeneralRe: Form flickering problem Pin
Lyon Sun17-Oct-09 4:14
Lyon Sun17-Oct-09 4:14 
GeneralRe: Form flickering problem Pin
Luc Pattyn17-Oct-09 4:18
sitebuilderLuc Pattyn17-Oct-09 4:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.