-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: exception in multi-threaded applications (#266)
Fixes #263 --------- Co-authored-by: i28423 <[email protected]> Co-authored-by: FantasticFiasco <[email protected]>
- Loading branch information
1 parent
2e3b8ca
commit ad75753
Showing
20 changed files
with
544 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net472</TargetFramework> | ||
|
||
<RootNamespace>Demo.StaThreads</RootNamespace> | ||
|
||
<UseWpf>true</UseWpf> | ||
|
||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\MvvmDialogs.csproj" /> | ||
<ProjectReference Include="..\Demo.StaThreads\Demo.StaThreads.csproj" /> | ||
<ProjectReference Include="..\TestBaseClasses\TestBaseClasses.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" /> | ||
<PackageReference Include="Moq" Version="4.20.69" /> | ||
<PackageReference Include="xunit" Version="2.5.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.1"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.Windows; | ||
using Moq; | ||
using MvvmDialogs; | ||
using Xunit; | ||
|
||
namespace Demo.StaThreads; | ||
|
||
public class MainWindowViewModelTest | ||
{ | ||
[Fact] | ||
public void ShowMessageBox() | ||
{ | ||
// Arrange | ||
var dialogService = new Mock<IDialogService>(); | ||
var viewModel = new MainWindowViewModel(dialogService.Object); | ||
|
||
dialogService | ||
.Setup(mock => | ||
mock.ShowMessageBox( | ||
viewModel, | ||
It.IsAny<string>(), | ||
"", | ||
MessageBoxButton.OK, | ||
MessageBoxImage.None, | ||
MessageBoxResult.None)) | ||
.Returns(MessageBoxResult.OK); | ||
|
||
// Act | ||
viewModel.ShowMessageBoxCommand.Execute(null); | ||
|
||
// Assert | ||
dialogService.VerifyAll(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using FlaUI.Core.AutomationElements; | ||
using FlaUI.Core.Input; | ||
using TestBaseClasses; | ||
|
||
namespace Demo.StaThreads.ScreenObjects; | ||
|
||
public class MainScreen : Screen | ||
{ | ||
public MainScreen(Window window) | ||
: base(window) | ||
{ | ||
} | ||
|
||
private Button MessageBoxButton => ElementByAutomationId<Button>("1k7d1Nm8MkOYK5qGrdVX4Q"); | ||
private Label ConfirmationLabel => ElementByAutomationId<Label>("kT3_ZUZfsEK1QdZ2jBfuIQ"); | ||
|
||
public string Confirmation => ConfirmationLabel.Text; | ||
|
||
public MessageBoxScreen ClickMessageBox() | ||
{ | ||
MessageBoxButton.Click(); | ||
Wait.UntilInputIsProcessed(); | ||
|
||
return new MessageBoxScreen(GetModalWindow(string.Empty)); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
samples/Demo.StaThreads.Test/ScreenObjects/MessageBoxScreen.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using FlaUI.Core.AutomationElements; | ||
using TestBaseClasses; | ||
|
||
namespace Demo.StaThreads.ScreenObjects; | ||
|
||
public class MessageBoxScreen : Screen | ||
{ | ||
public MessageBoxScreen(Window window) | ||
: base(window) | ||
{ | ||
} | ||
|
||
private Label MessageLabel => ElementByAutomationId<Label>("65535"); | ||
|
||
public string Caption => Window.Title; | ||
|
||
public string Message => MessageLabel.Text; | ||
|
||
public bool IsIconVisible => ElementWithAutomationIdExists("20"); | ||
|
||
public bool IsOKButtonVisible => ElementWithTextExists(OK); | ||
|
||
public bool IsCancelButtonVisible => ElementWithTextExists(Cancel); | ||
|
||
public void ClickOK() | ||
{ | ||
DefaultOKButton.Click(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Demo.StaThreads.ScreenObjects; | ||
using TestBaseClasses; | ||
using Xunit; | ||
|
||
namespace Demo.StaThreads; | ||
|
||
public class UITests : IDisposable | ||
{ | ||
private readonly Application app; | ||
private readonly MainScreen mainScreen; | ||
|
||
public UITests() | ||
{ | ||
app = Application.Launch("Demo.StaThreads.exe"); | ||
mainScreen = new MainScreen(app.GetMainWindowThatStartsWith("Demo - STA Threads")); | ||
} | ||
|
||
[Fact] | ||
[Trait("Category", "Manual")] | ||
public void ConfirmationWithText() | ||
{ | ||
var messageBoxScreen = mainScreen.ClickMessageBox(); | ||
Assert.Equal(string.Empty, messageBoxScreen.Caption); | ||
Assert.Equal("This is the text.", messageBoxScreen.Message); | ||
Assert.False(messageBoxScreen.IsIconVisible); | ||
Assert.True(messageBoxScreen.IsOKButtonVisible); | ||
Assert.False(messageBoxScreen.IsCancelButtonVisible); | ||
|
||
messageBoxScreen.ClickOK(); | ||
|
||
Assert.Equal("We got confirmation to continue!", mainScreen.Confirmation); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
app.Dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json", | ||
"shadowCopy": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<Application | ||
x:Class="Demo.StaThreads.App" | ||
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:Demo.StaThreads" | ||
mc:Ignorable="d" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System.Threading; | ||
using System.Windows; | ||
using System.Windows.Threading; | ||
using CommunityToolkit.Mvvm.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using MvvmDialogs; | ||
|
||
namespace Demo.StaThreads; | ||
|
||
public partial class App : Application | ||
{ | ||
protected override void OnStartup(StartupEventArgs e) | ||
{ | ||
Ioc.Default.ConfigureServices( | ||
new ServiceCollection() | ||
.AddSingleton<IDialogService, DialogService>() | ||
.AddTransient<MainWindowViewModel>() | ||
.BuildServiceProvider()); | ||
|
||
for (var id = 0; id < 2; id++) | ||
{ | ||
var windowThread = new WindowThread(id); | ||
|
||
var thread = new Thread(windowThread.Run); | ||
thread.SetApartmentState(ApartmentState.STA); | ||
thread.Start(); | ||
} | ||
} | ||
|
||
private class WindowThread | ||
{ | ||
private readonly int id; | ||
|
||
public WindowThread(int id) | ||
{ | ||
this.id = id; | ||
} | ||
|
||
public void Run() | ||
{ | ||
var mainWindow = new MainWindow(); | ||
mainWindow.Title += $" (id {id})"; | ||
mainWindow.DataContext = Ioc.Default.GetRequiredService<MainWindowViewModel>(); | ||
|
||
mainWindow.Show(); | ||
|
||
Dispatcher.Run(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<ResourceDictionary | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> | ||
|
||
<!-- | ||
Named Resources | ||
--> | ||
|
||
<Style x:Key="BaseStyle" TargetType="{x:Type FrameworkElement}"> | ||
<Setter Property="Margin" Value="3"/> | ||
</Style> | ||
|
||
<!-- | ||
Generic Resources | ||
--> | ||
|
||
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource BaseStyle}"> | ||
<Setter Property="MinWidth" Value="75" /> | ||
</Style> | ||
|
||
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource BaseStyle}"> | ||
<Setter Property="VerticalAlignment" Value="Center"/> | ||
<Setter Property="TextWrapping" Value="Wrap" /> | ||
</Style> | ||
|
||
</ResourceDictionary> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop"> | ||
|
||
<PropertyGroup> | ||
<OutputType>WinExe</OutputType> | ||
<TargetFrameworks>net462;net6.0-windows</TargetFrameworks> | ||
<UseWPF>true</UseWPF> | ||
<RootNamespace>Demo.StaThreads</RootNamespace> | ||
<AssemblyName>Demo.StaThreads</AssemblyName> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\MvvmDialogs.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" /> | ||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.1" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<Window | ||
x:Class="Demo.StaThreads.MainWindow" | ||
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:md="https://github.com/fantasticfiasco/mvvm-dialogs" | ||
mc:Ignorable="d" | ||
md:DialogServiceViews.IsRegistered="True" | ||
WindowStartupLocation="CenterScreen" | ||
Title="Demo - STA Threads" | ||
Width="300" | ||
Height="100"> | ||
|
||
<Window.Resources> | ||
<ResourceDictionary Source="ApplicationResources.xaml" /> | ||
</Window.Resources> | ||
|
||
<UniformGrid Columns="2" Rows="1"> | ||
<Button | ||
AutomationProperties.AutomationId="1k7d1Nm8MkOYK5qGrdVX4Q" | ||
Command="{Binding ShowMessageBoxCommand}"> | ||
<TextBlock Text="Show message box" /> | ||
</Button> | ||
<TextBlock | ||
AutomationProperties.AutomationId="kT3_ZUZfsEK1QdZ2jBfuIQ" | ||
Text="{Binding Confirmation}" /> | ||
</UniformGrid> | ||
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Demo.StaThreads; | ||
|
||
public partial class MainWindow | ||
{ | ||
public MainWindow() | ||
{ | ||
InitializeComponent(); | ||
} | ||
} |
Oops, something went wrong.