a7618c472e850e68c0bfa6107db1f42a3486961f
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / EcoreSynchronizationContext.cs
1 /*
2  * Copyright (c) 2016 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.Threading;
18
19 namespace ElmSharp
20 {
21
22     /// <summary>
23     /// Provides a synchronization context for the efl application.
24     /// </summary>
25     public class EcoreSynchronizationContext : SynchronizationContext
26     {
27         /// <summary>
28         /// Initializes a new instance of the EcoreSynchronizationContext class.
29         /// </summary>
30         public EcoreSynchronizationContext()
31         {
32         }
33
34         /// <summary>
35         /// Initilizes a new EcoreSynchronizationContext and install into current thread
36         /// </summary>
37         /// <remarks>
38         /// It is equivalent
39         /// <code>
40         /// SetSynchronizationContext(new EcoreSynchronizationContext());
41         /// </code>
42         /// </remarks>
43         public static void Initialize()
44         {
45             SetSynchronizationContext(new EcoreSynchronizationContext());
46         }
47
48         /// <summary>
49         /// Dispatches an asynchronous message to a Ecore main loop.
50         /// </summary>
51         /// <param name="d"><see cref="System.Threading.SendOrPostCallback"/>The SendOrPostCallback delegate to call.</param>
52         /// <param name="state"><see cref="System.Object"/>The object passed to the delegate.</param>
53         /// <remarks>
54         /// The Post method starts an asynchronous request to post a message.</remarks>
55         public override void Post(SendOrPostCallback d, object state)
56         {
57             EcoreMainloop.PostAndWakeUp(() =>
58             {
59                 d(state);
60             });
61         }
62
63         /// <summary>
64         /// Dispatches a synchronous message to a Ecore main loop
65         /// </summary>
66         /// <param name="d"><see cref="System.Threading.SendOrPostCallback"/>The SendOrPostCallback delegate to call.</param>
67         /// <param name="state"><see cref="System.Object"/>The object passed to the delegate.</param>
68         /// <remarks>
69         /// The Send method starts a synchronous request to send a message.</remarks>
70         public override void Send(SendOrPostCallback d, object state)
71         {
72             EcoreMainloop.Send(() =>
73             {
74                 d(state);
75             });
76         }
77     }
78 }