Control.Invoke in CF 1.0

H

hassanmushtaq

hi
i m using VS 2003 (C#) CF 1.0. i have a problem while accessing any
control from any other thread. i see alot of examples from web but it
rather always gives exceptions Argument exception. now my code didn't
give any exception but it is not working as i code to.

i have a form having a list box and button on it.

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.MainMenu mainMenu1;
private Thread t;


public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
//
// listBox1
//
this.listBox1.Location = new System.Drawing.Point(56, 24);
this.listBox1.Size = new System.Drawing.Size(100, 114);
//
// button1
//
this.button1.Location = new System.Drawing.Point(72, 168);
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Menu = this.mainMenu1;
this.Text = "Form1";

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>

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

private void button1_Click(object sender, System.EventArgs e)
{
cThread tt = new cThread();
tt.th = this.t;
t = new Thread(new ThreadStart(tt.Starts));
t.Start();
}
//public delegate void InsertintoList(object sender,
System.EventArgs e);

public void InsertList()
{
this.listBox1.Invoke(new EventHandler(this.Inserting));
}
protected void Inserting(object sender, System.EventArgs e)
{
this.listBox1.Items.Add("hassan");
//MessageBox.Show(this.listBox1.ToString());
}
}

and i have a class named cThread

using System;
using System.Data;
using System.Windows.Forms;
using System.Threading;

namespace TestingInvoke
{
/// <summary>
/// Summary description for cThread.
/// </summary>
public class cThread
{
public Thread th;
private bool stop;
public cThread()
{
//
// TODO: Add constructor logic here
//
}

public void Starts()
{
int i=0;
try
{
while(true)
{
Thread.Sleep(100);
new Form1().InsertList();
if(stop){break;}
i++;
if(i > 5) { this.Stop();}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

public void Stop()
{
this.stop=true;
}
}
}

in that cThread class i want to insert item in list. it was not doing
anything neither give me exception nor inserting the items.
plz help me
hassan mushtaq
 
J

Jeffrey.M.Newman

hi
i m using VS 2003 (C#) CF 1.0. i have a problem while accessing any
control from any other thread. i see alot of examples from web but it
rather always gives exceptions Argument exception. now my code didn't
give any exception but it is not working as i code to.

i have a form having a list box and button on it.

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.MainMenu mainMenu1;
private Thread t;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
//
// listBox1
//
this.listBox1.Location = new System.Drawing.Point(56, 24);
this.listBox1.Size = new System.Drawing.Size(100, 114);
//
// button1
//
this.button1.Location = new System.Drawing.Point(72, 168);
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Menu = this.mainMenu1;
this.Text = "Form1";

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>

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

private void button1_Click(object sender, System.EventArgs e)
{
cThread tt = new cThread();
tt.th = this.t;
t = new Thread(new ThreadStart(tt.Starts));
t.Start();
}
//public delegate void InsertintoList(object sender,
System.EventArgs e);

public void InsertList()
{
this.listBox1.Invoke(new EventHandler(this.Inserting));
}
protected void Inserting(object sender, System.EventArgs e)
{
this.listBox1.Items.Add("hassan");
//MessageBox.Show(this.listBox1.ToString());
}
}

and i have a class named cThread

using System;
using System.Data;
using System.Windows.Forms;
using System.Threading;

namespace TestingInvoke
{
/// <summary>
/// Summary description for cThread.
/// </summary>
public class cThread
{
public Thread th;
private bool stop;
public cThread()
{
//
// TODO: Add constructor logic here
//
}

public void Starts()
{
int i=0;
try
{
while(true)
{
Thread.Sleep(100);
new Form1().InsertList();
if(stop){break;}
i++;
if(i > 5) { this.Stop();}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

public void Stop()
{
this.stop=true;
}
}

}

in that cThread class i want to insert item in list. it was not doing
anything neither give me exception nor inserting the items.
plz help me
hassan mushtaq


The form that is being displayed in your program is created in the
line

Application.Run(new Form1());

You then create another form with

new Form1().InsertList();

InsertList() has to be called on the form that is in
Application.Run().
 
H

hassanmushtaq

The form that is being displayed in your program is created in the
line

Application.Run(new Form1());

You then create another form with

new Form1().InsertList();

InsertList() has to be called on the form that is in
Application.Run().- Hide quoted text -

- Show quoted text -

hi,
i could understand how can call InsertList() method in my Form class.
i have to insert item in listbox from the thread class.
so plz give me solution
regardz
hassan mushtaq
 
G

Guest

Jeffrey gave you the solution. Don't create a new Form1 when you want to
add items to the list. Use the original instance.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com
 
H

hassanmushtaq

Jeffrey gave you the solution. Don't create a new Form1 when you want to
add items to the list. Use the original instance.

--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded Worldwww.OpenNETCF.com







- Show quoted text -

thnx for response. u reply me that not to create a new instance of
Form1. then hoe can i access that funtion in form1 class. either i
make it static function that i called it from class name. or what any
suggestion
regardz
hassan mushtaq
 
J

Jeff Newman

thnx for response. u reply me that not to create a new instance of
Form1. then hoe can i access that funtion in form1 class. either i
make it static function that i called it from class name. or what any
suggestion
regardz
hassan mushtaq

There are plenty of different ways to do this. Try changing the
constructor of cThread to take in a reference to a form, then create
the thread as

cThread tt = new cThread(this);

Store the form in the thread, and use it as needed.
 

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