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
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