Click ok, the textbox value is assigned to a global variable
Click start, perform Click method
CodePudding user response:
Problem is: a run is jammedCodePudding user response:
Look up some information, asynchronous and multi-threadedSo tried, or jammed
CodePudding user response:
https://blog.csdn.net/desperaso/article/details/105728403
Private void Modify_Sector ()
{
While (true)
{
Enclosing the Dispatcher. InvokeAsync (()=& gt;
{
,,,,,,,,,,,,,,,,
});
Thread.sleep (1);//must have, or jammed
}
}
CodePudding user response:
Your logic has some problems, and using async grammar would be pretty simple, I tried BuKa
Public partial class MainWindow: Window
{
Public MainWindow ()
{
InitializeComponent();
}
Private void Button1_Click (object sender, RoutedEventArgs e)
{
Class1. Num1=int. Parse (textbox1. Text);
}
Private async void Button2_Click (object sender, RoutedEventArgs e)
{
Richtextbox1. AppendText (" start ");
Await Task. Run (new Action (
()=& gt;
{
While (true)
{
If (Class1. Num1==1) {
break;
}
System. Threading. Thread. Sleep (100);
}
}
));
Richtextbox1. AppendText (Class1. Num1. ToString ());
Richtextbox1. AppendText (" end ");
Class1. Num1=0;
}
}
Public class Class1
{
Public static int Num1 {get; set; }
}
CodePudding user response:
The Action should not explicitly create
Private async void Button2_Click (object sender, RoutedEventArgs e)
{
Richtextbox1. AppendText (" start ");
Await Task. Run (()=& gt; {
While (true) {
If (Class1. Num1==1) {
break;
}
System. Threading. Thread. Sleep (100);
}
});
Richtextbox1. AppendText (Class1. Num1. ToString ());
Richtextbox1. AppendText (" end ");
Class1. Num1=0;
}
CodePudding user response:
Write a Console example:using System;
Using System. The Threading. The Tasks;
The namespace ConsoleApp1
{
Class Program
{
The static void Main (string [] args)
{
Test1 ();
Console. WriteLine ("... Press any key to end ");
Console.ReadKey();
}
The static async void test1 ()
{
While (true)
{
Console. WriteLine (DateTime. Now. The ToString ());
Await Task. Delay (5000);
}
}
}
}
CodePudding user response:
In the process of the asynchronous, do not use block statements, including Thread. Sleep this class should use the asynchronous Task. The Delay, we to demonstrate a time-consuming calculation process with this statement, the real business logic to handle with obstruction is very outdated,CodePudding user response:
Control the . The Dispatcher. Invoke (()=& gt; {control. The Visibility=Visibility; });If you want to perform in any thread UI operation, then the thread unit must be set to the STA,
A thread if you create a UI object, then the UI object can only be the thread management,
Any thread if need access to other threads to create the UI object, can only access by other threads of the Dispatcher
A thread without performing any UI operation, so its associated Dispatcher to null
The above text content from https://www.cnblogs.com/DoNetCoder/p/4369903.html
CodePudding user response:
In your code, you use a child thread to run a while loop code, but you are in the child thread which USES Dispatcher. Invoke to block code executed the infinite loop, this call? This is blocked,CodePudding user response:
Oh, it is good to delay directly
Using System. The Threading. The Tasks;
Using System. Windows;
The namespace WpfApp1 {
///& lt; summary>
///MainWindow. Xaml interaction logic
///& lt;/summary>
Public partial class MainWindow: Window {
Public MainWindow () {
InitializeComponent();
}
Private void button1_Click (object sender, RoutedEventArgs e) {
Class1. Num1=int. Parse (textbox1. Text);
}
Private async void button2_Click (object sender, RoutedEventArgs e) {
Richtextbox1. AppendText (" start ");
While (true) {
If (Class1. Num1==1) {
break;
}
Await Task. Delay (1000);
}
Richtextbox1. AppendText (Class1. Num1. ToString ());
Richtextbox1. AppendText (" end ");
Class1. Num1=0;
}
}
Public class Class1 {
Public static int Num1 {get; set; }
}
}
CodePudding user response:
Thank you brother, has been solved, I try to DelayCodePudding user response:
Thanks for sharing. I want to learn!CodePudding user response:
Method looks nice, but the computer small white a little trouble