小编Wan*_*abe的帖子

C# RegEx 查找特定字符串或字符串中的所有单词

查了一下,我以为我明白了如何在一个句子中查找一串多个单词,但它没有找到匹配项。有人可以告诉我我做错了什么吗?我需要能够找到单个或多个单词匹配。我将“查找”传递给该方法,但它没有找到匹配项。此外,如果用户没有将他们的搜索短语用引号括起来,我还需要它来搜索输入的每个单词。

var pattern = @"\b\" + searchString + @"\b";    //searchString is passed in.

Regex rgx = new Regex(pattern);

var sentence = "I need to find a string in this sentence!";

Match match = rgx.Match(sentence);

if (match.Success)
{
    // Do something with the match.
}
Run Code Online (Sandbox Code Playgroud)

c# regex

7
推荐指数
1
解决办法
1万
查看次数

为什么Task.Wait()导致应用程序冻结

当我调用BuildCustomer.StartTask时,我调用一个方法WriteToDatabase.在WriteToDatabase内部,我想将状态发送回MainForm以将状态写入GUI.当代码到达那一点时,我的应用程序冻结并且没有错误.我确实发现如果我删除task.Wait(),它会停止冻结并运行.但我想我想要等待,因为我的BuildCustomer需要一些时间并将大量更新(包括来自Common类的更多更新)写入GUI.有人可以告诉我什么是错的或我应该采取哪些不同的做法?这是一个.Net 4项目,所以我不能使用async,我已经看到了其他答案.

public partial class MainForm : Window
{
    public MainForm()
    {
        Common.SendMessage += UpdateStatus;
    }

    private void Button_Click(object sender, EventArgs e)
    {
        BuildCustomer.StartTask();
    }

    private void UpdateStatus(string message)
    {
        Dispatcher.Invoke(new Action(() =>
        {
            StatusTextBox.Text = message;
        }));
    }
}

public class BuildCustomer
{
    public static void StartTask()
    {
        var action = new Action<object>(BuildCustomer);

        var task = new Task(() => action(buildDetails));

        task.Start();

        task.Wait();
    }

    private void BuildCustomerDetails(object buildDetails)
    {
        Common.WriteToDatabase();
    }
}

public class Common
{
    public delegate void MessageLogDelegate(string …
Run Code Online (Sandbox Code Playgroud)

c# task-parallel-library async-await

-3
推荐指数
1
解决办法
1354
查看次数

标签 统计

c# ×2

async-await ×1

regex ×1

task-parallel-library ×1