Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据文本框值暂停并恢复任务_C#_Wpf_Async Await - Fatal编程技术网

C# 根据文本框值暂停并恢复任务

C# 根据文本框值暂停并恢复任务,c#,wpf,async-await,C#,Wpf,Async Await,请先原谅我。我的发帖技巧很差。我正在学习以更干净的方式发布代码块。如果需要格式化,请随时告诉我 我有一个起点和一个设定点文本框。我还有一个显示当前设定值的TextBlock。我想增加textblock的值,使其从起点增加到设定点 例如起点=0 设置点=10 我创建了一个任务: private async Task MoveZ(double x, double y , double z) { zpos.Text = Convert.ToString(x); for (int i =

请先原谅我。我的发帖技巧很差。我正在学习以更干净的方式发布代码块。如果需要格式化,请随时告诉我

我有一个起点和一个设定点文本框。我还有一个显示当前设定值的
TextBlock
。我想增加
textblock
的值,使其从起点增加到设定点

例如
起点=0

设置
点=10

我创建了一个
任务

private async Task MoveZ(double x, double y , double z)
{
    zpos.Text = Convert.ToString(x);
    for (int i = 0; i < z; i++)
    {
        x = x + y;
        await Task.Delay(2000);
        zpos.Text = Convert.ToString(x);
    }
}

我的问题是:如果我有另一个
textbox
输入4——如果循环
textblock
值达到4——那么我需要暂停它,直到我将其更改为5。需要根据
文本框
输入暂停任务。

因为
MoveZ
返回一个
任务
,所以应该等待它

一般来说,您不应该暂停
任务
或线程或方法。尤其是当你不知道要多久的时候。最好取消
任务
并重新启动它。您始终可以使用事件来控制“非线性”流。
也许你应该考虑在后台线程上执行<代码> MOVEZ,并使用:

将递增的值发送回UI。 你需要知道:和

MainWindow.xaml.cs

public static readonly DependencyProperty StopValueProperty = DependencyProperty.Register(
  "StopValue",
  typeof(double),
  typeof(MainWindow),
  new PropertyMetadata(default(double)));

public double StopValue
{
    get => (double) GetValue(MainWindow.StopValueProperty);
    set => SetValue(MainWindow.StopValueProperty, value);
}

public static readonly DependencyProperty StepValueProperty = DependencyProperty.Register(
  "StepValue",
  typeof(double),
  typeof(MainWindow),
  new PropertyMetadata(default(double)));

public double StepValue
{
    get => (double) GetValue(MainWindow.StepValueProperty);
    set => SetValue(MainWindow.StepValueProperty, value);
}

public static readonly DependencyProperty BreakValueProperty = DependencyProperty.Register(
  "BreakValue",
  typeof(double),
  typeof(MainWindow),
  new PropertyMetadata(default(double), MainWindow.OnBreakValueChanged));

public double BreakValue
{
    get => (double) GetValue(MainWindow.BreakValueProperty);
    set => SetValue(MainWindow.BreakValueProperty, value);
}

public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
  "Value",
  typeof(double),
  typeof(MainWindow),
  new PropertyMetadata(default(double)));

public double Value
{
    get => (double) GetValue(MainWindow.ValueProperty);
    set => SetValue(MainWindow.ValueProperty, value);
}

private CancellationTokenSource CancellationTokenSource { get; set; }

public MainWindow()
{
    InitializeComponent();
    this.DataContext = this;
}

private static void OnBreakValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var this_ = d as MainWindow;
    if (this_.Value == this_.BreakValue)
    {
        // Cancel MoveZ()
        this_.CancellationTokenSource.Cancel();
        return;
    }   

    // Task was canceled before --> restart procedure
    if (this_.CancellationTokenSource.IsCancellationRequested)
    {
        this_.StartMoveZ();
    }   
}

private void MoveZ(IProgress<double> progressReporter, CancellationToken cancellationToken)
{
    Task.Run(
    async () =>
    {
        for (; this.Value <= this.StopValue; this.Value += this.StepValue)
        {
            progressReporter.Report(this.Value);
            cancellationToken.ThrowIfCancellationRequested();
            await Task.Delay(2000, cancellationToken);
         }
     }, cancellationToken);
}

private void Button_Click(object sender, RoutedEventArgs e) 
{
    this.Value = 0;
    StartMoveZ();
}

private void StartMoveZ() 
{
    this.CancellationTokenSource = new CancellationTokenSource();
    var progressReporter = new Progress<double>(value => zpos.Text = value);
    MoveZ(progressReporter, this.CancellationTokenSource.Token);
}
公共静态只读DependencyProperty StopValueProperty=DependencyProperty.Register( “停止值”, 类型(双), 类型(主窗口), 新属性元数据(默认值(双精度)); 公共双停止值 { get=>(双精度)GetValue(MainWindow.StopValueProperty); set=>SetValue(MainWindow.StopValueProperty,value); } 公共静态只读DependencyProperty StepValueProperty=DependencyProperty.Register( “阶跃值”, 类型(双), 类型(主窗口), 新属性元数据(默认值(双精度)); 公共双重价值 { get=>(双精度)GetValue(MainWindow.StepValueProperty); set=>SetValue(MainWindow.StepValueProperty,值); } 公共静态只读DependencyProperty BreakValueProperty=DependencyProperty.Register( “BreakValue”, 类型(双), 类型(主窗口), 新属性元数据(默认值(双精度),MainWindow.OnBreakValueChanged); 公共双断值 { get=>(双精度)GetValue(MainWindow.BreakValueProperty); set=>SetValue(MainWindow.BreakValueProperty,value); } 公共静态只读DependencyProperty ValueProperty=DependencyProperty.Register( “价值”, 类型(双), 类型(主窗口), 新属性元数据(默认值(双精度)); 公共双重价值 { get=>(双精度)GetValue(MainWindow.ValueProperty); set=>SetValue(MainWindow.ValueProperty,value); } 私有CancellationTokenSource CancellationTokenSource{get;set;} 公共主窗口() { 初始化组件(); this.DataContext=this; } 私有静态void OnBreakValueChanged(DependencyObject d、DependencyPropertyChangedEventArgs e) { var this_ud=main窗口; 如果(此值==此值) { //取消MoveZ() 此函数为.CancellationTokenSource.Cancel(); 返回; } //任务在-->重新启动过程之前被取消 if(此_uu.CancellationTokenSource.IsCancellationRequested) { 这是StartMoveZ(); } } 私有void MoveZ(IProgress progressReporter,CancellationToken CancellationToken) { 任务。运行( 异步()=> { for(;this.Value zpos.Text=Value); MoveZ(progressReporter,this.CancellationTokenSource.Token); } main window.xaml

<Window>
  <StackPanel>
    <TextBox Text="{Binding StopValue}" />
    <TextBox Text="{Binding BreakValue}" />
    <TextBox Text="{Binding StepValue}" />
    <Button Content="Start" Click="Button_Click" />
    <TextBlock Text="{Binding Value}" />
  </StackPanel>
</Window>

您可以使用来基于事件生成查询


检查并。

通过
TextBlock
您的意思是
TextBox
?有一个TextBlock为设定点提供反馈,还有一个TextBox用于暂停和恢复任务。TextBlock的输入需要与TextBlock中的反馈匹配才能恢复任务。在此之前,它应该处于暂停状态。我认为您需要使用后台工作人员类你会发现它在这里被记录@ @ WaltEVEHOWHONE <代码>后台工作人员<代码>已经被<代码>任务取代。运行< /代码>和AsiNC/Acth.@ CauSjad谢谢。我们可以同意,我们不同意。ck在维护一个针对客户或更早的客户的项目时。
<Window>
  <StackPanel>
    <TextBox Text="{Binding StopValue}" />
    <TextBox Text="{Binding BreakValue}" />
    <TextBox Text="{Binding StepValue}" />
    <Button Content="Start" Click="Button_Click" />
    <TextBlock Text="{Binding Value}" />
  </StackPanel>
</Window>