Click here to Skip to main content
15,907,905 members
Home / Discussions / C#
   

C#

 
GeneralRe: Session Problem. Pls Help Pin
Guffa28-Oct-06 1:34
Guffa28-Oct-06 1:34 
QuestionHow to open a file in c# winform? Pin
ecscheng27-Oct-06 21:00
ecscheng27-Oct-06 21:00 
AnswerRe: How to open a file in c# winform? Pin
Stefan Troschuetz27-Oct-06 21:31
Stefan Troschuetz27-Oct-06 21:31 
QuestionBackgroundWorker Pin
freshonlineMax27-Oct-06 19:46
freshonlineMax27-Oct-06 19:46 
AnswerRe: BackgroundWorker Pin
Nader Elshehabi27-Oct-06 20:41
Nader Elshehabi27-Oct-06 20:41 
AnswerRe: BackgroundWorker Pin
Robert Rohde28-Oct-06 3:37
Robert Rohde28-Oct-06 3:37 
GeneralRe: BackgroundWorker [modified] Pin
freshonlineMax28-Oct-06 7:57
freshonlineMax28-Oct-06 7:57 
GeneralRe: BackgroundWorker Pin
Robert Rohde28-Oct-06 19:46
Robert Rohde28-Oct-06 19:46 
Hi,

as said you should find an example where no GUI manipulation is involved. You could make some heavy operations in the thread. Make a Form with a ListBox, two Buttons, a BackgroundWorker and a Progressbar and add the following code:
private void button1_Click(object sender, EventArgs e)
{
    listBox1.Items.Clear();
    progressBar1.Value = 0;
    progressBar1.ForeColor = Color.Green;
    backgroundWorker1.RunWorkerAsync();
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    CalcPrimes(10000000, 10005000, e);
}

private void CalcPrimes(int start, int end, DoWorkEventArgs e)
{
    for (int i = start; i <= end; i++)
    {
        if (backgroundWorker1.CancellationPending)
            return;

        bool isPrime = false;
        if (i % 2 == 1)
        {
            isPrime = true;
            for (int j = 3; j < i / 2; j++)
            {
                if (i % j == 0)
                {
                    isPrime = false;
                    break;
                }
            }
        }
        int p = 100 * (i - start) / (end - start);

        backgroundWorker1.ReportProgress(p, isPrime ? i : 0);
    }
}

private void button2_Click(object sender, EventArgs e)
{
    progressBar1.ForeColor = Color.Red;
    backgroundWorker1.CancelAsync();
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    int primeNumber = (int)e.UserState;
    progressBar1.Value = e.ProgressPercentage;
    if (primeNumber > 0)
        listBox1.Items.Insert(0, primeNumber);
}

This should be a typical scenario for the BackgroundWorker. Don't forget to bind all eventhandlers.

Robert



GeneralRe: BackgroundWorker Pin
freshonlineMax28-Oct-06 20:09
freshonlineMax28-Oct-06 20:09 
GeneralRe: BackgroundWorker Pin
Robert Rohde28-Oct-06 21:46
Robert Rohde28-Oct-06 21:46 
GeneralRe: BackgroundWorker Pin
freshonlineMax29-Oct-06 18:49
freshonlineMax29-Oct-06 18:49 
Questionreentrant call to the SetCurrentCellAddressCore function. Pin
Nadia Monalisa27-Oct-06 18:53
Nadia Monalisa27-Oct-06 18:53 
AnswerRe: reentrant call to the SetCurrentCellAddressCore function. Pin
Nader Elshehabi27-Oct-06 20:47
Nader Elshehabi27-Oct-06 20:47 
GeneralRe: reentrant call to the SetCurrentCellAddressCore function. Pin
Nadia Monalisa29-Oct-06 6:41
Nadia Monalisa29-Oct-06 6:41 
Questionhow to get holding key down to repeat an action Pin
bradsnobar27-Oct-06 14:07
bradsnobar27-Oct-06 14:07 
AnswerRe: how to get holding key down to repeat an action Pin
sam#27-Oct-06 18:21
sam#27-Oct-06 18:21 
GeneralRe: how to get holding key down to repeat an action Pin
bradsnobar27-Oct-06 21:15
bradsnobar27-Oct-06 21:15 
AnswerRe: how to get holding key down to repeat an action Pin
Nader Elshehabi27-Oct-06 20:56
Nader Elshehabi27-Oct-06 20:56 
GeneralRe: how to get holding key down to repeat an action Pin
bradsnobar27-Oct-06 21:15
bradsnobar27-Oct-06 21:15 
GeneralRe: how to get holding key down to repeat an action Pin
Nader Elshehabi27-Oct-06 21:31
Nader Elshehabi27-Oct-06 21:31 
AnswerFound the answer Pin
bradsnobar27-Oct-06 21:20
bradsnobar27-Oct-06 21:20 
GeneralRe: Found the answer Pin
DiegoValdevino28-Oct-06 3:18
DiegoValdevino28-Oct-06 3:18 
Questionstore procedure vs hard coding sql statements Pin
keroed_edmond27-Oct-06 12:07
keroed_edmond27-Oct-06 12:07 
AnswerRe: store procedure vs hard coding sql statements Pin
bradsnobar27-Oct-06 14:20
bradsnobar27-Oct-06 14:20 
AnswerRe: store procedure vs hard coding sql statements Pin
Rob Graham27-Oct-06 14:29
Rob Graham27-Oct-06 14:29 

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.