MainWindowViewModel
public void MainWindowViewModel()
{
var ports = LoadInstruments();
if (ports.Count == 0) //ports.Count = 0 when NOT stepping through.
{
//DoSomething
}
else
{
Instruments=ports;
}
}
Public ObservableCollection
{
InstComs inst=new();
return inst.GetInstPortsAsync(); //Also tried awaiting this.
}
in InstComs Class
public async Task
{
ObservableCollection
foreach (string port in SerialPort.GetPortNames())
{
SerialPort sp = new()
{
PortName = port,
ReadBufferSize = 1024,
DiscardNull = true,
RtsEnable = true,
DtrEnable = true,
ReceivedBytesThreshold = 1,
BaudRate = 9600,
StopBits = StopBits.One,
Parity = Parity.None,
DataBits = 8
};
try
{
sp.Open();
if (sp.IsOpen)
{
string instID = await Task.Run(()=> GetInstrumentId(sp));
if (instID != null)
{
if (instID.ToString().Contains("InstNo"))
{
InstrumentViewModel ivm = new();
ivm.Parity = sp.Parity;
ivm.StopBits = sp.StopBits;
ivm.InstID = instID.ToString().Split(',').ElementAt(2);
ivm.BaudRate = sp.BaudRate;
ivm.PortName = sp.PortName;
ivm.DataBits = 8;
ivm.RTSEnabled = sp.RtsEnable;
ivm.DTREnabled = sp.DtrEnable;
ivm.Port = sp;
ivm.IsConnected = true;
ports.Add(ivm);
}
}
}
}
catch (InvalidOperationException)
{
//Handle it
}
}
return ports;
}
Public Task
{
try
{
string response = string.Empty;
sp.WriteLine("*IDN?");
while (sp.BytesToRead > 0)
{
respose += sp.ReadExisting();
}
return response.Replace("\r", "").Trim();
}
catch
{
//Do the catch stuff
}
}
When I step through the code, it works perfectly. The instrument is returned and appears in the list view in the main view with all of the data. If I just run it (in debug mode) the instruments collection remains empty. The MainWindowView has a ListView that is bound to the Instruments collection.
Yes, a lot of code has been removed or left out. But none of it has a bearing on the way this behaves. I have tried awaiting GetInstrumentID. I have tried awaiting GetInstrumentPorts. None of it makes a difference. I have even tried a pause using Thread.Sleep(1000) to slow things down and that didn't help.
Any ideas?
Thanks,
Scott StewartPosted Jun 11, 2024, 12:28 PM
Thank you, Amit, for your reply. Yes, the code is incomplete in so far as it is only four methods out of several classes and a lot more methods. I included the methods that make up the view that is the problem. If async await does not wait for the method to complete, then what does? The only thing I can think of now is to add a lot of pauses throughout to slow it all down. This is only a problem when the program starts. Once the collection is populated, there is no need to run through this again. I can say without a doubt it is an asyncronous issue. It ran perfectly twice at full speed.
I must not yet understand async await. Nothing is "awaited" the code just runs on and leaves the result of the called method hanging. When wathcing the threads, one will show the thread created with the call to GetInstrumentPortsAsync. Within that there is a call to GetInstrumentIDAsync. That method actually queries the instrument for it's ID. I have awaited that one too.At the moment my code looks like this:
What can I do differently?
Amit MohantyPosted Jun 11, 2024, 5:29 AM
I think the issue you're encountering is likely due to the asynchronous nature of your code and how you're handling the task results in the view model's constructor. When you step through the code, it gives time for the asynchronous operations to complete, but when running normally, the operations don't complete before the constructor finishes executing. So you need to ensure that your view model correctly waits for the asynchronous operation to complete before checking the result.
Also your code appears to be incomplete or incorrectly provided.