Click here to Skip to main content
16,019,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
<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:

C#
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
C#
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?
Posted
Updated 15-Dec-23 13:08pm
v2
Comments
[no name] 15-Dec-23 18:27pm    
I see nothing that connects your "led" with "CheckUSB" etc. "Origin" values are typical in the range of 0-1.0 where (0.5,0.5) is a "center point" relative to the origin, and probably meaningless in the current context.
Jared THompson 15-Dec-23 19:09pm    
typo. The led behavior works as expected.
[no name] 15-Dec-23 19:48pm    
"As expected". "Something like this." Enough said.
Jared THompson 15-Dec-23 21:11pm    
I appreciate what you're saying, but again, I can operate the led on screen correctly if I have the method set usbConnected to true outside the thread. The led turns from red to green.
[no name] 16-Dec-23 13:32pm    
And I know what you're saying and "running code from a dll" is no different from a "local" namespace; it's a matter of adding a reference to the dll and a "using". It needs to be a "compatible" dll; which is a wpf ddl / exe or a Standard Framework dll. What happens next "depends". These "threads" could be other Windows, BackgroundWorkers, FileWatchers, Tasks, Timers, etc. ... which all "hook" differently. There is no "one size fits all" "answer". What Dave said: the UI "binding" engine in WPF allows multiple threads to "talk" to the UI via properties (INotifyPropertyChanged interface).

1 solution

Your code is "polling" for a state change using a timer. You should be listening for an event from your DLL.

Gerry has mentioned the Event pattern. This is core to .Net, in fact you are using it with the timer, the timer raises an event that you have bound to. You can read more here: How to: Raise and Consume Events - .NET | Microsoft Learn[^]

Dave has mentioned the MVVM design pattern. The event system uses the INotifyPropertyChanged event pattern for data binding. You can read more here: How to: Implement Property Change Notification - WPF .NET Framework | Microsoft Learn[^]

Which is better? It depends on your level of experience. However, if you are using WPF, MVVM is far better to learn. WPF was designed with MVVM in mind.

If you want to learn MVVM, there there are many resources available to you: learn wpf mvvm tutorial - Google Search[^] and closer to home: Wpf mvvm - CodeProject[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900