Click here to Skip to main content
15,920,633 members
Home / Discussions / C#
   

C#

 
AnswerRe: Changing the background Pin
Malcolm Smart17-Jul-07 3:05
Malcolm Smart17-Jul-07 3:05 
AnswerRe: Changing the background Pin
Vasudevan Deepak Kumar17-Jul-07 4:41
Vasudevan Deepak Kumar17-Jul-07 4:41 
QuestionThreading ??? Pin
Software_Specialist17-Jul-07 2:31
Software_Specialist17-Jul-07 2:31 
AnswerRe: Threading ??? Pin
originSH17-Jul-07 2:41
originSH17-Jul-07 2:41 
GeneralRe: Threading ??? Pin
Software_Specialist17-Jul-07 3:31
Software_Specialist17-Jul-07 3:31 
GeneralRe: Threading ??? Pin
Bekjong17-Jul-07 3:46
Bekjong17-Jul-07 3:46 
GeneralRe: Threading ??? Pin
Software_Specialist17-Jul-07 4:20
Software_Specialist17-Jul-07 4:20 
GeneralRe: Threading ??? Pin
Bekjong17-Jul-07 4:48
Bekjong17-Jul-07 4:48 
So do i need ProgressChanged or RunWorkerCompleted event or no..??

Yes. You're getting the Crossthreading exceptions because you're trying to do an operation on a thread other than the thread the UI control was created on. This isn't allowed in forms. You can use ProgressChanged and RunWorkerCompleted to avoid this. Use the ReportProgress together with the ProgressChanged event to achieve this:



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

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        BackgroundWorker bw = new BackgroundWorker();
        public Form1()
        {
            InitializeComponent();
            
            bw.WorkerReportsProgress = true;
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
            bw.RunWorkerAsync();
        }

        

        void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.Text = "Looped " + e.ProgressPercentage + " times";
        }

        void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Close();
        }

        void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            int i = 0;
            while (i++ < 100)
            {
                bw.ReportProgress(i);
                Thread.Sleep(100);
            }
        }
    }
}


Standards are great! Everybody should have one!

GeneralRe: Threading ??? Pin
Russell Jones17-Jul-07 5:02
Russell Jones17-Jul-07 5:02 
QuestionCould yous give some idea ? Pin
LyBun17-Jul-07 2:31
LyBun17-Jul-07 2:31 
AnswerRe: Could yous give some idea ? Pin
Malcolm Smart17-Jul-07 2:56
Malcolm Smart17-Jul-07 2:56 
AnswerRe: Could yous give some idea ? Pin
Colin Angus Mackay17-Jul-07 3:10
Colin Angus Mackay17-Jul-07 3:10 
QuestionDouble row in TreeView or ListView Pin
Andrea_8617-Jul-07 1:51
Andrea_8617-Jul-07 1:51 
AnswerRe: Double row in TreeView or ListView Pin
m@u17-Jul-07 2:06
m@u17-Jul-07 2:06 
GeneralRe: Double row in TreeView or ListView Pin
Andrea_8617-Jul-07 6:14
Andrea_8617-Jul-07 6:14 
QuestionMSMQ Error Handling Pin
ramdil17-Jul-07 1:38
ramdil17-Jul-07 1:38 
AnswerRe: MSMQ Error Handling Pin
Jimmanuel17-Jul-07 3:55
Jimmanuel17-Jul-07 3:55 
GeneralRe: MSMQ Error Handling Pin
ramdil17-Jul-07 4:24
ramdil17-Jul-07 4:24 
GeneralRe: MSMQ Error Handling Pin
Jimmanuel17-Jul-07 4:35
Jimmanuel17-Jul-07 4:35 
GeneralRe: MSMQ Error Handling Pin
ramdil17-Jul-07 5:35
ramdil17-Jul-07 5:35 
GeneralRe: MSMQ Error Handling Pin
Jimmanuel17-Jul-07 6:10
Jimmanuel17-Jul-07 6:10 
GeneralRe: MSMQ Error Handling Pin
ramdil17-Jul-07 6:23
ramdil17-Jul-07 6:23 
GeneralRe: MSMQ Error Handling [modified] Pin
Jimmanuel17-Jul-07 6:45
Jimmanuel17-Jul-07 6:45 
GeneralRe: MSMQ Error Handling Pin
ramdil17-Jul-07 7:15
ramdil17-Jul-07 7:15 
GeneralRe: MSMQ Error Handling Pin
Jimmanuel17-Jul-07 7:27
Jimmanuel17-Jul-07 7:27 

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.