From c3893970a83fa3c80d45238d113e37431e25026e Mon Sep 17 00:00:00 2001 From: "dongsug.song" Date: Mon, 27 Nov 2017 16:10:07 +0900 Subject: [PATCH] [NUI] add checking whether NUI object is created in outside of main thread Change-Id: I7ed069c285acafd3cfa405c11109c0a37da848c1 Signed-off-by: dongsug.song --- src/Tizen.NUI/src/internal/Registry.cs | 59 +++++++++++++++------- src/Tizen.NUI/src/public/NUIApplication.cs | 4 ++ .../NUITestSample/examples/hello-world.cs | 16 +++--- 3 files changed, 56 insertions(+), 23 deletions(-) diff --git a/src/Tizen.NUI/src/internal/Registry.cs b/src/Tizen.NUI/src/internal/Registry.cs index 01a9168..6d2a22b 100755 --- a/src/Tizen.NUI/src/internal/Registry.cs +++ b/src/Tizen.NUI/src/internal/Registry.cs @@ -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 /// internal sealed class Registry { + private static readonly Registry registry = new Registry(); + /// - /// The registry is a singleton. + /// static initialization singleton /// - private static Registry instance = null; + internal static Registry Instance + { + get { return registry; } + } /// /// Given a C++ object, the dictionary allows us to find which C# object it belongs to. @@ -36,6 +55,11 @@ namespace Tizen.NUI /// The instance of BaseHandle (C# base class). 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; + } + } + } } diff --git a/src/Tizen.NUI/src/public/NUIApplication.cs b/src/Tizen.NUI/src/public/NUIApplication.cs index 2075225..a79785e 100755 --- a/src/Tizen.NUI/src/public/NUIApplication.cs +++ b/src/Tizen.NUI/src/public/NUIApplication.cs @@ -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 /// 3 public NUIApplication() : base(new NUICoreBackend()) { + Registry.Instance.SavedApplicationThread = Thread.CurrentThread; } /// @@ -61,6 +63,7 @@ namespace Tizen.NUI /// 3 public NUIApplication(string styleSheet) : base(new NUICoreBackend(styleSheet)) { + Registry.Instance.SavedApplicationThread = Thread.CurrentThread; } /// @@ -71,6 +74,7 @@ namespace Tizen.NUI /// 3 public NUIApplication(string styleSheet, WindowMode windowMode) : base(new NUICoreBackend(styleSheet, windowMode)) { + Registry.Instance.SavedApplicationThread = Thread.CurrentThread; } /// diff --git a/test/NUITestSample/NUITestSample/examples/hello-world.cs b/test/NUITestSample/NUITestSample/examples/hello-world.cs index 8609a96..3035d0f 100755 --- a/test/NUITestSample/NUITestSample/examples/hello-world.cs +++ b/test/NUITestSample/NUITestSample/examples/hello-world.cs @@ -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) { -- 2.7.4