Click here to Skip to main content
15,887,746 members
Home / Discussions / C#
   

C#

 
GeneralRe: rename listbox item Pin
Luc Pattyn3-Feb-10 23:58
sitebuilderLuc Pattyn3-Feb-10 23:58 
AnswerRe: rename listbox item Pin
ScottM13-Feb-10 21:25
ScottM13-Feb-10 21:25 
Questionmanaged code that will do the same as SetForegroundWindow Pin
kevinnicol3-Feb-10 8:10
kevinnicol3-Feb-10 8:10 
AnswerRe: managed code that will do the same as SetForegroundWindow Pin
DaveyM693-Feb-10 8:22
professionalDaveyM693-Feb-10 8:22 
GeneralRe: managed code that will do the same as SetForegroundWindow Pin
kevinnicol3-Feb-10 8:38
kevinnicol3-Feb-10 8:38 
QuestionMessage Removed Pin
3-Feb-10 7:08
attalurisubbu3-Feb-10 7:08 
AnswerRe: Need to display the page number as "2" from second page Onwards. Pin
Not Active3-Feb-10 7:37
mentorNot Active3-Feb-10 7:37 
Questiontrying to understand classes and threads using the progress bar in c# Pin
tonyonlinux3-Feb-10 6:15
tonyonlinux3-Feb-10 6:15 
I'm trying to make a class that will simply put the progress bar in a loop (just to see how it works for learning purposes). I keep getting the following error though and I don't understand why.

error
Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on. 


here is the class code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms; // remove after testing used to show message boxes
using System.Threading;

namespace WindowsFormsApplication1
{



    public partial class thread
    {
        //Properties
        private thread trd;
        private ProgressBar _threadProgressBar;

        public ProgressBar ThreadProgressBar
        {
            set { _threadProgressBar = value; }
            get { return _threadProgressBar; }
        }
        public void startthread()
        {
            
            
            Thread trd = new Thread(new ThreadStart(this.ThreadTask));
            trd.IsBackground = true;
            trd.Start();
        }



        private void ThreadTask()
        {

            int stp;
            int newval;
            Random rnd = new Random();

            while (true)
            {
                stp = ThreadProgressBar.Step * rnd.Next(-1, 2);
                newval = ThreadProgressBar.Value + stp;

                if (newval > ThreadProgressBar.Maximum)
                    newval = ThreadProgressBar.Maximum;
                else if (newval < ThreadProgressBar.Minimum)
                    newval = ThreadProgressBar.Minimum;

                ThreadProgressBar.Value = newval;

                Thread.Sleep(100);
            }
        }
    }
}

i'm calling it inside a windows form
with
public void callthread()
        {
            thread mythread = new thread();
            mythread.ThreadProgressBar = progressBar1;
            mythread.startthread();
        }


thanks
AnswerRe: trying to understand classes and threads using the progress bar in c# Pin
Luc Pattyn3-Feb-10 6:28
sitebuilderLuc Pattyn3-Feb-10 6:28 
GeneralRe: trying to understand classes and threads using the progress bar in c# Pin
Not Active3-Feb-10 6:33
mentorNot Active3-Feb-10 6:33 
GeneralRe: trying to understand classes and threads using the progress bar in c# Pin
Luc Pattyn3-Feb-10 6:42
sitebuilderLuc Pattyn3-Feb-10 6:42 
GeneralRe: trying to understand classes and threads using the progress bar in c# Pin
tonyonlinux3-Feb-10 10:18
tonyonlinux3-Feb-10 10:18 
QuestionHow to bind control’s properties to variables in C#? Pin
PomaPomaPoma3-Feb-10 5:48
PomaPomaPoma3-Feb-10 5:48 
AnswerRe: How to bind control’s properties to variables in C#? Pin
Luc Pattyn3-Feb-10 9:23
sitebuilderLuc Pattyn3-Feb-10 9:23 
GeneralRe: How to bind control’s properties to variables in C#? Pin
PomaPomaPoma3-Feb-10 9:52
PomaPomaPoma3-Feb-10 9:52 
GeneralRe: How to bind control’s properties to variables in C#? Pin
Luc Pattyn3-Feb-10 9:59
sitebuilderLuc Pattyn3-Feb-10 9:59 
GeneralRe: How to bind control’s properties to variables in C#? Pin
PomaPomaPoma3-Feb-10 11:12
PomaPomaPoma3-Feb-10 11:12 
AnswerRe: How to bind control’s properties to variables in C#? Pin
Not Active3-Feb-10 9:32
mentorNot Active3-Feb-10 9:32 
GeneralRe: How to bind control’s properties to variables in C#? Pin
PomaPomaPoma3-Feb-10 10:09
PomaPomaPoma3-Feb-10 10:09 
GeneralRe: How to bind control’s properties to variables in C#? Pin
Not Active3-Feb-10 10:17
mentorNot Active3-Feb-10 10:17 
QuestionC# code clean up and improvement Pin
eeffoc423-Feb-10 5:20
eeffoc423-Feb-10 5:20 
AnswerRe: C# code clean up and improvement Pin
Luc Pattyn3-Feb-10 5:26
sitebuilderLuc Pattyn3-Feb-10 5:26 
GeneralRe: C# code clean up and improvement [modified] Pin
eeffoc423-Feb-10 5:50
eeffoc423-Feb-10 5:50 
GeneralRe: C# code clean up and improvement Pin
Luc Pattyn3-Feb-10 6:26
sitebuilderLuc Pattyn3-Feb-10 6:26 
GeneralRe: C# code clean up and improvement Pin
eeffoc423-Feb-10 9:17
eeffoc423-Feb-10 9:17 

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.