Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.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_Multithreading_Binding - Fatal编程技术网

C# 如何在线程中刷新文本框绑定

C# 如何在线程中刷新文本框绑定,c#,wpf,multithreading,binding,C#,Wpf,Multithreading,Binding,下面的代码是一个例子来解释我的问题 我有一个文本框在装订中。当我点击一个按钮时,执行一个函数。(在本例中,它是循环的 现在,我想用I变量的内容更新文本框。(public void MyAction()) 所以,我做了一个线程,但这不起作用 为什么? 提前谢谢 XAML代码 <TextBox Text ="{Binding MyValue}" HorizontalAlignment="Left" Height="47" Margin="4,4,4,4" VerticalAlignment="

下面的代码是一个例子来解释我的问题

我有一个文本框在装订中。当我点击一个按钮时,执行一个函数。(在本例中,它是循环的

现在,我想用I变量的内容更新文本框。(public void MyAction())

所以,我做了一个线程,但这不起作用

为什么?

提前谢谢

XAML代码

<TextBox Text ="{Binding MyValue}" HorizontalAlignment="Left" Height="47" Margin="4,4,4,4" VerticalAlignment="Top" Width="342"/>
<Button Command="{Binding ClickCommand}" Content="Run" Margin="114,69,283,216"/>

所以我认为你必须解决这些问题

首先使用
异步等待
模式

它是这样的-不阻塞你的UI线程

public async Task<object> MyAsyncMethod()
{
    return await Task.Run<object>(() =>
    {
        return null;
    });
}
第二个问题是,您可能希望在处理异步时更新UI。例如,这是更新进度的常见问题。您可以通过
SynchronizationContext
解决此问题

public interface IUpdateProgess
{
    void SendMessage(string val);
}



public async Task<object> MyAsyncMethod(IUpdateProgess progress)
{
    //UI thread
    SynchronizationContext context = SynchronizationContext.Current;

    return await Task.Run<object>(() =>
    {
        //other thread


        if (context != null && progress != null)
        {
            context.Post(new SendOrPostCallback((o) =>
            {
                progress.SendMessage("Progress");
            }), null);
        }

        return null;
    });
}
公共接口iupdateprogress
{
void SendMessage(字符串val);
}
公共异步任务MyAsyncMethod(IUUpdate进度)
{
//用户界面线程
SynchronizationContext=SynchronizationContext.Current;
返回等待任务。运行(()=>
{
//其他线程
if(上下文!=null&&progress!=null)
{
context.Post(新的SendOrPostCallback((o)=>
{
进度。发送消息(“进度”);
}),空);
}
返回null;
});
}

显然,您可以使用它来更新进度,我想您已经明白了。

'因为您在
MyAction
方法中完全阻塞了UI线程。
public async Task<object> MyAsyncMethod()
{
    return await Task.Run<object>(() =>
    {
        return null;
    });
}
public async void MyAsyncMethod()
{
    await Task.Run(() =>
    {

    });
}
public interface IUpdateProgess
{
    void SendMessage(string val);
}



public async Task<object> MyAsyncMethod(IUpdateProgess progress)
{
    //UI thread
    SynchronizationContext context = SynchronizationContext.Current;

    return await Task.Run<object>(() =>
    {
        //other thread


        if (context != null && progress != null)
        {
            context.Post(new SendOrPostCallback((o) =>
            {
                progress.SendMessage("Progress");
            }), null);
        }

        return null;
    });
}