2 * Copyright 2004 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 /// This interface should be implemented by any class whose instances are intended
20 /// to be executed by a thread.
22 public interface IThreadRunnable
25 /// This method has to be implemented in order that starting of the thread causes the object's
26 /// run method to be called in that separately executing thread.
32 /// Contains conversion support elements such as classes, interfaces and static methods.
34 public class SupportClass
37 /// Support class used to handle threads
39 public class ThreadClass
: IThreadRunnable
42 /// The instance of System.Threading.Thread
44 private System
.Threading
.Thread threadField
;
47 /// Initializes a new instance of the ThreadClass class
51 threadField
= new System
.Threading
.Thread(new System
.Threading
.ThreadStart(Run
));
55 /// Initializes a new instance of the Thread class.
57 /// <param name="Name">The name of the thread</param>
58 public ThreadClass(System
.String Name
)
60 threadField
= new System
.Threading
.Thread(new System
.Threading
.ThreadStart(Run
));
65 /// Initializes a new instance of the Thread class.
67 /// <param name="Start">A ThreadStart delegate that references the methods to be invoked when this thread begins executing</param>
68 public ThreadClass(System
.Threading
.ThreadStart Start
)
70 threadField
= new System
.Threading
.Thread(Start
);
74 /// Initializes a new instance of the Thread class.
76 /// <param name="Start">A ThreadStart delegate that references the methods to be invoked when this thread begins executing</param>
77 /// <param name="Name">The name of the thread</param>
78 public ThreadClass(System
.Threading
.ThreadStart Start
, System
.String Name
)
80 threadField
= new System
.Threading
.Thread(Start
);
85 /// This method has no functionality unless the method is overridden
87 public virtual void Run()
92 /// Causes the operating system to change the state of the current thread instance to ThreadState.Running
94 public virtual void Start()
100 /// Interrupts a thread that is in the WaitSleepJoin thread state
102 public virtual void Interrupt()
104 threadField
.Interrupt();
108 /// Gets the current thread instance
110 public System
.Threading
.Thread Instance
123 /// Gets or sets the name of the thread
125 public System
.String Name
129 return threadField
.Name
;
133 if (threadField
.Name
== null)
134 threadField
.Name
= value;
139 /// Gets or sets a value indicating the scheduling priority of a thread
141 public System
.Threading
.ThreadPriority Priority
145 return threadField
.Priority
;
149 threadField
.Priority
= value;
154 /// Gets a value indicating the execution status of the current thread
160 return threadField
.IsAlive
;
165 /// Gets or sets a value indicating whether or not a thread is a background thread.
167 public bool IsBackground
171 return threadField
.IsBackground
;
175 threadField
.IsBackground
= value;
180 /// Blocks the calling thread until a thread terminates
188 /// Blocks the calling thread until a thread terminates or the specified time elapses
190 /// <param name="MiliSeconds">Time of wait in milliseconds</param>
191 public void Join(long MiliSeconds
)
195 threadField
.Join(new System
.TimeSpan(MiliSeconds
* 10000));
200 /// Blocks the calling thread until a thread terminates or the specified time elapses
202 /// <param name="MiliSeconds">Time of wait in milliseconds</param>
203 /// <param name="NanoSeconds">Time of wait in nanoseconds</param>
204 public void Join(long MiliSeconds
, int NanoSeconds
)
208 threadField
.Join(new System
.TimeSpan(MiliSeconds
* 10000 + NanoSeconds
* 100));
213 /// Resumes a thread that has been suspended
217 threadField
.Resume();
221 /// Raises a ThreadAbortException in the thread on which it is invoked,
222 /// to begin the process of terminating the thread. Calling this method
223 /// usually terminates the thread
231 /// Raises a ThreadAbortException in the thread on which it is invoked,
232 /// to begin the process of terminating the thread while also providing
233 /// exception information about the thread termination.
234 /// Calling this method usually terminates the thread.
236 /// <param name="stateInfo">An object that contains application-specific information, such as state, which can be used by the thread being aborted</param>
237 public void Abort(System
.Object stateInfo
)
241 threadField
.Abort(stateInfo
);
246 /// Suspends the thread, if the thread is already suspended it has no effect
248 public void Suspend()
250 threadField
.Suspend();
254 /// Obtain a String that represents the current Object
256 /// <returns>A String that represents the current Object</returns>
257 public override System
.String
ToString()
259 return "Thread[" + Name
+ "," + Priority
.ToString() + "," + "" + "]";
263 /// Gets the currently running thread
265 /// <returns>The currently running thread</returns>
266 public static ThreadClass
Current()
268 ThreadClass CurrentThread
= new ThreadClass();
269 CurrentThread
.Instance
= System
.Threading
.Thread
.CurrentThread
;
270 return CurrentThread
;
275 /// A simple class for number conversions.
282 public const int MIN_RADIX
= 2;
286 public const int MAX_RADIX
= 36;
288 private const System
.String digits
= "0123456789abcdefghijklmnopqrstuvwxyz";
290 public static System
.String
ToString(float f
)
292 if (((float)(int)f
) == f
)
294 return ((int)f
).ToString() + ".0";
298 return f
.ToString(System
.Globalization
.NumberFormatInfo
.InvariantInfo
);
303 /// Converts a number to System.String in the specified radix.
305 /// <param name="i">A number to be converted.</param>
306 /// <param name="radix">A radix.</param>
307 /// <returns>A System.String representation of the number in the specified redix.</returns>
308 public static System
.String
ToString(long i
, int radix
)
310 if (radix
< MIN_RADIX
|| radix
> MAX_RADIX
)
313 char[] buf
= new char[65];
315 bool negative
= (i
< 0);
324 buf
[charPos
--] = digits
[(int)(-(i
% radix
))];
327 buf
[charPos
] = digits
[(int)(-i
)];
331 buf
[--charPos
] = '-';
334 return new System
.String(buf
, charPos
, (65 - charPos
));
338 /// Parses a number in the specified radix.
340 /// <param name="s">An input System.String.</param>
341 /// <param name="radix">A radix.</param>
342 /// <returns>The parsed number in the specified radix.</returns>
343 public static long Parse(System
.String s
, int radix
)
347 throw new ArgumentException("null");
350 if (radix
< MIN_RADIX
)
352 throw new NotSupportedException("radix " + radix
+
353 " less than Number.MIN_RADIX");
355 if (radix
> MAX_RADIX
)
357 throw new NotSupportedException("radix " + radix
+
358 " greater than Number.MAX_RADIX");
366 for (int i
= s
.Length
- 1; i
>= 0; i
--)
368 int weight
= digits
.IndexOf(s
[i
]);
370 throw new FormatException("Invalid number for the specified radix");
372 result
+= (weight
* mult
);
381 /// Mimics Java's Character class.
383 public class Character
385 private const char charNull
= '\0';
386 private const char charZero
= '0';
387 private const char charA
= 'a';
391 public static int MAX_RADIX
401 public static int MIN_RADIX
412 /// <param name="digit"></param>
413 /// <param name="radix"></param>
414 /// <returns></returns>
415 public static char ForDigit(int digit
, int radix
)
417 // if radix or digit is out of range,
418 // return the null character.
419 if (radix
< Character
.MIN_RADIX
)
421 if (radix
> Character
.MAX_RADIX
)
428 // if digit is less than 10,
429 // return '0' plus digit
431 return (char) ( (int) charZero
+ digit
);
433 // otherwise, return 'a' plus digit.
434 return (char) ((int) charA
+ digit
- 10);
446 /// <param name="dateTime"></param>
447 /// <returns></returns>
448 static public long GetTime(DateTime dateTime
)
450 TimeSpan ts
= dateTime
.Subtract(new DateTime(1970, 1, 1));
451 ts
= ts
.Subtract(TimeZone
.CurrentTimeZone
.GetUtcOffset(dateTime
));
452 return ts
.Ticks
/ TimeSpan
.TicksPerMillisecond
;
464 /// <param name="s"></param>
465 /// <param name="style"></param>
466 /// <param name="provider"></param>
467 /// <returns></returns>
468 public static System
.Single
Parse(System
.String s
, System
.Globalization
.NumberStyles style
, System
.IFormatProvider provider
)
472 if (s
.EndsWith("f") || s
.EndsWith("F"))
473 return System
.Single
.Parse(s
.Substring(0, s
.Length
- 1), style
, provider
);
475 return System
.Single
.Parse(s
, style
, provider
);
477 catch (System
.FormatException fex
)
486 /// <param name="s"></param>
487 /// <param name="provider"></param>
488 /// <returns></returns>
489 public static System
.Single
Parse(System
.String s
, System
.IFormatProvider provider
)
493 if (s
.EndsWith("f") || s
.EndsWith("F"))
494 return System
.Single
.Parse(s
.Substring(0, s
.Length
- 1), provider
);
496 return System
.Single
.Parse(s
, provider
);
498 catch (System
.FormatException fex
)
507 /// <param name="s"></param>
508 /// <param name="style"></param>
509 /// <returns></returns>
510 public static System
.Single
Parse(System
.String s
, System
.Globalization
.NumberStyles style
)
514 if (s
.EndsWith("f") || s
.EndsWith("F"))
515 return System
.Single
.Parse(s
.Substring(0, s
.Length
- 1), style
);
517 return System
.Single
.Parse(s
, style
);
519 catch(System
.FormatException fex
)
528 /// <param name="s"></param>
529 /// <returns></returns>
530 public static System
.Single
Parse(System
.String s
)
534 if (s
.EndsWith("f") || s
.EndsWith("F"))
535 return System
.Single
.Parse(s
.Substring(0, s
.Length
- 1));
537 return System
.Single
.Parse(s
);
539 catch(System
.FormatException fex
)
549 public class AppSettings
554 /// <param name="key"></param>
555 /// <param name="defValue"></param>
556 /// <returns></returns>
557 public static int Get(System
.String key
, int defValue
)
559 System
.String theValue
= System
.Configuration
.ConfigurationSettings
.AppSettings
.Get(key
);
560 if (theValue
== null)
564 return System
.Convert
.ToInt16(theValue
.Trim());
570 /// <param name="key"></param>
571 /// <param name="defValue"></param>
572 /// <returns></returns>
573 public static long Get(System
.String key
, long defValue
)
575 System
.String theValue
= System
.Configuration
.ConfigurationSettings
.AppSettings
.Get(key
);
576 if (theValue
== null)
580 return System
.Convert
.ToInt32(theValue
.Trim());
586 /// <param name="key"></param>
587 /// <param name="defValue"></param>
588 /// <returns></returns>
589 public static System
.String
Get(System
.String key
, System
.String defValue
)
591 System
.String theValue
= System
.Configuration
.ConfigurationSettings
.AppSettings
.Get(key
);
592 if (theValue
== null)