[NUI] add checking whether NUI object is created in outside of main thread 29/161729/3
authordongsug.song <dongsug.song@samsung.com>
Mon, 27 Nov 2017 07:10:07 +0000 (16:10 +0900)
committerdongsug.song <dongsug.song@samsung.com>
Mon, 27 Nov 2017 07:22:14 +0000 (16:22 +0900)
Change-Id: I7ed069c285acafd3cfa405c11109c0a37da848c1
Signed-off-by: dongsug.song <dongsug.song@samsung.com>
src/Tizen.NUI/src/internal/Registry.cs
src/Tizen.NUI/src/public/NUIApplication.cs
test/NUITestSample/NUITestSample/examples/hello-world.cs

index 01a9168..6d2a22b 100755 (executable)
@@ -1,9 +1,23 @@
+/*
+ * Copyright(c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
 
-using System.Reflection;
 using System;
-using System.Runtime.InteropServices;
 using System.Collections.Generic;
-using Tizen.NUI.BaseComponents;
+using System.Threading;
 
 namespace Tizen.NUI
 {
@@ -13,10 +27,15 @@ namespace Tizen.NUI
     /// </summary>
     internal sealed class Registry
     {
+        private static readonly Registry registry = new Registry();
+
         /// <summary>
-        /// The registry is a singleton.
+        /// static initialization singleton
         /// </summary>
-        private static Registry instance = null;
+        internal static Registry Instance
+        {
+            get { return registry; }
+        }
 
         /// <summary>
         /// Given a C++ object, the dictionary allows us to find which C# object it belongs to.
@@ -36,6 +55,11 @@ namespace Tizen.NUI
         /// <param name="baseHandle">The instance of BaseHandle (C# base class).</param>
         internal static void Register(BaseHandle baseHandle)
         {
+            if(savedApplicationThread?.ManagedThreadId != Thread.CurrentThread.ManagedThreadId)
+            {
+                throw new global::System.ApplicationException("NUI object is attempt to be created in another thread. It should be created in main thread only!");
+            }
+
             // We store a pointer to the RefObject for the control
             RefObject refObj = baseHandle.GetObjectPtr();
             IntPtr refCptr = (IntPtr)RefObject.getCPtr(refObj);
@@ -67,18 +91,6 @@ namespace Tizen.NUI
             return;
         }
 
-        private static Registry Instance
-        {
-            get
-            {
-                if (instance == null)
-                {
-                    instance = new Registry();
-                }
-                return instance;
-            }
-        }
-
         internal static BaseHandle GetManagedBaseHandleFromNativePtr(BaseHandle baseHandle)
         {
             RefObject refObj = baseHandle.GetObjectPtr();
@@ -112,5 +124,18 @@ namespace Tizen.NUI
             }
         }
 
+        private static Thread savedApplicationThread;
+        internal Thread SavedApplicationThread
+        {
+            get
+            {
+                return savedApplicationThread;
+            }
+            set
+            {
+                savedApplicationThread = value;
+            }
+        }
+
     }
 }
index 2075225..a79785e 100755 (executable)
@@ -17,6 +17,7 @@
 
 using System;
 using System.ComponentModel;
+using System.Threading;
 using Tizen.Applications;
 using Tizen.Applications.CoreBackend;
 
@@ -52,6 +53,7 @@ namespace Tizen.NUI
         /// <since_tizen> 3 </since_tizen>
         public NUIApplication() : base(new NUICoreBackend())
         {
+            Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
         }
 
         /// <summary>
@@ -61,6 +63,7 @@ namespace Tizen.NUI
         /// <since_tizen> 3 </since_tizen>
         public NUIApplication(string styleSheet) : base(new NUICoreBackend(styleSheet))
         {
+            Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
         }
 
         /// <summary>
@@ -71,6 +74,7 @@ namespace Tizen.NUI
         /// <since_tizen> 3 </since_tizen>
         public NUIApplication(string styleSheet, WindowMode windowMode) : base(new NUICoreBackend(styleSheet, windowMode))
         {
+            Registry.Instance.SavedApplicationThread = Thread.CurrentThread;
         }
 
         /// <summary>
index 8609a96..3035d0f 100755 (executable)
@@ -61,16 +61,20 @@ namespace HelloWorldTest
             pointLabel.PointSize = 32.0f;
             window.Add(pointLabel);
 
+            Timer timer = new Timer(1000);
+
             Task.Factory.StartNew(() =>
             {
                 try
                 {
-            TextLabel ellipsis = new TextLabel("Ellipsis of TextLabel is enabled.");
-            ellipsis.Size2D = new Size2D(100, 100);
-            ellipsis.Position2D = new Position2D(10, 250);
-            ellipsis.PointSize = 20.0f;
-            ellipsis.Ellipsis = true;
-            window.Add(ellipsis);
+                    Timer timer_in_another_thread = new Timer(1000);
+
+                    TextLabel ellipsis = new TextLabel("Ellipsis of TextLabel is enabled.");
+                    ellipsis.Size2D = new Size2D(100, 100);
+                    ellipsis.Position2D = new Position2D(10, 250);
+                    ellipsis.PointSize = 20.0f;
+                    ellipsis.Ellipsis = true;
+                    window.Add(ellipsis);
                 }
                 catch (Exception e)
                 {