I am a beginner with C# and WPF.
I would like to read from a CSV . I would like to pick up a line from the CSV each 30 second and with the fields of the line update the values in a User control ( a gauge with a Text box).
What I have already: I am doing something similar with a button event. I am reading the CSV with
StreamReader and
ReadLine and I am storing the data in a List<string>. I use for the user control
INotifypropertyChanged and I am binding the control properties to a data context ( values from the list). Then, when I press the button , the values are taken from to the next csv line (next list position) and consequently are updated into the user control.
I want to do it automatically , I want to read the next line each 30 seconds.I read about timer and I did some tests printing the objtects of the list each 5 secodnds ,but it is not working for my plan.
Any recommendation for my objective? I don't have to much experience with events and periods.
My code in this case it is not relevant because I am asking about how to fire , read , pick up a line or execute the data update each 30 second but just in case I added the main code blocks:
User control:
<pre lang="c#"> <Viewbox Grid.Column="1" >
<gauge:CircularGaugeControl x:Name="gaugeWindAngle" Grid.Column="1" Grid.Row="0" Margin="5"
Radius="105"
ScaleRadius="60"
ScaleStartAngle="140"
CurrentValue="{Binding Type}"
RangeIndicatorThickness="9"
RangeIndicatorRadius="45"
RangeIndicatorLightRadius="8"
PointerThickness ="5"
DialTextOffset="40"
DialText="Wind Angle"
DialTextColor="SlateGray"
/>
</Viewbox >
<WrapPanel Margin="5">
<Label Foreground="#DFA297">Speed: </Label>
<TextBox
x:Name="textboxWindSpeed" Text="{Binding TalkerID , UpdateSourceTrigger=PropertyChanged}"/>
</WrapPanel>
<Button Grid.Column="0" Content="Send sentence" x:Name="b1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Click="Button_Click" />
</Grid>
CODE BEHIND
public partial class Wind_gauge : UserControl, INotifyPropertyChanged
{
Trucker trucker = new Trucker();
double windAngle;
int click = 0;
public double WindAnglevalue
{
get { return windAngle; }
set
{
windAngle = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("WindAnglevalue"));
}
}
}
double windSpeed;
public double WindSpeedvalue
{
get { return windSpeed; }
set
{
windSpeed = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("WindSpeedvalue"));
} }
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
public Wind_gauge()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Window1_Loaded);
}
void Window1_Loaded(object sender, RoutedEventArgs e)
{
this.gaugeWindAngle.DataContext = trucker.sentenceReceived;
this.textboxWindSpeed.DataContext = trucker.sentenceReceived;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
sendsentence(sender, e);
click++;
}
READING MY CSV IN class Truker to List<string>sentencesListt;:
class Trucker
{ static Timer _timer;
public List<string> s = new List<string>();
public List<string> sentencesList
{
get {
if (s == null) {
loadNMEAsentences();
}
return s;
} }
public Trucker()
{
loadNMEAsentences();
}
public void loadNMEAsentences()
{
_timer = new Timer(3000);
_timer.Enabled = true;
_timer.Elapsed += new ElapsedEventHandler(timerRead);
}
void timerRead(object sender, ElapsedEventArgs e)
{ s= new List<string>();
System.IO.StreamReader file =
new System.IO.StreamReader(@"C:\Users\........\Data\NMEAmsg_wind.csv");
string line;
while ((line = file.ReadLine()) != null)
{ if (line.IndexOf("$IIMWV", StringComparison.CurrentCultureIgnoreCase) >= 0)
{
s.Add(line);
}
}
file.Close(); }