我想自动检测SerialPort
。所以我有五个组合框来选择SerialPort
这样的参数:
<TextBlock Text="BaudRate"/>
<ComboBox x:Name="comboBox_BaudRate" SelectedIndex="2">
<ComboBoxItem>9600</ComboBoxItem>
<ComboBoxItem>14400</ComboBoxItem>
<ComboBoxItem>19200</ComboBoxItem>
<ComboBoxItem>38400</ComboBoxItem>
<ComboBoxItem>56000</ComboBoxItem>
<ComboBoxItem>115200</ComboBoxItem>
</ComboBox>
<TextBlock Text="DataBit"/>
<ComboBox x:Name="comboBox_DataBit" SelectedIndex="0" >
<ComboBoxItem>8</ComboBoxItem>
<ComboBoxItem>7</ComboBoxItem>
</ComboBox>
<TextBlock Text="Parity" />
<ComboBox x:Name="comboBox_Parity" SelectedIndex="0" >
<ComboBoxItem>None</ComboBoxItem>
<ComboBoxItem>Odd</ComboBoxItem>
<ComboBoxItem>Even</ComboBoxItem>
</ComboBox>
<TextBlock Text="StopBit" />
<ComboBox x:Name="comboBox_StopBit" SelectedIndex="0" >
<ComboBoxItem>1</ComboBoxItem>
<ComboBoxItem>2</ComboBoxItem>
</ComboBox>
<Button x:Name="btn_Auto_Detect" Click="btn_Find_Click" />
当我点击btn_Auto_Detect
时,我想将这些组合框的不同参数设置为try_Port
,并尝试使用try_Port
发送数据来连接我的设备。问题是这个动作可能需要几分钟。所以我可以在这个时候取消这个任务。
public bool cancelFlag = false;
private void btn_Find_Click(object sender, RoutedEventArgs e)
{
var tokenSource = new CancellationTokenSource();
var token = tokenSource.Token;
if(cancelFlag)
{
cancelFlag = false;
tokenSource.Cancel();
Hint("Searching Canceled!");
}
else
{
cancelFlag = true;
Hint("Start Searching...");
}
Task.Factory.StartNew(() =>
{
if(!token.IsCancellationRequested)
{
Matching_Process_Thread();
}
else
{
return;
}
}, token).ContinueWith(task =>
{
...
}, token, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
}
public static SerialPort try_port = new SerialPort();
private void Matching_Process_Thread()
{
int tryNumber = 0;
//Loop searching the portname, baudrate, databit, parity, stopbit
for (int i = 0; i < comboBox_Port.Items.Count; i++)
{
try_port.PortName = comboBox_Port.Items[i].ToString();
for (int j = 0; j < comboBox_BaudRate.Items.Count; j++)
{
for (int k = 0; k < comboBox_DataBit.Items.Count; k++)
{
for (int m = 0; m < comboBox_Parity.Items.Count; m++)
{
for (int n = 0; n < comboBox_StopBit.Items.Count; n++)
{
//Here I want to update the UI
this.Dispatcher.BeginInvoke(
DispatcherPriority.SystemIdle, new Action<int, int,
int, int>(Update_ComboBoxs), j, k, m, n);
if(!try_port.isOpen){ try_port.Open(); }
...
//if get response, return
//else close this try_port.close() and return;
}
}
}
}
}
private void Update_ComboBoxs(int baudrate, int databit, int parity, int stopbit)
{
this.comboBox_BaudRate.SelectedIndex = baudrate;
try_port.BaudRate = int.Parse(comboBox_BaudRate.Text);
this.comboBox_DataBit.SelectedIndex = databit;
try_port.DataBits = int.Parse(comboBox_DataBit.Text);
this.comboBox_Parity.SelectedIndex = parity;
switch (parity)
{
case 0:
try_port.Parity = Parity.None;
break;
case 1:
try_port.Parity = Parity.Odd;
break;
case 2:
try_port.Parity = Parity.Even;
break;
default:break;
}
this.comboBox_StopBit.SelectedIndex = stopbit;
switch (stopbit)
{
case 0:
try_port.StopBits = StopBits.One;
break;
case 1:
try_port.StopBits = StopBits.Two;
break;
default:break;
}
}
但这永远不会停止任务,我也不知道为什么!
我该如何解决这个问题,还是有更好的方法来实现我的目标?提前谢谢!
答案 0 :(得分:0)
您需要检查循环中的IsCancellationRequested
属性。你在任务开始时检查一次,然后再不检查。
我会在较慢的操作之前检查它,如果有多个慢速操作则立即检查。然后退出你的功能,取消将按你的要求工作。