Reputation: 879
I've got the following function. This function starts reading settings from a device. First I set a parameter so my program knows it is reading from the device, than I set the setting which is being read, and finally I send a command to the device.
The function which reads the device's response knows it should check for parameters to read, because of the parameters set to do so. But I'm facing one problem: Because I only change booleans and execute a very small function to send the command, The function below is finished before the device has sent any response.
I tried fixing this with placing while functions in between. This solved it for the first block, but if I try that with the others, my whole application crashes. My question is how do I make this function pause till the parameter IsReadingSettingFromDevice
is set back to false?
private void ReadSettings()
{
ProgramParameters.C.IsReadingSettingFromDevice = true;
ProgramParameters.C.SettingToReadFromDevice = Data.CMD_type;
SerialCom.SC.SendCommand(Data.CMD_type);
Debug.WriteLine("DEBUG INFO: SendCommand: " + Data.CMD_type);
while (ProgramParameters.C.IsReadingSettingFromDevice) ;
ProgramParameters.C.IsReadingSettingFromDevice = true;
ProgramParameters.C.SettingToReadFromDevice = Data.CMD_IPsettings;
SerialCom.SC.SendCommand(Data.CMD_IPsettings);
Debug.WriteLine("DEBUG INFO: SendCommand: " + Data.CMD_IPsettings);
//while (ProgramParameters.C.IsReadingSettingFromDevice) ;
ProgramParameters.C.IsReadingSettingFromDevice = true;
ProgramParameters.C.SettingToReadFromDevice = Data.CMD_version;
SerialCom.SC.SendCommand(Data.CMD_version);
Debug.WriteLine("DEBUG INFO: SendCommand: " + Data.CMD_version);
//while (ProgramParameters.C.IsReadingSettingFromDevice) ;
ProgramParameters.C.IsReadingSettingFromDevice = true;
ProgramParameters.C.SettingToReadFromDevice = Data.CMD_channels;
SerialCom.SC.SendCommand(Data.CMD_channels);
Debug.WriteLine("DEBUG INFO: SendCommand: " + Data.CMD_channels);
}
EDIT: The class ProgramParameters is just a class I created myself. It keeps some info about global parameters of the current state of the program.
Upvotes: 2
Views: 156
Reputation: 10947
Start long-running methods in separate thread(s), for example using Task Parallel Library for simplicity - it does lots of plumbing for you in the background. You can then decide whether you want to wait for the results or be able to do something else in the application (maybe at least exit it correctly).
Take a look here for a "Getting Started" article on TPL, or here for a more general (and more low-level) threading tutorial.
Upvotes: 1