Control.Invoke causes ArgumentException

A

Amit Chopra

An interesting Q&A worth sharing with the community.

I am trying to update a form from a work thread and cant get Control.Invoke
to work, it always excepts with ArgumentException but cant see whats wrong.
private void UpdateProg(object o, EventArgs e)
{ progressBar1.Value++;
}

private delegate void myDelegate(object o, EventArgs e);
private void MyThread()
{ for (int i = 0; i<100;i++)
{
this.Invoke(new myDelegate(UpdateProg));
Thread.Sleep(500);
}
}
private void button1_Click(object sender, System.EventArgs e)
{ new Thread(new
ThreadStart(MyThread)).Start();
}

Suggestion:

You need to either use EventHandler (or) a delegate that takes void
parameter list. In your case replace 'myDelegate' with 'EventHandler' and
it should work fine
An example of this could would be

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;

public class Form1 : System.Windows.Forms.Form
{
Label lbl = new Label();
Button btn = new Button();
public bool fAlive;
int cHits = 0;
ArrayList m_alth = new ArrayList();

public Form1()
{
InitializeComponent();
}

private void InitializeComponent()
{
this.Text = "Form1";

lbl.Text = "Thread1";
this.Controls.Add(lbl);

btn.Location = new Point(50, 50);
btn.Text = "Start";
btn.Click += new EventHandler(this.OnStop);
this.Controls.Add(btn);
this.Closing += new CancelEventHandler(this.OnClosing);
this.ControlBox = true;
this.MinimizeBox = false;

}

private void OnStop(object sender, EventArgs e)
{
if (fAlive)
{
fAlive = false;
btn.Text = "Start";
m_alth.Clear();
}
else
{
fAlive = true;
btn.Text = "Stop";
Thread th;
for (int i=0; i<1; i++)
{
th = new Thread(new ThreadStart(new ThreadRunner(this, i+1).ThreadRun));
th.Start();
m_alth.Add(th);
}
}
}

private void OnClosing(object sender, CancelEventArgs e)
{
Console.WriteLine("inside dest");

foreach(Object obj in m_alth)
{
Thread th = (Thread)obj;
Console.WriteLine("stopping " + th);
//th.Abort();
}

fAlive = false;
//base.Dispose();
}


public void OnTextChange(object sender, EventArgs e)
{
this.cHits++;
this.Text = (cHits) + "Thread - " /*+ sender*/;
//this.Refresh();
}

static void Main()
{
Application.Run(new Form1());
}
}

public class ThreadRunner
{
Form1 m_frm = null;
public int m_id;

public ThreadRunner(Form1 frm1, int id)
{
m_frm = frm1;
m_id = id;
}

public void ThreadRun()
{
while(m_frm.fAlive)
{
Console.WriteLine("heart beat from: " + m_id);
m_frm.Invoke(new EventHandler(m_frm.OnTextChange));
Thread.Sleep(300);
}
}
}


Amit Chopra
Visual Studio for Devices
Microsoft Corp.
 
A

Alex Feinman [MVP]

Um, this is asked and answered approximately once a week. Perhaps the
documentation could be fixed...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top