Click here to Skip to main content
15,891,136 members
Home / Discussions / C#
   

C#

 
GeneralRe: Locking excel file while writing Pin
Pete O'Hanlon14-Jun-10 22:52
mvePete O'Hanlon14-Jun-10 22:52 
QuestionAODL ans .xls files Pin
SummerBulb13-Jun-10 21:34
SummerBulb13-Jun-10 21:34 
QuestionDatagridview CellValidating [Solved] Pin
jojoba201113-Jun-10 21:34
jojoba201113-Jun-10 21:34 
QuestionReport Service - passing XML paramter to filter data Pin
thenewbee13-Jun-10 21:21
thenewbee13-Jun-10 21:21 
QuestionData Passing between two different Application Pin
Priyagdpl13-Jun-10 20:06
Priyagdpl13-Jun-10 20:06 
AnswerRe: Data Passing between two different Application Pin
Md. Marufuzzaman13-Jun-10 20:31
professionalMd. Marufuzzaman13-Jun-10 20:31 
AnswerRe: Data Passing between two different Application Pin
Abhinav S13-Jun-10 21:45
Abhinav S13-Jun-10 21:45 
QuestionDo stuff while button pressed down? [solved] Pin
la0113-Jun-10 16:52
la0113-Jun-10 16:52 
Hello CPers,

I'm trying to get a Winform to do some stuff while a button is pressed down, and stop doing it when the button's let go. I'm trying this out in a simple form with a button and a label. So as long as the button is pressed (MouseDown) the value displayed on the label should keep incrementing by 1. I've tried to used a BackgroundWorker for this and have so far failed. So, in my form class I have:

BackgroundWorker bwAsync = new BackgroundWorker();

public Form1() {
    InitializeComponent();

    bwAsync.WorkerReportsProgress = true;
    bwAsync.WorkerSupportsCancellation = true;
    bwAsync.DoWork += new DoWorkEventHandler(bwAsync_DoWork);
} 

private void button1_MouseDown(object sender, MouseEventArgs e) {

    bwAsync.RunWorkerAsync();
}

private void button1_MouseUp(object sender, MouseEventArgs e) {

    bwAsync.CancelAsync();
}


void bwAsync_DoWork(object sender, DoWorkEventArgs e) {

    BackgroundWorker bwAsync = sender as BackgroundWorker;

    int i;
    label1.Invoke( (MethodInvoker)delegate()
    {
        i = int.TryParse(label1.Text, out i) ? i : 0;
        while(!bwAsync.CancellationPending){
            Console.WriteLine("down");
            label1.Text = i.ToString();
            i++;
        }
    });
}


What happens here is, once it goes into DoWork() it gets stuck in the loop, and it never gets to the MouseUp handler. Anyone have any feeback on my code, or on how it's supposed to be done? Would be much appreciated.

Thank you.

modified on Sunday, June 13, 2010 11:47 PM

AnswerRe: Do stuff while button pressed down? Pin
Luc Pattyn13-Jun-10 17:18
sitebuilderLuc Pattyn13-Jun-10 17:18 
GeneralRe: Do stuff while button pressed down? Pin
la0113-Jun-10 17:46
la0113-Jun-10 17:46 
GeneralRe: Do stuff while button pressed down? Pin
Luc Pattyn13-Jun-10 18:02
sitebuilderLuc Pattyn13-Jun-10 18:02 
GeneralRe: Do stuff while button pressed down? Pin
la0115-Jun-10 13:33
la0115-Jun-10 13:33 
Questionneed help Pin
aaga513-Jun-10 9:56
aaga513-Jun-10 9:56 
AnswerRe: need help Pin
Luc Pattyn13-Jun-10 10:03
sitebuilderLuc Pattyn13-Jun-10 10:03 
AnswerRe: need help Pin
Lutosław13-Jun-10 12:26
Lutosław13-Jun-10 12:26 
AnswerRe: need help Pin
Abhinav S13-Jun-10 18:18
Abhinav S13-Jun-10 18:18 
QuestionProblem to use letters ( c: , d:) with the physical drive: Pin
muteb13-Jun-10 7:32
muteb13-Jun-10 7:32 
AnswerRe: Problem to use letters ( c: , d:) with the physical drive: Pin
Henry Minute13-Jun-10 8:14
Henry Minute13-Jun-10 8:14 
AnswerRe: Problem to use letters ( c: , d:) with the physical drive: Pin
Luc Pattyn13-Jun-10 8:21
sitebuilderLuc Pattyn13-Jun-10 8:21 
QuestionConnection string Pin
humayunlalzad13-Jun-10 3:01
humayunlalzad13-Jun-10 3:01 
AnswerRe: Connection string Pin
Abhinav S13-Jun-10 3:27
Abhinav S13-Jun-10 3:27 
GeneralRe: Connection string Pin
humayunlalzad13-Jun-10 3:51
humayunlalzad13-Jun-10 3:51 
Questioncrystal report and list<> Pin
reza assar12-Jun-10 21:39
reza assar12-Jun-10 21:39 
AnswerRe: crystal report and list Pin
Mycroft Holmes13-Jun-10 19:55
professionalMycroft Holmes13-Jun-10 19:55 
QuestionNeed to add 1 to a cache value Pin
Randall Hall12-Jun-10 18:07
Randall Hall12-Jun-10 18:07 

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.