[Applications.Common] Remove warning messages (#5532)
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.Common / Tizen.Applications / TizenUISynchronizationContext.cs
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Collections.Concurrent;
19 using System.ComponentModel;
20 using System.Threading;
21
22 namespace Tizen.Applications
23 {
24     /// <summary>
25     /// Provides a synchronization context for the Tizen thread application model.
26     /// </summary>
27     /// <since_tizen> 10 </since_tizen>
28     [EditorBrowsable(EditorBrowsableState.Never)]
29     public class TizenUISynchronizationContext : SynchronizationContext
30     {
31         /// <summary>
32         /// Initilizes a new TizenUISynchronizationContext and install into the current thread.
33         /// </summary>
34         /// <remarks>
35         /// It is equivalent.
36         /// <code>
37         /// SetSynchronizationContext(new TizenUISynchronizationContext());
38         /// </code>
39         /// </remarks>
40         /// <since_tizen> 10 </since_tizen>
41         public static void Initialize()
42         {
43             SetSynchronizationContext(new TizenUISynchronizationContext());
44         }
45
46         /// <summary>
47         /// Dispatches an asynchronous message to a Tizen main loop of the UI thread.
48         /// </summary>
49         /// <param name="d"><see cref="System.Threading.SendOrPostCallback"/>The SendOrPostCallback delegate to call.</param>
50         /// <param name="state"><see cref="System.Object"/>The object passed to the delegate.</param>
51         /// <remarks>
52         /// The post method starts an asynchronous request to post a message.</remarks>
53         /// <since_tizen> 10 </since_tizen>
54         public override void Post(SendOrPostCallback d, object state)
55         {
56             GSourceManager.Post(() =>
57             {
58                 d(state);
59             }, true);
60         }
61
62         /// <summary>
63         /// Dispatches a synchronous message to a Tizen main loop of the UI thread.
64         /// </summary>
65         /// <param name="d"><see cref="System.Threading.SendOrPostCallback"/>The SendOrPostCallback delegate to call.</param>
66         /// <param name="state"><see cref="System.Object"/>The object passed to the delegate.</param>
67         /// <remarks>
68         /// The send method starts a synchronous request to send a message.</remarks>
69         /// <since_tizen> 10 </since_tizen>
70         public override void Send(SendOrPostCallback d, object state)
71         {
72             using (var mre = new ManualResetEvent(false))
73             {
74                 Exception err = null;
75                 GSourceManager.Post(() =>
76                 {
77 #pragma warning disable CA1031
78                     try
79                     {
80                         d(state);
81                     }
82                     catch (Exception ex)
83                     {
84                         err = ex;
85                     }
86                     finally
87                     {
88                         mre.Set();
89                     }
90 #pragma warning restore CA1031
91                 }, true);
92                 mre.WaitOne();
93                 if (err != null)
94                 {
95                     throw err;
96                 }
97             }
98         }
99     }
100 }