Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9341

Xamarin C# 非同期関数内で起きたExceptionをthrowする

$
0
0

WindowsBaseパッケージに入ってる
System.Windows.ThreadingのDispatcherを使えば、
別のスレッド上でからメインスレッドでExceptionを起こすことができる。

非同期関数~~~System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke(()=>{thrownewException("メインスレッドでExceptionが起こる");});~~~

キャッチするにはApplication.TheradExceptionにイベントを設定しておくと、
ハンドリングされなかったExceptionが飛んでくる

System.Windows.Forms.Application.ThreadException+=newSystem.Threading.ThreadExceptionEventHandler(this.OnThreadException);privatevoidOnThreadException(objectsender,ThreadExceptionEventArgse){}

Xamarin.Androidは以下でできる
Exception発生

非同期関数~~~~Xamarin.Forms.Device.BeginInvokeOnMainThread(()=>{thrownewException("メインスレッドでExceptionが起こる");});~~~

キャッチ

AppDomain.CurrentDomain.UnhandledException+=newUnhandledExceptionEventHandler(OnThreadException);

Viewing all articles
Browse latest Browse all 9341

Latest Images

Trending Articles