Returning value from MT - need help

J

Jozef

Hi!

In class1.cs i have something like this:

public class Class1
{
int b = 0;

public Class1()
{
}

public void DoSomething()
{
do {
b++;
Thread.Sleep(1000);
} while (true);

}

}

in Form1 i have a label control and i want to show values that from DoSomething() in class1.cs

private void Form1_Load(...)
{
Class1 obj = new Class1();

Thread wrk = new Thread(new ThreadStart(obj.DoSomething));
wrk.Start();

}


how to update gui, i.e. label control on form to show values from thread every second?
 
A

Arne Vajhøj

In class1.cs i have something like this:

public class Class1
{
int b = 0;

public Class1()
{
}

public void DoSomething()
{
do {
b++;
Thread.Sleep(1000);
} while (true);

}

}

in Form1 i have a label control and i want to show values that from
DoSomething() in class1.cs

private void Form1_Load(...)
{
Class1 obj = new Class1();

Thread wrk = new Thread(new ThreadStart(obj.DoSomething));
wrk.Start();

}

how to update gui, i.e. label control on form to show values from thread
every second?

Class1 should have a ref to Form1 and update the GUI via Invoke.

For inspiration:

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

public class MainForm : Form
{
private Label time;
private Button start;
private Button abort;
private Thread t;

public MainForm()
{
time = new Label();
start = new Button();
abort = new Button();
SuspendLayout();
time.Location = new Point(50, 50);
time.Size = new Size(300, 50);
time.Font = new Font(FontFamily.GenericSerif, 16.0f);
start.Location = new Point(50,150);
start.Size = new Size(200, 50);
start.Name = "Start Button";
start.Text = "Start clock";
start.Click += new EventHandler(StartClick);
abort.Location = new Point(50,250);
abort.Size = new Size(200, 50);
abort.Name = "Abort Button";
abort.Text = "Abort clock";
abort.Click += new EventHandler(AbortClick);
ClientSize = new Size(400, 350);
Controls.Add(time);
Controls.Add(start);
Controls.Add(abort);
Name = "Main Form";
Text = "Main Form";
ResumeLayout(false);
}
public void UpdateTime()
{
time.Text = DateTime.Now.ToString();
}
public delegate void UpdateTimeHandler();
public void Run()
{
while(true)
{
if(time.InvokeRequired)
{
time.Invoke(new UpdateTimeHandler(UpdateTime));
}
else
{
Update();
}
Thread.Sleep(1000);
}
}
public void StartClick(object sender, EventArgs e)
{
t = new Thread(new ThreadStart(Run));
t.Start();
}
public void AbortClick(object sender, EventArgs e)
{
t.Abort();
}
[STAThread]
public static void Main(string[] args)
{
Application.Run(new MainForm());
Application.Exit();
Environment.Exit(0);
}
}

Arne
 
J

Jozef

On 25.1.2011 22:38, Arne Vajhøj wrote:

hm... I'm not sure that i get idea from your code. I need just to
modify my code that i put here.
I want to return value from method DoSomething() to any control
on form that i want (label, textbox)...
 
A

Arne Vajhøj

hm... I'm not sure that i get idea from your code. I need just to
modify my code that i put here.
I want to return value from method DoSomething() to any control
on form that i want (label, textbox)...

Send a ref to the Form1 over in Class1 constructor
and save it in a field so DoSomething can get it.
The DoSomething uses that ref and the Invoke method
(due to being in a separate thread) to do the update.

Arne
 
J

Jozef

Send a ref to the Form1 over in Class1 constructor
and save it in a field so DoSomething can get it.
The DoSomething uses that ref and the Invoke method
(due to being in a separate thread) to do the update.

Arne

Could you simplfy this how to send a ref?
I'm not expert in C#.

Thank you!
 

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