I need to access the TimerData.textSet field to change the text displayed from a another thread that controls the timing. But a InvalidOperationException is thrown. Is there a solution to this problem.
namespace Scilca.KBL
{
public class TimerData
{
public Run textSet;
public ProgressBar statusTimer;
public TimerData(Run run, ProgressBar statusTimer)
{
textSet = run;
this.statusTimer = statusTimer;
}
}
/// <summary>
/// Interaction logic for KBLSessionWindow.xaml
/// </summary>
public partial class KBLSessionWindow : Window
{
private int leftTime = 60;
private static Run run;
private static ProgressBar progressBar;
public int TimeLeftOver
{
get { return leftTime; }
}
public KBLSessionWindow()
{
InitializeComponent();
run = runSecondTime;
progressBar = timeProgress;
Thread timerThread = new Thread(new ParameterizedThreadStart(startTimer));
timerThread.Start(new TimerData(run, progressBar));
}
public void startTimer(Object td)
{
TimerData timerData = (TimerData)td;
int time = 60;
while (true)
{
Thread.Sleep(1000);
time -= 1;
if (time == 0)
time = 60;
Console.WriteLine(timerData.textSet.Text);
timerData.textSet.Text = time.ToString(); // InvalidOperationException
timerData.statusTimer.Value = time * 100 / 60;
}
}
}
}