小编Pyt*_*oob的帖子

不同返回类型的 C# 策略设计模式

我尝试应用策略设计模式来解析一些文本内容,其中每个结果在不同的类中表示。

最小的例子。

所以我的界面是这样的:

public interface IParseStrategy
{
    object Parse(string filePath);
}
Run Code Online (Sandbox Code Playgroud)

实现算法的类:

class ParseA : IParseStrategy
{
    public object Parse(string filePath) => new ConcurrentQueue<ParseAData>();
}

class ParseB : IParseStrategy
{
    public object Parse(string filePath) => new Dictionary<string, ParseBData>();
}
Run Code Online (Sandbox Code Playgroud)

具体的“数据”类:

class ParseAData
{
    public int Id { get; set; }
    public string Name { get; set; }

}

class ParseBData
{
    private byte[] data;

    public byte[] GetData()
    {
        return data;
    }

    public void SetData(byte[] value)
    {
        data = value;
    } …
Run Code Online (Sandbox Code Playgroud)

c# strategy-pattern

5
推荐指数
1
解决办法
878
查看次数

在C#中调用showdialog时的线程吸收

我有一个分析大型XML文件并基于该内容构建WPF UI控件的应用程序。此任务通常需要大约15-30秒。为了通知用户正在运行的任务,我显示了一个简单的中间进度对话框窗口,例如:

Thread progressDialogThread = new Thread(() =>
{
    Window window = new Window
    {
        Content = new ProgressDialog(),
        Height = 100,
        Width = 150,
        WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen
    };
    window.ShowDialog();
});

progressDialogThread.SetApartmentState(ApartmentState.STA);
progressDialogThread.IsBackground = true;
progressDialogThread.Start();

buildUI();

progressDialogThread.Abort();
Run Code Online (Sandbox Code Playgroud)

这是可行的,但是progressDialogThread.Start()当应该再次解析XML时,有时会在上获得ThreadAbortException 。

有谁知道“关闭”进度对话框的更好方法?

由于控件必须构建在主UI线程上,因此我不能使用backgroundworker ...

XAML中的进度对话框本身如下所示:

<UserControl x:Class="MyDialog.ProgressDialog"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MyDialog"
        mc:Ignorable="d"
        Background="{DynamicResource MaterialDesignPaper}"
        TextElement.Foreground="{DynamicResource MaterialDesignBody}"
        Height="100" Width="150">
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <Label HorizontalAlignment="Center">Please wait</Label>
        <ProgressBar
          Style="{StaticResource MaterialDesignCircularProgressBar}"
          Value="0"
          IsIndeterminate="True" Width="40" Height="41" Margin="55,0" HorizontalAlignment="Center" VerticalAlignment="Center" />
    </StackPanel>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

c# threadabortexception

2
推荐指数
1
解决办法
68
查看次数