如果后台线程中有未处理的异常,如何使主进程崩溃

时间:2018-08-21 09:14:50

标签: c# multithreading com background-thread

我有一个发布者,调度员,订阅者。

publisher通过调度程序向订阅者发布事件。

订户是嵌入在S.exe中的com对象,当特定事件来自Publisher.exe时可以从dispatcher.exe中回调。

我希望订阅者内部发生任何异常,以终止S.exe。

我做了调查:

  1. 任务,带有 配置可以终止主进程。相关文章, https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.taskscheduler.unobservedtaskexception?view=netframework-4.7.2

  2. 在我的代码中新建一个线程,无需任何特定配置即可     另外,线程内任何未处理的异常都可能导致主程序崩溃     处理。相关文章,     https://docs.microsoft.com/en-us/dotnet/standard/threading/exceptions-in-managed-threads

一个线程的两个属性:{isbackground,isThreadPoolThread}。

  1. 任务是{true,true}
  2. 人造线程为{true,false},
  3. 用户回调线程为{true,false}。

是否还有其他配置(例如)可以设置为控制是否使主进程崩溃?

2 个答案:

答案 0 :(得分:0)

您可以处理<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorBackground" tools:context=".MainActivity"> <android.support.design.widget.AppBarLayout android:id="@+id/abMainLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:elevation="0dp"> <android.support.v7.widget.Toolbar android:id="@+id/abMain" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorBackgroundAccent" android:theme="@style/ThemeOverlay.AppCompat.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> </android.support.design.widget.AppBarLayout> <android.support.design.widget.FloatingActionButton android:id="@+id/fabMain" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end|bottom" android:layout_margin="16dp" android:layout_marginBottom="8dp" android:layout_marginEnd="8dp" android:backgroundTint="@color/colorBackground" android:src="@drawable/ic_note_add" app:borderWidth="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" /> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/refreshMain" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/abMainLayout"> <android.support.v4.widget.NestedScrollView android:id="@+id/scrollMain" android:layout_width="match_parent" android:layout_height="wrap_content" android:focusableInTouchMode="true" app:layout_constraintTop_toBottomOf="@+id/abMainLayout"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <GridLayout android:id="@+id/gridMoney" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorBackgroundAccent" android:orientation="horizontal" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingTop="24dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/abMain"> <TextView android:id="@+id/sumTxt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_columnSpan="5" android:layout_columnWeight="1" android:layout_gravity="center" android:layout_marginBottom="16dp" android:layout_marginTop="8dp" android:layout_row="1" android:textColor="@color/colorDarkText" android:textSize="50sp" android:textStyle="bold" /> <ImageView android:id="@+id/moneyImg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="1" android:layout_columnWeight="1" android:layout_gravity="right|center_vertical" android:layout_row="2" android:background="@android:color/transparent" android:scaleX="0.5" android:scaleY="0.5" app:srcCompat="@drawable/ic_money" /> <TextView android:id="@+id/moneyTxt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="2" android:layout_columnWeight="1" android:layout_gravity="left|center_vertical" android:layout_row="2" android:background="@android:color/transparent" android:textColor="@color/colorLightText" android:textSize="15sp" /> <ImageView android:id="@+id/sharesImg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="3" android:layout_columnWeight="1" android:layout_gravity="right|center_vertical" android:layout_row="2" android:background="@android:color/transparent" android:scaleX="0.5" android:scaleY="0.5" app:srcCompat="@drawable/outline_assessment_black_36" /> <TextView android:id="@+id/sharesTxt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="4" android:layout_columnWeight="1" android:layout_gravity="left|center_vertical" android:layout_row="2" android:background="@android:color/transparent" android:textColor="@color/colorLightText" android:textSize="15sp" /> </GridLayout> <View android:id="@+id/dividerMoney" android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/colorDivider" app:layout_constraintTop_toBottomOf="@id/gridMoney" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginEnd="@dimen/recycler_horizontal_margin" android:layout_marginStart="@dimen/recycler_horizontal_margin" android:layout_marginTop="@dimen/activity_vertical_margin" android:text="Aktien" android:textColor="@color/colorPrimary" android:textStyle="bold" app:layout_constraintEnd_toEndOf="@+id/dividerMoney" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/gridMoney" /> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerShares" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="@dimen/recycler_title_bottom_margin" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView" /> </LinearLayout> </android.support.v4.widget.NestedScrollView> </android.support.v4.widget.SwipeRefreshLayout> </android.support.constraint.ConstraintLayout> 事件,在该事件中,您可以使用AppDomain.CurrentDomain.UnhandledException删除当前进程,并终止该进程,以便您的Process.GetCurrentProcess()将终止。

将此行exe放在Main()中:

Program.cs

然后实施以下的AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException

CurrentDomain_UnhandledException

根据您的问题陈述,这只是概念。您可以根据需要进行更改。

答案 1 :(得分:0)

RCW将COM内的任何异常包装为HRESULT返回给它的客户端。 因此,当在COM线程中引发异常时,无法使主进程崩溃。

答案可能与以下方面有关: COM方法通过返回HRESULT报告错误; .NET方法通过抛出异常来报告它们。运行时处理两者之间的过渡。 .NET Framework中的每个异常类都映射到一个HRESULT。

来自https://docs.microsoft.com/en-us/dotnet/framework/interop/how-to-map-hresults-and-exceptions

和其他有用的链接: https://docs.microsoft.com/en-us/dotnet/framework/interop/runtime-callable-wrapper

https://docs.microsoft.com/en-us/dotnet/framework/interop/com-callable-wrapper