Skip to content

ExcelDNA Intellisense could lead to Excel hanging on exit #73

@sharpe5

Description

@sharpe5

There appears to be a potential issue in this block of code that may prevent Excel from exiting properly:

public UIMonitor(SynchronizationContext syncContextMain)
{
    _syncContextMain = syncContextMain;

    // Make a separate thread and set to MTA, according to: https://msdn.microsoft.com/en-us/library/windows/desktop/ee671692%28v=vs.85%29.aspx
    // This thread was initially intended for UI Automation calls, particularly adding and removing event handlers.
    var threadAuto = new Thread(RunUIAutomation);
    threadAuto.SetApartmentState(ApartmentState.MTA);
    threadAuto.Start();
}

Recommending adding "threadAuto.IsBackground=true" before "threadAuto.Start()".

Without this line, if this thread does not exit automatically, then this will could prevent the entire process from exiting (i.e. Excel could hang on exit).

Note that ".IsBackground" has no effect on thread priority (this is a different property). All it means is that if the process exits, this thread will not block the process exiting. Unfortunately, this is set to false by default. The general rule is that this should always be set to true, unless it is absolutely imperative that the thread does some cleanup before the process exits.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions