问题
I am currently developing a program where Raspberry Pi 3 will read contents sent by 4 Arduino (USB) devices through serial communication every 100ms. The UI gets stuck on the fourth call (serialDeviceIndex = 3) of Concurrency::create_task but if there are only 3 Arduino devices, the problem is not happening and the thread execution continues to the .then lambda body. Can you help me pin point what is the problem? Here's the function called when the connect
button from the xaml devices is pressed.
void MainPage::comPortInput_Click(Object^ sender, RoutedEventArgs^ e)
{
m_timer->Start();
auto selectionIndex = ConnectDevices->SelectedIndex;
if (selectionIndex < 0)
{
status->Text = "Select a device and connect";
return;
}
auto selectedItems = ConnectDevices->SelectedItems;
for (unsigned serialDeviceIndex = 0; serialDeviceIndex < selectedItems->Size; serialDeviceIndex++)
{
Device ^device = static_cast<Device ^>(selectedItems->GetAt(serialDeviceIndex));
textBox->Text = textBox->Text + "\n<START>:" + device->Id->ToString() + "<END>";
Windows::Devices::SerialCommunication::SerialDevice ^serial_device;
Windows::Devices::Enumeration::DeviceInformation ^entry = device->DeviceInfo;
auto childTokenSource = Concurrency::cancellation_token_source::create_linked_source(cancellationTokenSource->get_token());
auto childToken = childTokenSource.get_token();
Concurrency::create_task(
Windows::Devices::SerialCommunication::SerialDevice::FromIdAsync(entry->Id), childToken)
.then([this, serialDeviceIndex](Windows::Devices::SerialCommunication::SerialDevice ^serial_device)
{
_serialPort = serial_device;
Platform::String ^deviceString1 = _serialPort->ToString();
// Disable the 'Connect' button
//comPortInput->IsEnabled = false;
Windows::Foundation::TimeSpan _timeOut;
_timeOut.Duration = 100000L; //100000 works with delay(200)
// Configure serial settings
_serialPort->WriteTimeout = _timeOut;
_serialPort->ReadTimeout = _timeOut;
_serialPort->BaudRate = 9600;
_serialPort->Parity = Windows::Devices::SerialCommunication::SerialParity::None;
_serialPort->StopBits = Windows::Devices::SerialCommunication::SerialStopBitCount::One;
_serialPort->DataBits = 8;
_serialPort->Handshake = Windows::Devices::SerialCommunication::SerialHandshake::None;
_dataReaderObject = ref new Windows::Storage::Streams::DataReader(_serialPort->InputStream);
_dataReaderObject->InputStreamOptions = Windows::Storage::Streams::InputStreamOptions::Partial;
_dataReaderObjects.Append(_dataReaderObject);
_serialPorts.Append(_serialPort);
int serialPortsSize = _serialPorts.Size;
Listen(_serialPorts.GetAt(serialDeviceIndex), _dataReaderObject, serialDeviceIndex);
});
Sleep(5);
}
}
EDIT:
Okay, I simplified now the code to JUST this one
for (unsigned serialDeviceIndex = 0; serialDeviceIndex < selectedItems->Size; serialDeviceIndex++){
Device ^device = static_cast<Device ^>(selectedItems->GetAt(serialDeviceIndex));
textBox->Text = textBox->Text + "\n<START>:" + device->Id->ToString() + "<END>";
Windows::Devices::Enumeration::DeviceInformation ^entry = device->DeviceInfo;
auto it = Concurrency::create_task(Windows::Devices::SerialCommunication::SerialDevice::FromIdAsync(entry->Id));
while (!it.is_done())
textBoxStatus->Text = "processing" + deviceIndex.ToString();
it.get();
}
FYI, this code runs normally fine in my pc. no errors. When I try to run this in my RaspberryPi3 under Windows IoT the program hungs up at the fourth time I try to get the device using it.get()
来源:https://stackoverflow.com/questions/45662620/parallel-programming-stuck-on-4th-instance-of-concurrencycreate-task-call-for