Is Control.Invoke supported?

  • Thread starter Thread starter Ron Lemire
  • Start date Start date
R

Ron Lemire

Hi,

The Visual Studio .NET 2003 documentation says that .NET Compact Framework -
Windows CE .NET supports Control.Invoke (i.e. accessing windows forms
controls from a thread they were not created on).

I'm trying to use Control.Invoke on a Pocket PC 2002 device and I get an
"Argument Exception" managed error.

Has anyone been successful with Control.Invoke and if so do you have some
sample code? My code works on the desktop but not on the Pocket PC.

thanks...Ron Lemire
 
It works, but can only be used to invoke EventHandlers. Trying to invoke
any other method signature will fail.
 
Thanks.
Did a Google search and came up with an article in Italian that has a
working code example in English (that's what counts) that used EventHandler.
I had been using my own delegate.

private void button1_Click(object sender, System.EventArgs e)

{

// Create the worker thread and start it

ThreadStart starter = new ThreadStart(this.UpdateTextBox);

Thread t = new Thread(starter);

t.Start();


}

public void UpdateTextBox()

{

// Set the message to be added to the ListBox from the worker

// thread

this.Message = "ron";

// Invoke the WorkerUpdate method in the ListBox's thread

// context

this.textBox1.Invoke(new EventHandler(WorkerUpdate));

}

// The delegate that's called from the worker thread

// to update the ListBox

public void WorkerUpdate(object sender, EventArgs e)

{

this.textBox1.Text = this.Message;

this.textBox1.Update();

}
 
Most of the time it pays doing google search on this newsgroup. This
particular question is asked and answered approximately every other week
 
You need to either use EventHandler (or) a delegate that takes void
parameter list.

I have also attached a sample below that illustrates multi threaded GUI
application.



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, System.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);
}
}
}




This posting is provided "AS IS" with no warranties, and confers no rights.
 
OK but this code works if Form1 is really main form (it has inside static
void Main()). If I have Form2 and:

public class Form1:
{
private void Button1_Click(object sender, System.EventArgs e)
{
Form2 form2 = new Form2();
form2.ShowDialog()
}
}

and I try using in form2 Invoke it doesn't work!
m_frm.Invoke(new EventHandler(m_frm.OnTextChange));

Arek
 
Which version of NETCF are you running? can you upgrade and see whether you
are still seeing this?

There used to be issue with dialog and cross thread invokation in V1

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hmmm. I'm using HHC with Pocket 2002 without .NET CF SP 1, Pocket PC 2002
Emulator, Pocket PC 2003 - it doesn't work. But when I install today on HHC
..NET CF SP 1 it start works - but only on HHC. Emulators (2002 and 2003)
still doesn't work :-(

Arek.
 
Back
Top