bk13
Hello,
I have made the change, but I still have the problem that I mentioned earlier. There is no path for the new value created in the spawned thread to get to the text box. I tried to add a parameter to procedure UpdateBar and consequently to the delegate code. There is obviously someing I am missing as I get two errors, both associated with this line:
Without the argument I get a message about mismatch in arguments. With the argument the first error message says The best overloaded method match for 'declaration' has some invalid arguments |
|
and the other says Argument '1': cannot convert from 'void' to 'System.Delegate' |
|
I have checked the arguments and all are of the same type. The code is below. I have edited in comments as I understand what is going on. Please let me know if I need to change these comments. I have also added the text ERROR POINT as a comment at the offending line of code.
As I found in another thread, it does help to paste the code into an editor, the paste that into here. The process keeps some resemblense of formatting.
| |
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 ThreadWinApp { public partial class Form1 : Form { // private Thread trd; // Delegates have 3 parts // 1) the type of signature of the method it can point to // 2) The reference use to call a method // 3) The method that is referenced and runs when called via the delegate // I think this if part 1 of the delegate concept. public delegate void UpdateDel( int new_value ); // this procedure will update the progress bar in the window. // this is part 3 of the delegate construct. The procedure that will // run when the delegate is called (but calling is named invoke) private void UpdateBar( int new_value ) { // If this procedure was called from a thread that is not where // the procedure was declared/created (right wording ), then // InvokeRequired will be true meaning that this procedure cannot // be directly called. It must be called via a delegate. if (InvokeRequired) { // This procedure appears to be assigning itself as the delegate. // I suspect that the Invoke(d) essentially causes the UpdateBar // to be called from the original thread. In that event, the // if statement will not be taken, and the progress bar will be // udated. // I don't understand what is going on here. // this is part 2 of the delegate concept. A delegate named "d" // is created and associated with the function that will eventually // be called. UpdateDel d = new UpdateDel(UpdateBar); // Delegate invocation. Call the function using the delegate. Invoke(d( new_value ) ); // << ERROR POINT << return; } // This is the statement we want to be executed to update the // progress bar. this.progressBar1.Value = new_value; } public Form1() { InitializeComponent(); CheckForIllegalCrossThreadCalls = false; } // Give the button something to show it is an active component. private void button1_Click(object sender, EventArgs e) { MessageBox.Show("This is the main thread"); } // this is the task to be spawned off. When the form is loaded // function Form1_Load is called and it starts this function as // a separated task. It will calculate a new value for the // progress bar, update the prgress bar, then sleep for 0.2 seconds // BUT: UpdateBar will not be running within the same thread as // this function. That is handled elsewhere. private void ThreadTask() { int stp; int newval; Random rnd = new Random(); while (true) { stp = this.progressBar1.Step * rnd.Next(-1, 2); newval = this.progressBar1.Value + stp; if (newval > this.progressBar1.Maximum) newval = this.progressBar1.Maximum; else if (newval < this.progressBar1.Minimum) newval = this.progressBar1.Minimum; UpdateBar( newval ); // change from frederikm //this.progressBar1.Value = newval; Thread.Sleep(100); } } private void Form1_Load(object sender, EventArgs e) { Thread trd = new Thread(new ThreadStart(this.ThreadTask)); trd.IsBackground = true; trd.Start(); }
} }
|