Hi, new to C#, very new to programming for WPF.
I want to cause an indicator on the MainWindow to change based on the output of a DLL. The program needs to be continuously aware of the behavior of the DLL, think of it as a streaming data source. In this case, I only want to check if a variable has changed.
MainWindow.xaml:
<pre><Window x:Class="TestWPFApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ScottPlot="clr-namespace:ScottPlot;assembly=ScottPlot.WPF"
xmlns:local="clr-namespace:TestWPFApp"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<local:LedControl x:Name="USBConn" Content="USB Conn" OnColor="ForestGreen" OffColor="DarkRed" RenderTransformOrigin="4.00,16.637" Margin="63,80,640,135" />
</Grid>
</Window>
In the code behind, I have something like this:
public partial class MainWindow : Window
{
public static bool usbConnected = false;
readonly Timer CheckUSBConnTimer = new Timer() { Interval = 1000, Enabled = true };
public MainWindow()
{
InitializeComponent();
RenderTestPlot();
Console.WriteLine("hello");
CheckUSBConnTimer.Tick += (s, e) => CheckUSBConnection(out usbConnected);
UsbConn.IsChecked = usbConnected;
Console.WriteLine($"check {usbConnected}");
}
private void CheckUSBConnection(out bool usbConnected){...}
}
This doesn't ever return back to the main UI thread. It just goes over and over the timer no matter what and doesn't seem to behave correctly.
What I need is something that periodically checks the output of
CheckUSBConnection()
and returns that value back to the main screen. The DLL works.
Everything else I've debugged.
This code works for example if I call the function once while the true condition is satisfied. But the second I involve a Task or thread like this, the way this works doesn't ever seem to result in a return to the main window.
What I have tried:
Obviously I don't understand something fundamental. I have been through and through many sites and many guides, but everything seems predicated on the idea that a task is going to stop. In this case, we need to continuously check whether or not the condition is true. Is this a matter of needing a Dispatcher?
Can someone guide me to a learning resource?