| | |
multithreading exception
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
Hey guys, I've an issue when it comes to multithreading
i seem to get this error after updating a variable...
Basically I'm trying to use a progress bar indicator to show the applications status. Any ideas of what i'm doing wrong here?
Thanks...
i seem to get this error after updating a variable...
•
•
•
•
Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on.
Thanks...
•
•
Join Date: Aug 2008
Posts: 1,160
Reputation:
Solved Threads: 137
here's something i made from another post with the same question
you need to use invokerequired to be able to do this
you need to use invokerequired to be able to do this
C# Syntax (Toggle Plain Text)
private delegate void UpdatePbDelegate(); private void UpdatePb() { if (this.pbStatus.InvokeRequired) { UpdatePbDelegate updateCount = new UpdatePbDelegate(this.UpdatePb); this.pbStatus.Invoke(updateCount); } else { if(this.pbStatus.Value == 100) { this.pbStatus.Value = 1; } this.pbStatus.PerformStep(); this.pbStatus.Refresh(); } } private void DownloadData() { //your code in here that downloads your values } private void StartingThread() { Thread t= new Thread(new ThreadStart(DownloadData)); t.Start(); while (t.IsAlive) { UpdatePb(); System.Threading.Thread.Sleep(50); } }
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
C# Syntax (Toggle Plain Text)
namespace pb { using System; using System.Drawing; using System.ComponentModel; using System.Windows.Forms; using System.Threading; /// <summary> /// Summary description for Win32Form2. /// </summary> public partial class Win32Form2 : System.Windows.Forms.Form { /// <summary> /// Required by the Win Forms designer /// </summary> private System.ComponentModel.Container components; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Label label1; private System.Windows.Forms.Button button1; private System.Windows.Forms.ProgressBar progressBar1; public Win32Form2() { // Required for Win Form Designer support InitializeComponent(); } /// <summary> /// The main entry point for the application. /// </summary> public static void Main(string[] args) { Application.Run(new Win32Form2()); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with an editor /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.label1 = new System.Windows.Forms.Label(); this.progressBar1 = new System.Windows.Forms.ProgressBar(); this.button1 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); //@design this.TrayHeight = 0; //@design this.TrayLargeIcon = false; //@design this.TrayAutoArrange = true; label1.Location = new System.Drawing.Point(32, 40); label1.Text = "Progress Value"; label1.Size = new System.Drawing.Size(88, 24); label1.TabIndex = 2; progressBar1.Maximum = 100; progressBar1.Location = new System.Drawing.Point(8, 312); progressBar1.Minimum = 0; progressBar1.TabIndex = 0; progressBar1.Value = 0; //We have calculated the excat size which will result in only 20 boxes to be drawn progressBar1.Size = new System.Drawing.Size(520, 40); progressBar1.Step = 1; button1.Location = new System.Drawing.Point(152, 168); button1.Size = new System.Drawing.Size(144, 48); button1.TabIndex = 1; button1.Text = "button1"; button1.Click += new System.EventHandler(button1_Click); textBox1.Location = new System.Drawing.Point(136, 40); textBox1.Text = "0"; textBox1.TabIndex = 3; textBox1.Size = new System.Drawing.Size(184, 20); this.Text = "Win32Form2"; this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(616, 393); this.Click += new System.EventHandler(Win32Form2_Click); this.Controls.Add(textBox1); this.Controls.Add(label1); this.Controls.Add(button1); this.Controls.Add(progressBar1); Thread t = new Thread(new ThreadStart(testing)); t.Start(); } protected void button1_Click(object sender, System.EventArgs e) { //this checking is automatically done as stated in the Ref Documentation //but it does not work , BUGssssss //so we have to do it shhhhh .... if (progressBar1.Value == progressBar1.Maximum) { progressBar1.Value = progressBar1.Minimum; } progressBar1.PerformStep(); textBox1.Text = progressBar1.Value.ToString(); // Displays the values of progressbar in textbox } protected void testing() { while (progressBar1.Value < 100) { if (progressBar1.Value == progressBar1.Maximum) { progressBar1.Value = progressBar1.Minimum; } progressBar1.PerformStep(); textBox1.Text = progressBar1.Value.ToString(); // Displays the values of progressbar in textbox System.Threading.Thread.Sleep(50); } } protected void Win32Form2_Click(object sender, System.EventArgs e) { } }
Where does the update count get implemented? I don't get that...
•
•
Join Date: Aug 2008
Posts: 1,160
Reputation:
Solved Threads: 137
in your method for testing you are updating the pb, anywhere you are updating it just call
C# Syntax (Toggle Plain Text)
UpdatePb();
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
![]() |
Similar Threads
- A few questions about C++ (C++)
- Multithreading.....Help pls (Java)
- Hello there! (Java)
- Multithreading in jsp-HELP!!! (JSP)
Other Threads in the C# Forum
- Previous Thread: How to launch .net application automatically when computer starts up
- Next Thread: pay for c# project help
| Thread Tools | Search this Thread |
.net access algorithm array barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom cyclethruopenforms data database datagrid datagridview dataset date/time datetime degrees development dll draganddrop drawing encryption enum event excel file finalyearproject form format forms function gdi+ getoutlookcontactusinfcsvfile globalization httpwebrequest image index input install installer java label list listbox mandelbrot math mono mouseclick mysql operator panel path photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox save server silverlight sleep socket sql sql-server statistics stream string table text textbox thread time timer timespan update usercontrol users validate validation visualstudio webbrowser wia windows winforms wpf xml






