SerialPort
が自動的に検出されます。私はtry_Port
にこれらのコンボボックスの異なるパラメータを設定し、try_Port
を使用してデータを送信することによって、自分のデバイスを接続するために試してみたいbtn_Auto_Detect
をクリックするとタスクを開始してキャンセルするには?
<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" />
:だから私はこのようなSerialPort
のパラメータを選択するための5 comboboxsを持っています。問題は、このアクションは数分かかることがあります。だから私は今回の仕事をキャンセルするかもしれません。
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;
}
}
しかし、これは決して仕事を止めるものではありません。理由はわかりません。
私はこれを修正する必要がありますか、または私が欲しいものを達成するためのより良い方法がありますか?前もって感謝します!
なぜDispatcherPriority.SystemIdleを使用しましたが、別のオプションを使用しませんでしたか? – pix
try_port.open()が実行される前にアクション内のUpdate_ComboBoxが終了しているかどうかを確認しましたか?はいの場合は、コードをデバッグしましたか?あなたのコードをテストすることなく、エラーはタスク内からUIを更新することとは関係がないように思えます。 – ooorndtski
はいコードをデバッグしていて、uiコードの更新をループに入れています。それは非常に長い時間を要することを除いてうまく動作します。 – user8595258