Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.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#wpf更改按钮内容执行一些代码,然后将其更改回来_C#_Wpf_Multithreading_Async Await - Fatal编程技术网

c#wpf更改按钮内容执行一些代码,然后将其更改回来

c#wpf更改按钮内容执行一些代码,然后将其更改回来,c#,wpf,multithreading,async-await,C#,Wpf,Multithreading,Async Await,我有一个简单的wpf c#应用程序,它从inputField获取文本,尝试在那里查找一些信息,并将结果返回outputField。 代码如下: private void FindButton_Click(object sender, RoutedEventArgs e) { try { string parsed = string.Empty; if (string.IsNullOrWhiteSpace(new TextRange(InputFie

我有一个简单的wpf c#应用程序,它从inputField获取文本,尝试在那里查找一些信息,并将结果返回outputField。 代码如下:

private void FindButton_Click(object sender, RoutedEventArgs e)
{
    try
    {
        string parsed = string.Empty;
        if (string.IsNullOrWhiteSpace(new TextRange(InputField.Document.ContentStart, InputField.Document.ContentEnd).Text));
        {
            OutputField.Document.Blocks.Clear();
            MessageBox.Show("Empty input");
        }
        else
        {
            Parser nOb = new Parser(new TextRange(InputField.Document.ContentStart, InputField.Document.ContentEnd).Text);
            string[] result = nOb.findAddresses();

            if (result.Length == 0)
            {
                OutputField.Document.Blocks.Clear();
                MessageBox.Show("Nothing found");
            }
            else
            {
                for (int i = 0; i < result.Length; i++)
                {
                    parsed += result[i] + Environment.NewLine;
                }

                OutputField.Document.Blocks.Clear();
                OutputField.Document.Blocks.Add(new Paragraph(new Run(parsed)));

                MessageBox.Show("Success");
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: " + ex.Message);
    }
}
await Task.Run(() => {
//My code which is above
});
并在尝试{}“查找”后将其更改回,但无效。 正如我在某个地方读到的,我需要在这里使用异步方法或线程。 我在这里找到了一些解决方案,并尝试将“async”添加到我的函数中,还更改了代码:

private void FindButton_Click(object sender, RoutedEventArgs e)
{
    try
    {
        string parsed = string.Empty;
        if (string.IsNullOrWhiteSpace(new TextRange(InputField.Document.ContentStart, InputField.Document.ContentEnd).Text));
        {
            OutputField.Document.Blocks.Clear();
            MessageBox.Show("Empty input");
        }
        else
        {
            Parser nOb = new Parser(new TextRange(InputField.Document.ContentStart, InputField.Document.ContentEnd).Text);
            string[] result = nOb.findAddresses();

            if (result.Length == 0)
            {
                OutputField.Document.Blocks.Clear();
                MessageBox.Show("Nothing found");
            }
            else
            {
                for (int i = 0; i < result.Length; i++)
                {
                    parsed += result[i] + Environment.NewLine;
                }

                OutputField.Document.Blocks.Clear();
                OutputField.Document.Blocks.Add(new Paragraph(new Run(parsed)));

                MessageBox.Show("Success");
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: " + ex.Message);
    }
}
await Task.Run(() => {
//My code which is above
});
但它开始返回以下错误:

MS.Internal.PtsHost.UnsafeNativeMethods.PTS.Secondary异常 NullReferenceException


我对这些话题完全陌生,不知道如何让它起作用。请找人帮助。

假设您的
findAddresses()
方法访问UI元素,并且必须在UI线程上执行,您可以尝试使用
任务。延迟
,让UI线程有机会在您启动操作之前更新
FindButton
。试试这个:

private async void FindButton_Click(object sender, RoutedEventArgs e)
{
    FindButton.Content = "Searching...";
    FindButton.IsEnabled = false;
    await Task.Delay(1);

    try
    {
        string parsed = string.Empty;
        if (string.IsNullOrWhiteSpace(new TextRange(InputField.Document.ContentStart, InputField.Document.ContentEnd).Text)) ;
        {
            OutputField.Document.Blocks.Clear();
            MessageBox.Show("Empty input");
        }
                else
                {
            Parser nOb = new Parser(new TextRange(InputField.Document.ContentStart, InputField.Document.ContentEnd).Text);
            string[] result = nOb.findAddresses();

            if (result.Length == 0)
            {
                OutputField.Document.Blocks.Clear();
                MessageBox.Show("Nothing found");
            }
            else
            {
                for (int i = 0; i < result.Length; i++)
                {
                    parsed += result[i] + Environment.NewLine;
                }

                OutputField.Document.Blocks.Clear();
                OutputField.Document.Blocks.Add(new Paragraph(new Run(parsed)));

                MessageBox.Show("Success");
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: " + ex.Message);
    }

    FindButton.Content = "Default";
    FindButton.IsEnabled = true;
}
private async void FindButton_单击(对象发送方,RoutedEventArgs e)
{
FindButton.Content=“搜索…”;
FindButton.IsEnabled=false;
等待任务。延迟(1);
尝试
{
string parsed=string.Empty;
if(string.IsNullOrWhiteSpace(新文本范围(InputField.Document.ContentStart,InputField.Document.ContentEnd.Text));
{
OutputField.Document.Blocks.Clear();
MessageBox.Show(“空输入”);
}
其他的
{
Parser nOb=new Parser(新的文本范围(InputField.Document.ContentStart,InputField.Document.ContentEnd.Text);
字符串[]结果=nOb.findAddresses();
如果(result.Length==0)
{
OutputField.Document.Blocks.Clear();
MessageBox.Show(“未找到任何内容”);
}
其他的
{
for(int i=0;i
查看一下
BackgroundWorker
类。这是一个多线程的好入口谢谢你,它很有效。但有可能在MessageBox弹出之前更改FindButton.Content吗?因为只有在MessageBox消失并FindButton.Content更改后,我才能按OK。