Click here to Skip to main content
15,901,283 members
Home / Discussions / C#
   

C#

 
AnswerRe: Difference between C and Visual C. Pin
Not Active24-Mar-10 3:05
mentorNot Active24-Mar-10 3:05 
GeneralRe: Difference between C and Visual C. Pin
Som Shekhar24-Mar-10 4:42
Som Shekhar24-Mar-10 4:42 
GeneralRe: Difference between C and Visual C. Pin
Pete O'Hanlon24-Mar-10 7:48
mvePete O'Hanlon24-Mar-10 7:48 
GeneralRe: Difference between C and Visual C. Pin
Som Shekhar24-Mar-10 7:53
Som Shekhar24-Mar-10 7:53 
AnswerRe: Difference between C and Visual C. Pin
yu-jian26-Mar-10 7:14
yu-jian26-Mar-10 7:14 
QuestionWorking with the UI while multithreading Pin
Islorvat24-Mar-10 3:02
Islorvat24-Mar-10 3:02 
AnswerRe: Working with the UI while multithreading Pin
Luc Pattyn24-Mar-10 3:31
sitebuilderLuc Pattyn24-Mar-10 3:31 
AnswerRe: Working with the UI while multithreading Pin
Tim Weckx24-Mar-10 3:58
Tim Weckx24-Mar-10 3:58 
The easiest way to multithread in a WinForms application is by using a BackgroundWorker. With it, you don't need to worry about InvokeRequired and Control.Invoke, it takes care of this for you. You can get more details on the MSDN website (http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx[^]) , but below is a simple example.

private void button1_Click(object sender, EventArgs e)
        {
            // Create a new backgroundworker that supports progressreporting and cancellation.
            BackgroundWorker worker = new BackgroundWorker();
            worker.WorkerReportsProgress = true;
            worker.WorkerSupportsCancellation = true;

            // Hook up the events that will do the actual work
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);

            // Start the background worker
            worker.RunWorkerAsync();
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // When an exception happens in the do_work eventhandler,
            // the RunWorkerCompleted is called with the exception passed on the argument.
            // Always check for this
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
            else if (e.Cancelled)
            {
                // show the user that the background worker was cancelled
            }
            else
            {
                // show the user that the worker completed succesfully
            }
        }

        void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = (BackgroundWorker)sender;

            // perform the time consuming task
            for (int i = 1; i <= 100; i++)
            {
                if (!worker.CancellationPending) // this is only needed if your task can be cancelled in the middle of the run
                {
                    Thread.Sleep(50); // do the actual task
                    worker.ReportProgress(i);
                }
                else
                {
                    e.Cancel = true;
                    break;
                }
            }
        }

GeneralRe: Working with the UI while multithreading Pin
Islorvat24-Mar-10 4:27
Islorvat24-Mar-10 4:27 
QuestionNews feed popup desktop application Pin
Sherif W. Girgis24-Mar-10 2:39
Sherif W. Girgis24-Mar-10 2:39 
AnswerRe: News feed popup desktop application Pin
Not Active24-Mar-10 3:03
mentorNot Active24-Mar-10 3:03 
QuestionTo save the file Pin
v17.poornima24-Mar-10 2:01
v17.poornima24-Mar-10 2:01 
AnswerRe: To save the file Pin
Richard MacCutchan24-Mar-10 3:43
mveRichard MacCutchan24-Mar-10 3:43 
Questionembedd flash in winform in mono .net Pin
melihcicek24-Mar-10 2:01
melihcicek24-Mar-10 2:01 
AnswerRe: embedd flash in winform in mono .net Pin
CDP180224-Mar-10 2:13
CDP180224-Mar-10 2:13 
GeneralRe: embedd flash in winform in mono .net Pin
melihcicek24-Mar-10 2:49
melihcicek24-Mar-10 2:49 
GeneralRe: embedd flash in winform in mono .net Pin
Tony Richards24-Mar-10 6:42
Tony Richards24-Mar-10 6:42 
Questionvoice recognition Pin
kingsto24-Mar-10 1:04
kingsto24-Mar-10 1:04 
AnswerRe: voice recognition Pin
Rob Philpott24-Mar-10 1:11
Rob Philpott24-Mar-10 1:11 
AnswerRe: voice recognition PinPopular
OriginalGriff24-Mar-10 1:13
mveOriginalGriff24-Mar-10 1:13 
AnswerRe: voice recognition Pin
Keith Barrow24-Mar-10 1:17
professionalKeith Barrow24-Mar-10 1:17 
JokeRe: voice recognition Pin
Calla24-Mar-10 1:56
Calla24-Mar-10 1:56 
QuestionAdd Custom Color in ColorDialog Pin
Anubhava Dimri23-Mar-10 23:44
Anubhava Dimri23-Mar-10 23:44 
AnswerRe: Add Custom Color in ColorDialog Pin
sanforjackass24-Mar-10 0:21
sanforjackass24-Mar-10 0:21 
GeneralRe: Add Custom Color in ColorDialog Pin
Anubhava Dimri24-Mar-10 0:50
Anubhava Dimri24-Mar-10 0:50 

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.