Bug 15572. Lookup KnownTypeCollection element types in MSSimpleNamespace
[mono.git] / mcs / class / WindowsBase / System.Windows.Threading / DispatcherOperation.cs
1 //
2 // Permission is hereby granted, free of charge, to any person obtaining
3 // a copy of this software and associated documentation files (the
4 // "Software"), to deal in the Software without restriction, including
5 // without limitation the rights to use, copy, modify, merge, publish,
6 // distribute, sublicense, and/or sell copies of the Software, and to
7 // permit persons to whom the Software is furnished to do so, subject to
8 // the following conditions:
9 // 
10 // The above copyright notice and this permission notice shall be
11 // included in all copies or substantial portions of the Software.
12 // 
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
17 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
19 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 //
21 // Copyright (c) 2006 Novell, Inc. (http://www.novell.com)
22 //
23 // Authors:
24 //      Miguel de Icaza (miguel@novell.com)
25 //
26 using System;
27 using System.Collections;
28 using System.Collections.Generic;
29 using System.ComponentModel;
30 using System.Security;
31 using System.Threading;
32
33 namespace System.Windows.Threading {
34
35         public sealed class DispatcherOperation {
36                 DispatcherOperationStatus status;
37                 DispatcherPriority priority;
38                 Dispatcher dispatcher;
39                 object result;
40                 Delegate delegate_method;
41                 object [] delegate_args;
42
43                 internal DispatcherOperation (Dispatcher dis, DispatcherPriority prio)
44                 {
45                         dispatcher = dis;
46                         priority = prio;
47                         if (Dispatcher.HasShutdownFinished)
48                                 status = DispatcherOperationStatus.Aborted;
49                         else
50                                 status = DispatcherOperationStatus.Pending;
51                 }
52                 
53                 internal DispatcherOperation (Dispatcher dis, DispatcherPriority prio, Delegate d)
54                         : this (dis, prio)
55                 {
56                         delegate_method = d;
57                 }
58
59                 internal DispatcherOperation (Dispatcher dis, DispatcherPriority prio, Delegate d, object arg)
60                         : this (dis, prio)
61                 {
62                         delegate_method = d;
63                         delegate_args = new object [1];
64                         delegate_args [0] = arg;
65                 }
66
67                 internal DispatcherOperation (Dispatcher dis, DispatcherPriority prio, Delegate d, object arg, object [] args)
68                         : this (dis, prio)
69                 {
70                         delegate_method = d;
71                         delegate_args = new object [args.Length + 1];
72                         delegate_args [0] = arg;
73                         Array.Copy (args, 1, delegate_args, 0, args.Length);
74                 }
75
76                 internal void Invoke ()
77                 {
78                         status = DispatcherOperationStatus.Executing;
79                         result = delegate_method.DynamicInvoke (delegate_args);
80                                 
81                         status = DispatcherOperationStatus.Completed;
82
83                         if (Completed != null)
84                                 Completed (this, EventArgs.Empty);
85                 }
86                 
87                 public bool Abort ()
88                 {
89                         status = DispatcherOperationStatus.Aborted;
90                         throw new NotImplementedException ();
91                 }
92
93                 public DispatcherOperationStatus Status {
94                         get {
95                                 return status;
96                         }
97
98                         internal set {
99                                 status = value;
100                         }
101                 }
102
103                 public Dispatcher Dispatcher {
104                         get {
105                                 return dispatcher;
106                         }
107                 }
108
109                 public DispatcherPriority Priority {
110                         get {
111                                 return priority;
112                         }
113
114                         set {
115                                 if (priority != value){
116                                         DispatcherPriority old = priority;
117                                         priority = value;
118                                         dispatcher.Reprioritize (this, old);
119                                 }
120                         }
121                 }
122
123                 public object Result {
124                         get {
125                                 return result;
126                         }
127                 }
128
129                 public DispatcherOperationStatus Wait ()
130                 {
131                         if (status == DispatcherOperationStatus.Executing)
132                                 throw new InvalidOperationException ("Already executing");
133
134                         throw new NotImplementedException ();
135                 }
136
137                 [SecurityCritical]
138                 public DispatcherOperationStatus Wait (TimeSpan timeout)
139                 {
140                         if (status == DispatcherOperationStatus.Executing)
141                                 throw new InvalidOperationException ("Already executing");
142
143                         throw new NotImplementedException ();
144                 }
145                 
146                 public event EventHandler Aborted;
147                 public event EventHandler Completed;
148         }
149 }