From 12dfdecafabd34585f5f143e6726b249b476807d Mon Sep 17 00:00:00 2001
From: ryancheung <ryancheung.go@gmail.com>
Date: Wed, 9 Sep 2020 08:42:38 +0800
Subject: [PATCH] Fix possible crash on ThreadMgr creation

---
 ImeSharp/TextServicesContext.cs | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/ImeSharp/TextServicesContext.cs b/ImeSharp/TextServicesContext.cs
index f701edd..87e53b5 100644
--- a/ImeSharp/TextServicesContext.cs
+++ b/ImeSharp/TextServicesContext.cs
@@ -204,10 +204,17 @@ public ITfThreadMgrEx ThreadManager
             {
                 if (_threadManager == null)
                 {
-                    ITfThreadMgr threadMgr;
-                    Tsf.GetThreadMgr(out threadMgr);
+                    ITfThreadMgr threadMgr = null;
+                    try
+                    {
+                        // This might fail
+                        Tsf.GetThreadMgr(out threadMgr);
+                    }
+                    catch (SharpGen.Runtime.SharpGenException)
+                    {
+                    }
 
-                    // Dispose previous ITfThreadMgr in case something weird happens 
+                    // Dispose previous ITfThreadMgr in case something weird happens
                     if (threadMgr != null)
                     {
                         if (threadMgr.IsThreadFocus)