Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
ThreadClass.cs
Go to the documentation of this file.
1 /*
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements. See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership. The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied. See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20 */
21 
22 using System;
23 using System.Threading;
24 
25 namespace Lucene.Net.Support
26 {
27  /// <summary>
28  /// Support class used to handle threads
29  /// </summary>
31  {
32  /// <summary>
33  /// The instance of System.Threading.Thread
34  /// </summary>
35  private System.Threading.Thread threadField;
36 
37 
38  /// <summary>
39  /// Initializes a new instance of the ThreadClass class
40  /// </summary>
41  public ThreadClass()
42  {
43  threadField = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
44  }
45 
46  /// <summary>
47  /// Initializes a new instance of the Thread class.
48  /// </summary>
49  /// <param name="Name">The name of the thread</param>
50  public ThreadClass(System.String Name)
51  {
52  threadField = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
53  this.Name = Name;
54  }
55 
56  /// <summary>
57  /// Initializes a new instance of the Thread class.
58  /// </summary>
59  /// <param name="Start">A ThreadStart delegate that references the methods to be invoked when this thread begins executing</param>
60  public ThreadClass(System.Threading.ThreadStart Start)
61  {
62  threadField = new System.Threading.Thread(Start);
63  }
64 
65  /// <summary>
66  /// Initializes a new instance of the Thread class.
67  /// </summary>
68  /// <param name="Start">A ThreadStart delegate that references the methods to be invoked when this thread begins executing</param>
69  /// <param name="Name">The name of the thread</param>
70  public ThreadClass(System.Threading.ThreadStart Start, System.String Name)
71  {
72  threadField = new System.Threading.Thread(Start);
73  this.Name = Name;
74  }
75 
76  /// <summary>
77  /// This method has no functionality unless the method is overridden
78  /// </summary>
79  public virtual void Run()
80  {
81  }
82 
83  /// <summary>
84  /// Causes the operating system to change the state of the current thread instance to ThreadState.Running
85  /// </summary>
86  public virtual void Start()
87  {
88  threadField.Start();
89  }
90 
91  /// <summary>
92  /// Interrupts a thread that is in the WaitSleepJoin thread state
93  /// </summary>
94  public virtual void Interrupt()
95  {
96  threadField.Interrupt();
97  }
98 
99  /// <summary>
100  /// Gets the current thread instance
101  /// </summary>
102  public System.Threading.Thread Instance
103  {
104  get
105  {
106  return threadField;
107  }
108  set
109  {
110  threadField = value;
111  }
112  }
113 
114  /// <summary>
115  /// Gets or sets the name of the thread
116  /// </summary>
117  public System.String Name
118  {
119  get
120  {
121  return threadField.Name;
122  }
123  set
124  {
125  if (threadField.Name == null)
126  threadField.Name = value;
127  }
128  }
129 
130  public void SetDaemon(bool isDaemon)
131  {
132  threadField.IsBackground = isDaemon;
133  }
134 
135  /// <summary>
136  /// Gets or sets a value indicating the scheduling priority of a thread
137  /// </summary>
138  public System.Threading.ThreadPriority Priority
139  {
140  get
141  {
142  try
143  {
144  return threadField.Priority;
145  }
146  catch
147  {
148  return ThreadPriority.Normal;
149  }
150  }
151  set
152  {
153  try
154  {
155  threadField.Priority = value;
156  }
157  catch { }
158 
159  }
160  }
161 
162  /// <summary>
163  /// Gets a value indicating the execution status of the current thread
164  /// </summary>
165  public bool IsAlive
166  {
167  get
168  {
169  return threadField.IsAlive;
170  }
171  }
172 
173  /// <summary>
174  /// Gets or sets a value indicating whether or not a thread is a background thread.
175  /// </summary>
176  public bool IsBackground
177  {
178  get
179  {
180  return threadField.IsBackground;
181  }
182  set
183  {
184  threadField.IsBackground = value;
185  }
186  }
187 
188  /// <summary>
189  /// Blocks the calling thread until a thread terminates
190  /// </summary>
191  public void Join()
192  {
193  threadField.Join();
194  }
195 
196  /// <summary>
197  /// Blocks the calling thread until a thread terminates or the specified time elapses
198  /// </summary>
199  /// <param name="MiliSeconds">Time of wait in milliseconds</param>
200  public void Join(long MiliSeconds)
201  {
202  threadField.Join(new System.TimeSpan(MiliSeconds * 10000));
203  }
204 
205  /// <summary>
206  /// Blocks the calling thread until a thread terminates or the specified time elapses
207  /// </summary>
208  /// <param name="MiliSeconds">Time of wait in milliseconds</param>
209  /// <param name="NanoSeconds">Time of wait in nanoseconds</param>
210  public void Join(long MiliSeconds, int NanoSeconds)
211  {
212  threadField.Join(new System.TimeSpan(MiliSeconds * 10000 + NanoSeconds * 100));
213  }
214 
215  /// <summary>
216  /// Resumes a thread that has been suspended
217  /// </summary>
218  public void Resume()
219  {
220  Monitor.PulseAll(threadField);
221  }
222 
223  /// <summary>
224  /// Raises a ThreadAbortException in the thread on which it is invoked,
225  /// to begin the process of terminating the thread. Calling this method
226  /// usually terminates the thread
227  /// </summary>
228  public void Abort()
229  {
230  threadField.Abort();
231  }
232 
233  /// <summary>
234  /// Raises a ThreadAbortException in the thread on which it is invoked,
235  /// to begin the process of terminating the thread while also providing
236  /// exception information about the thread termination.
237  /// Calling this method usually terminates the thread.
238  /// </summary>
239  /// <param name="stateInfo">An object that contains application-specific information, such as state, which can be used by the thread being aborted</param>
240  public void Abort(object stateInfo)
241  {
242  threadField.Abort(stateInfo);
243  }
244 
245  /// <summary>
246  /// Suspends the thread, if the thread is already suspended it has no effect
247  /// </summary>
248  public void Suspend()
249  {
250  Monitor.Wait(threadField);
251  }
252 
253  /// <summary>
254  /// Obtain a String that represents the current object
255  /// </summary>
256  /// <returns>A String that represents the current object</returns>
257  public override System.String ToString()
258  {
259  return "Thread[" + Name + "," + Priority.ToString() + "]";
260  }
261 
262  [ThreadStatic]
263  static ThreadClass This = null;
264 
265  // named as the Java version
266  public static ThreadClass CurrentThread()
267  {
268  return Current();
269  }
270 
271  public static void Sleep(long ms)
272  {
273  // casting long ms to int ms could lose resolution, however unlikely
274  // that someone would want to sleep for that long...
275  Thread.Sleep((int)ms);
276  }
277 
278  /// <summary>
279  /// Gets the currently running thread
280  /// </summary>
281  /// <returns>The currently running thread</returns>
282  public static ThreadClass Current()
283  {
284  if (This == null)
285  {
286  This = new ThreadClass();
287  This.Instance = Thread.CurrentThread;
288  }
289  return This;
290  }
291 
292  public static bool operator ==(ThreadClass t1, object t2)
293  {
294  if (((object)t1) == null) return t2 == null;
295  return t1.Equals(t2);
296  }
297 
298  public static bool operator !=(ThreadClass t1, object t2)
299  {
300  return !(t1 == t2);
301  }
302 
303  public override bool Equals(object obj)
304  {
305  if (obj == null) return false;
306  if (obj is ThreadClass) return this.threadField.Equals(((ThreadClass)obj).threadField);
307  return false;
308  }
309 
310  public override int GetHashCode()
311  {
312  return this.threadField.GetHashCode();
313  }
314  }
315 }