LoginSignup
0
1

More than 5 years have passed since last update.

Visual Studio / WPF > UIスレッド以外からの呼び出しで例外発生時 > try, catchでとらえたex

Last updated at Posted at 2017-04-27
動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2

@ WPF 4.5入門 by 大田一希さん
No.5290 / 9985

UIスレッド以外からの呼び出しで例外が出ることが紹介されている。

以下のDebug出力にてどういうメッセージが表示されるか気になった。

Debug.WriteLine(ex);
XAML
<Window x:Class="_170428_t0820_dispatcher.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:local="clr-namespace:_170428_t0820_dispatcher"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel Margin="0,20">
            <Button Content="Button1" Click="Button1_Click"/>
            <Button Content="Button2" Click="Button2_Click"/>
            <Button Content="Button3" Click="Button3_Click"/>
        </StackPanel>
    </Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading; //
using System.Diagnostics; // 

namespace _170428_t0820_dispatcher
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            var d = new DerivedObject();
            d.DoSomething();
        }

        private async void Button2_Click(object sender, RoutedEventArgs e)
        {
            var d = new DerivedObject();
            try
            {
                await Task.Run(() => d.DoSomething());
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Line45");
                Debug.WriteLine(ex);
                Debug.WriteLine("Line47");
            }
        }

        private void Button3_Click(object sender, RoutedEventArgs e)
        {

        }
    }

    public class DerivedObject : DispatcherObject
    {
        public void DoSomething()
        {
            this.VerifyAccess();
            MessageBox.Show("Hello");
        }
    }
}

exの内容

Line45
System.InvalidOperationException: このオブジェクトは別のスレッドに所有されているため、呼び出しスレッドはこのオブジェクトにアクセスできません。
   場所 System.Windows.Threading.Dispatcher.VerifyAccess()
   場所 System.Windows.Threading.DispatcherObject.VerifyAccess()
   場所 _170428_t0820_dispatcher.DerivedObject.DoSomething() 場所 c:\users\xxx\documents\visual studio 2017\Projects\170428_t0820_dispatcher\170428_t0820_dispatcher\MainWindow.xaml.cs:行 61
   場所 _170428_t0820_dispatcher.MainWindow.<>c__DisplayClass2_0.<Button2_Click>b__0() 場所 c:\users\xxx\documents\visual studio 2017\Projects\170428_t0820_dispatcher\170428_t0820_dispatcher\MainWindow.xaml.cs:行 41
   場所 System.Threading.Tasks.Task.InnerInvoke()
   場所 System.Threading.Tasks.Task.Execute()
--- 直前に例外がスローされた場所からのスタック トレースの終わり ---
   場所 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   場所 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   場所 System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   場所 _170428_t0820_dispatcher.MainWindow.<Button2_Click>d__2.MoveNext() 場所 c:\users\xxx\documents\visual studio 2017\Projects\170428_t0820_dispatcher\170428_t0820_dispatcher\MainWindow.xaml.cs:行 41
Line47
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1