PC Review


Reply
Thread Tools Rate Thread

Control.Invoke frustration

 
 
ORC
Guest
Posts: n/a
 
      24th Nov 2004
It's a bit difficult for me to understand the threading and control.invoking
issue - I can't see what's wrong with the following code and would highly
appreciate any help ( i'm using C#) (error message is included below code):

namespace Application_for_tests
{
public delegate void UpdateFormDelegate();
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private TestClass testClass = new TestClass();
System.Threading.Thread thread;
System.Threading.ThreadStart threadDelegate;
public UpdateFormDelegate updateFormDelegate;

private bool threadRunning = false;
public Form1()
{
InitializeComponent();
updateFormDelegate = new UpdateFormDelegate(this.updateForm);
}
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(64, 80);
this.textBox1.Size = new System.Drawing.Size(96, 20);
this.textBox1.Text = "textBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(64, 112);
this.button1.Size = new System.Drawing.Size(96, 24);
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.MinimizeBox = false;
this.Text = "Form1";
}

static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
if (!threadRunning)
{
threadDelegate = new System.Threading.ThreadStart(threadFunction);
thread = new System.Threading.Thread(threadDelegate);
thread.Start();
threadRunning = true;
}
}
private void threadFunction()
{
this.Invoke(this.updateFormDelegate);
MessageBox.Show("thread will finish now!");
threadRunning = false;
}

private void updateForm()
{
textBox1.Text = "some text";
}
}
}
******************************************************
An error occurs in the line "this.Invoke(this.updateFormDelegate);" with
following message: "An unhandled exception of type
'System.ArgumentException' occurred in System.Windows.Forms.dll"
"Additional information: ArgumentException".

Thanks,
Ole


 
Reply With Quote
 
 
 
 
Daniel Moth
Guest
Posts: n/a
 
      24th Nov 2004
1. You cannot pass any delegate you want to Invoke; rather, you can only
use the EventHandler.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


"ORC" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> It's a bit difficult for me to understand the threading and
> control.invoking
> issue - I can't see what's wrong with the following code and would highly
> appreciate any help ( i'm using C#) (error message is included below
> code):
>
> namespace Application_for_tests
> {
> public delegate void UpdateFormDelegate();
> public class Form1 : System.Windows.Forms.Form
> {
> private System.Windows.Forms.TextBox textBox1;
> private System.Windows.Forms.Button button1;
> private TestClass testClass = new TestClass();
> System.Threading.Thread thread;
> System.Threading.ThreadStart threadDelegate;
> public UpdateFormDelegate updateFormDelegate;
>
> private bool threadRunning = false;
> public Form1()
> {
> InitializeComponent();
> updateFormDelegate = new UpdateFormDelegate(this.updateForm);
> }
> protected override void Dispose( bool disposing )
> {
> base.Dispose( disposing );
> }
> private void InitializeComponent()
> {
> this.textBox1 = new System.Windows.Forms.TextBox();
> this.button1 = new System.Windows.Forms.Button();
> //
> // textBox1
> //
> this.textBox1.Location = new System.Drawing.Point(64, 80);
> this.textBox1.Size = new System.Drawing.Size(96, 20);
> this.textBox1.Text = "textBox1";
> //
> // button1
> //
> this.button1.Location = new System.Drawing.Point(64, 112);
> this.button1.Size = new System.Drawing.Size(96, 24);
> this.button1.Text = "button1";
> this.button1.Click += new System.EventHandler(this.button1_Click);
> //
> // Form1
> //
> this.Controls.Add(this.button1);
> this.Controls.Add(this.textBox1);
> this.MinimizeBox = false;
> this.Text = "Form1";
> }
>
> static void Main()
> {
> Application.Run(new Form1());
> }
> private void button1_Click(object sender, System.EventArgs e)
> {
> if (!threadRunning)
> {
> threadDelegate = new System.Threading.ThreadStart(threadFunction);
> thread = new System.Threading.Thread(threadDelegate);
> thread.Start();
> threadRunning = true;
> }
> }
> private void threadFunction()
> {
> this.Invoke(this.updateFormDelegate);
> MessageBox.Show("thread will finish now!");
> threadRunning = false;
> }
>
> private void updateForm()
> {
> textBox1.Text = "some text";
> }
> }
> }
> ******************************************************
> An error occurs in the line "this.Invoke(this.updateFormDelegate);" with
> following message: "An unhandled exception of type
> 'System.ArgumentException' occurred in System.Windows.Forms.dll"
> "Additional information: ArgumentException".
>
> Thanks,
> Ole
>
>



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tree control focus frustration Andrew Lighten Microsoft Dot NET Framework Forms 3 24th Jun 2006 02:48 AM
Control.Invoke Cool Guy Microsoft Dot NET Framework 1 7th Jun 2005 06:46 PM
need for control invoke Stelrad Doulton Microsoft Dot NET Compact Framework 2 27th Apr 2005 11:46 AM
question anbout control.invoke and control.begininvoke C# Microsoft Dot NET Framework Forms 4 24th May 2004 09:45 AM
Using Control.Invoke Aaron Microsoft Dot NET Framework Forms 0 19th Aug 2003 05:45 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:20 PM.