multithreading exception

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

multithreading exception

 
0
  #1
Nov 14th, 2008
Hey guys, I've an issue when it comes to multithreading

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.
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...
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,160
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: multithreading exception

 
0
  #2
Nov 14th, 2008
here's something i made from another post with the same question

you need to use invokerequired to be able to do this

  1. private delegate void UpdatePbDelegate();
  2. private void UpdatePb()
  3. {
  4. if (this.pbStatus.InvokeRequired)
  5. {
  6. UpdatePbDelegate updateCount = new UpdatePbDelegate(this.UpdatePb);
  7. this.pbStatus.Invoke(updateCount);
  8. }
  9. else
  10. {
  11. if(this.pbStatus.Value == 100)
  12. {
  13. this.pbStatus.Value = 1;
  14. }
  15. this.pbStatus.PerformStep();
  16. this.pbStatus.Refresh();
  17. }
  18. }
  19.  
  20.  
  21. private void DownloadData()
  22. {
  23. //your code in here that downloads your values
  24. }
  25.  
  26. private void StartingThread()
  27. {
  28. Thread t= new Thread(new ThreadStart(DownloadData));
  29. t.Start();
  30.  
  31. while (t.IsAlive)
  32. {
  33. UpdatePb();
  34. System.Threading.Thread.Sleep(50);
  35. }
  36. }
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: multithreading exception

 
0
  #3
Nov 15th, 2008
  1.  
  2. namespace pb
  3. {
  4. using System;
  5. using System.Drawing;
  6. using System.ComponentModel;
  7. using System.Windows.Forms;
  8. using System.Threading;
  9.  
  10.  
  11. /// <summary>
  12. /// Summary description for Win32Form2.
  13. /// </summary>
  14. public partial class Win32Form2 : System.Windows.Forms.Form
  15. {
  16.  
  17. /// <summary>
  18. /// Required by the Win Forms designer
  19. /// </summary>
  20. private System.ComponentModel.Container components;
  21. private System.Windows.Forms.TextBox textBox1;
  22. private System.Windows.Forms.Label label1;
  23. private System.Windows.Forms.Button button1;
  24. private System.Windows.Forms.ProgressBar progressBar1;
  25.  
  26. public Win32Form2()
  27. {
  28. // Required for Win Form Designer support
  29. InitializeComponent();
  30. }
  31.  
  32. /// <summary>
  33. /// The main entry point for the application.
  34. /// </summary>
  35. public static void Main(string[] args)
  36. {
  37. Application.Run(new Win32Form2());
  38. }
  39.  
  40. /// <summary>
  41. /// Required method for Designer support - do not modify
  42. /// the contents of this method with an editor
  43. /// </summary>
  44. private void InitializeComponent()
  45. {
  46. this.components = new System.ComponentModel.Container();
  47. this.label1 = new System.Windows.Forms.Label();
  48. this.progressBar1 = new System.Windows.Forms.ProgressBar();
  49. this.button1 = new System.Windows.Forms.Button();
  50. this.textBox1 = new System.Windows.Forms.TextBox();
  51.  
  52. //@design this.TrayHeight = 0;
  53. //@design this.TrayLargeIcon = false;
  54. //@design this.TrayAutoArrange = true;
  55. label1.Location = new System.Drawing.Point(32, 40);
  56. label1.Text = "Progress Value";
  57. label1.Size = new System.Drawing.Size(88, 24);
  58. label1.TabIndex = 2;
  59.  
  60. progressBar1.Maximum = 100;
  61. progressBar1.Location = new System.Drawing.Point(8, 312);
  62. progressBar1.Minimum = 0;
  63. progressBar1.TabIndex = 0;
  64. progressBar1.Value = 0;
  65.  
  66. //We have calculated the excat size which will result in only 20 boxes to be drawn
  67.  
  68. progressBar1.Size = new System.Drawing.Size(520, 40);
  69. progressBar1.Step = 1;
  70.  
  71. button1.Location = new System.Drawing.Point(152, 168);
  72. button1.Size = new System.Drawing.Size(144, 48);
  73. button1.TabIndex = 1;
  74. button1.Text = "button1";
  75. button1.Click += new System.EventHandler(button1_Click);
  76.  
  77. textBox1.Location = new System.Drawing.Point(136, 40);
  78. textBox1.Text = "0";
  79. textBox1.TabIndex = 3;
  80. textBox1.Size = new System.Drawing.Size(184, 20);
  81. this.Text = "Win32Form2";
  82. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  83. this.ClientSize = new System.Drawing.Size(616, 393);
  84. this.Click += new System.EventHandler(Win32Form2_Click);
  85.  
  86. this.Controls.Add(textBox1);
  87. this.Controls.Add(label1);
  88. this.Controls.Add(button1);
  89. this.Controls.Add(progressBar1);
  90. Thread t = new Thread(new ThreadStart(testing));
  91. t.Start();
  92.  
  93. }
  94.  
  95. protected void button1_Click(object sender, System.EventArgs e)
  96. {
  97.  
  98. //this checking is automatically done as stated in the Ref Documentation
  99. //but it does not work , BUGssssss
  100. //so we have to do it shhhhh ....
  101. if (progressBar1.Value == progressBar1.Maximum)
  102. {
  103. progressBar1.Value = progressBar1.Minimum;
  104. }
  105. progressBar1.PerformStep();
  106. textBox1.Text = progressBar1.Value.ToString(); // Displays the values of progressbar in textbox
  107.  
  108. }
  109.  
  110. protected void testing()
  111. {
  112. while (progressBar1.Value < 100)
  113. {
  114. if (progressBar1.Value == progressBar1.Maximum)
  115. {
  116. progressBar1.Value = progressBar1.Minimum;
  117. }
  118. progressBar1.PerformStep();
  119. textBox1.Text = progressBar1.Value.ToString(); // Displays the values of progressbar in textbox
  120. System.Threading.Thread.Sleep(50);
  121. }
  122.  
  123. }
  124. protected void Win32Form2_Click(object sender, System.EventArgs e)
  125. {
  126. }
  127. }

Where does the update count get implemented? I don't get that...
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,160
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: multithreading exception

 
0
  #4
Nov 15th, 2008
in your method for testing you are updating the pb, anywhere you are updating it just call

  1. UpdatePb();
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC