Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
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 {
31  {
35  private System.Threading.Thread threadField;
36 
37 
41  public ThreadClass()
42  {
43  threadField = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
44  }
45 
50  public ThreadClass(System.String Name)
51  {
52  threadField = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
53  this.Name = Name;
54  }
55 
60  public ThreadClass(System.Threading.ThreadStart Start)
61  {
62  threadField = new System.Threading.Thread(Start);
63  }
64 
70  public ThreadClass(System.Threading.ThreadStart Start, System.String Name)
71  {
72  threadField = new System.Threading.Thread(Start);
73  this.Name = Name;
74  }
75 
79  public virtual void Run()
80  {
81  }
82 
86  public virtual void Start()
87  {
88  threadField.Start();
89  }
90 
94  public virtual void Interrupt()
95  {
96  threadField.Interrupt();
97  }
98 
102  public System.Threading.Thread Instance
103  {
104  get
105  {
106  return threadField;
107  }
108  set
109  {
110  threadField = value;
111  }
112  }
113 
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 
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 
165  public bool IsAlive
166  {
167  get
168  {
169  return threadField.IsAlive;
170  }
171  }
172 
176  public bool IsBackground
177  {
178  get
179  {
180  return threadField.IsBackground;
181  }
182  set
183  {
184  threadField.IsBackground = value;
185  }
186  }
187 
191  public void Join()
192  {
193  threadField.Join();
194  }
195 
200  public void Join(long MiliSeconds)
201  {
202  threadField.Join(new System.TimeSpan(MiliSeconds * 10000));
203  }
204 
210  public void Join(long MiliSeconds, int NanoSeconds)
211  {
212  threadField.Join(new System.TimeSpan(MiliSeconds * 10000 + NanoSeconds * 100));
213  }
214 
218  public void Resume()
219  {
220  Monitor.PulseAll(threadField);
221  }
222 
228  public void Abort()
229  {
230  threadField.Abort();
231  }
232 
240  public void Abort(object stateInfo)
241  {
242  threadField.Abort(stateInfo);
243  }
244 
248  public void Suspend()
249  {
250  Monitor.Wait(threadField);
251  }
252 
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 
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 }