Reputation: 85
I am very new to all this so please bear with me! I am writing a small app to control my telescope, at the moment I can connect to it and tell it where to point. I want to have a couple of text boxes, or labels that constantly update with the telescopes position - T
is the telescope object and I am calling T.Altitude
, T.Azimuth
, T.RightAscention
and T.Declination
and I want these values to update the four labels every half second or so. I assume I need to use a background worker but am I correct? Will I be able to access the Telescope object since it was created on the main thread? And how exactly do I do it all! This is what I have so far (and it aint much!)...
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
string Az = T.Azimuth.ToString();
string Alt = T.Altitude.ToString();
string Dec = T.Declination.ToString();
string Ra = T.RightAscension.ToString();
System.Threading.Thread.Sleep(500);
}
Upvotes: 1
Views: 4228
Reputation: 4369
The easiest way is probably as suggested to use a Windows.Forms.Timer to periodically update the Gui with current values from your Telescope (object).
As a side note, the Background Worker is kind of obsolete in C# 5.0 since it is much easier to use async/await (see this thread about async/await vs BackgroundWorker).
Here is an example implementation in WinForms which refreshes a set of labels every 500 milliseconds.
public partial class MyForm : Form
{
private readonly Timer _timer = new Timer();
public MyForm()
{
InitializeComponent();
_timer.Interval = 500;
_timer.Tick += TimerTick;
_timer.Enabled = true;
}
void TimerTick(object sender, EventArgs e)
{
_labelAzimuth.Text = T.Azimuth.ToString();
_labelAltitude.Text = T.Altitude.ToString();
_labelDeclination.Text = T.Declination.ToString();
_labelRightAscension.Text = T.RightAscension.ToString();
}
}
Upvotes: 0
Reputation: 3333
In your case you should consider using one of the Timer classes. Those classes call a given delegate in specified intervals.
The Timer class from Windows.Forms namespace calls a delegate in UI thread, so you will not have to bother with dispatching or anything, but it might make UI less responsive if you call it too often.
Other Timers use separate threads, so you will need to use either Dispatcher object or SynchronizationContext object to modify UI values. You can read more about those on msdn.
Upvotes: 2