I am creating the Windows form application that I currently have on Visual C#.
If you check the check box, I would like you to keep doing other work behind the scenes.
At that time, I decided to make the UI by asynchronous processing because I wanted to keep it from lumping.
Below is the coding that I am currently thinking about, but I wonder if the asynchronous processing of infinite loops using async/await is like this.
I'm sorry for the poor writing, but could someone please point it out?
private async void job 1_CheckedChange(object sender, EventArgse)
{
if (Work 1. Checked) // A: if checked
{
try // A: B, try this
{
wait Task.Run(async()=>Work1(); // A: Ask B to do one.Until then, I'll do other work. If there's any problem, please let me know.
}
catch(Exception ex) // A: I'll check if there's a problem
{
Job 1. Checked=false; // A: There's a problem, so I'll take it off.
AddLog(ex+"\r\n", Color.Blue); // A: I'm going to write any problems I have in my logbook
}
}
}
Private async Task Work 1()
{
while (Work 1. Checked) // B: If it's checked, all the time it's done
{
Write operations (pid, addr, ofst); // B: Write operations
wait Task.Delay(150).ConfigureAwait(false); // B: 0.15 seconds Next task will be delayed.There is no problem with Mr. A even if he is late.
}
End of write operation (pid, addr, ofst); // B: End of write operation since unchecked
}
Thank you for your prompt reply...
If I want to get UI side conditions from another thread, should I do this?
vartokenSource=new CancellationTokenSource(); // A: I'll make a condition for you to quit work 1.
vartoken=tokenSource.Token; // A: Here's the condition.
private async void job1_CheckedChange(object sender, EventArgse)
{
if (Work 1. Checked) // A: if checked
{
try // A: B, try this
{
wait Task.Run(async()=>Work1(token),token);//Mr.A: Have Mr.B do work1.Until then, I'll do other work. If there's any problem, please let me know.
}
catch(Exception ex) // A: I'll check if there's a problem
{
Job 1. Checked=false; // A: There's a problem, so I'll take it off.
AddLog(ex+"\r\n", Color.Blue); // A: I'm going to write any problems I have in my logbook
}
}
Mr. Else//A: If the check is not checked,
{
tokenSource.Cancel(); // A: I'll ask you to stop working 1.
}
}
Private async Task Job 1 (CancellationToken Token)
{
while(true) // B: While Mr. A didn't order me to stop working,
{
Write operations (pid, addr, ofst); // B: Write operations
wait Task.Delay(150).ConfigureAwait(false); // B: 0.15 seconds Next task will be delayed.There is no problem with Mr. A even if he is late.
If(token.IsCancellationRequested) // B: If A orders me to stop working,
{
End of write operation (pid, addr, ofst); // B: End of write operation since unchecked
break; // B: Stop writing. I'm not going to write.
}
}
}
@sayuri-san There is a problem due to the cause, so let's use CancellationToken for checking.Read this article.
Perform multiple asynchronous operations and interactively control
How: Cancel a task and its children
.NET Framework CancellationToken has two behaviors
wait Task.Run(async()=>Work1();
means that Work 1()
is performed on a separate thread.In addition, WinForms does not allow you to manipulate GUI elements (controls) from different threads.Therefore,
Work1()
method while (Work 1. Checked)
is an unauthorized access.
© 2024 OneMinuteCode. All rights reserved.