Threading problem
Jeg har en proces der skal opdatere min GUI, men får denne exception:Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on.
Proces.cs >>
//declaring the event handler delegate
public delegate void ValueChangedEventHandler(object source, double Value);
public class Process
{
bool isAlive = true;
public Process()
{
Thread thread = new Thread(new ThreadStart(Run));
thread.Start();
}
//declaring the event
public event ValueChangedEventHandler ValueChanged;
public void Run()
{
Vaegt_Fase fase = new Vaegt_Fase();
while (isAlive)
{
Vaegt_Fase.VaegtFase_Vaerdier current_Fase = fase.Run(15,10);
if (current_Fase.State == FaseState.Approved)
ValueChanged(this, current_Fase.Value);
/*if (current_Fase.State == FaseState.NotApproved)
ValueChanged(this, current_Fase.Value);*/
if (current_Fase.State == FaseState.Working)
ValueChanged(this, current_Fase.Value);
Thread.Sleep(1000);
}
}
}
Form1.cs >>
public partial class Form1 : Form
{
Entity.Process proces;
public Form1()
{
InitializeComponent();
proces = new Entity.Process();
proces.ValueChanged += new Entity.ValueChangedEventHandler(proces_ValueChanged);
}
void proces_ValueChanged(object source, double Value)
{
textBox1.Text = Value.ToString(); // Exception kommer her.
//MessageBox.Show(Value.ToString());
}
}
