History

Renamed folders and separated GSF.Media and GSF.Net into separate projects.

StephenCWills committed 14 years ago

Commit ac1ef661 parent 45bce55628 files · +733 −461Browse files

Showing the changes to Source/Libraries/GSF.Core/Threading/ManagedThread.cs (1 of 628 files). Show all

Unified · Split

Source/Libraries/GSF.Core/Threading/ManagedThread.cs+715−0
@@ -0,0 +1,715 @@
1//*******************************************************************************************************
2// ManagedThread.cs - Gbtc
3//
4// Tennessee Valley Authority, 2009
5// No copyright is claimed pursuant to 17 USC � 105. All Other Rights Reserved.
6//
7// This software is made freely available under the TVA Open Source Agreement (see below).
8// Code in this file licensed to GSF under one or more contributor license agreements listed below.
9//
10// Code Modification History:
11// -----------------------------------------------------------------------------------------------------
12// 01/25/2008 - J. Ritchie Carroll
13// Initial version of source generated.
14// 09/11/2008 - J. Ritchie Carroll
15// Converted to C#.
16// 08/10/2009 - Josh L. Patterson
17// Edited Comments.
18// 09/14/2009 - Stephen C. Wills
19// Added new header and license agreement.
20// 09/22/2011 - J. Ritchie Carroll
21// Added Mono implementation exception regions.
22//
23//*******************************************************************************************************
24
25#region [ TVA Open Source Agreement ]
26/*
27
28 THIS OPEN SOURCE AGREEMENT ("AGREEMENT") DEFINES THE RIGHTS OF USE,REPRODUCTION, DISTRIBUTION,
29 MODIFICATION AND REDISTRIBUTION OF CERTAIN COMPUTER SOFTWARE ORIGINALLY RELEASED BY THE
30 TENNESSEE VALLEY AUTHORITY, A CORPORATE AGENCY AND INSTRUMENTALITY OF THE UNITED STATES GOVERNMENT
31 ("GOVERNMENT AGENCY"). GOVERNMENT AGENCY IS AN INTENDED THIRD-PARTY BENEFICIARY OF ALL SUBSEQUENT
32 DISTRIBUTIONS OR REDISTRIBUTIONS OF THE SUBJECT SOFTWARE. ANYONE WHO USES, REPRODUCES, DISTRIBUTES,
33 MODIFIES OR REDISTRIBUTES THE SUBJECT SOFTWARE, AS DEFINED HEREIN, OR ANY PART THEREOF, IS, BY THAT
34 ACTION, ACCEPTING IN FULL THE RESPONSIBILITIES AND OBLIGATIONS CONTAINED IN THIS AGREEMENT.
35
36 Original Software Designation: openPDC
37 Original Software Title: The GSF Open Source Phasor Data Concentrator
38 User Registration Requested. Please Visit https://naspi.tva.com/Registration/
39 Point of Contact for Original Software: J. Ritchie Carroll <mailto:jrcarrol@tva.gov>
40
41 1. DEFINITIONS
42
43 A. "Contributor" means Government Agency, as the developer of the Original Software, and any entity
44 that makes a Modification.
45
46 B. "Covered Patents" mean patent claims licensable by a Contributor that are necessarily infringed by
47 the use or sale of its Modification alone or when combined with the Subject Software.
48
49 C. "Display" means the showing of a copy of the Subject Software, either directly or by means of an
50 image, or any other device.
51
52 D. "Distribution" means conveyance or transfer of the Subject Software, regardless of means, to
53 another.
54
55 E. "Larger Work" means computer software that combines Subject Software, or portions thereof, with
56 software separate from the Subject Software that is not governed by the terms of this Agreement.
57
58 F. "Modification" means any alteration of, including addition to or deletion from, the substance or
59 structure of either the Original Software or Subject Software, and includes derivative works, as that
60 term is defined in the Copyright Statute, 17 USC � 101. However, the act of including Subject Software
61 as part of a Larger Work does not in and of itself constitute a Modification.
62
63 G. "Original Software" means the computer software first released under this Agreement by Government
64 Agency entitled openPDC, including source code, object code and accompanying documentation, if any.
65
66 H. "Recipient" means anyone who acquires the Subject Software under this Agreement, including all
67 Contributors.
68
69 I. "Redistribution" means Distribution of the Subject Software after a Modification has been made.
70
71 J. "Reproduction" means the making of a counterpart, image or copy of the Subject Software.
72
73 K. "Sale" means the exchange of the Subject Software for money or equivalent value.
74
75 L. "Subject Software" means the Original Software, Modifications, or any respective parts thereof.
76
77 M. "Use" means the application or employment of the Subject Software for any purpose.
78
79 2. GRANT OF RIGHTS
80
81 A. Under Non-Patent Rights: Subject to the terms and conditions of this Agreement, each Contributor,
82 with respect to its own contribution to the Subject Software, hereby grants to each Recipient a
83 non-exclusive, world-wide, royalty-free license to engage in the following activities pertaining to
84 the Subject Software:
85
86 1. Use
87
88 2. Distribution
89
90 3. Reproduction
91
92 4. Modification
93
94 5. Redistribution
95
96 6. Display
97
98 B. Under Patent Rights: Subject to the terms and conditions of this Agreement, each Contributor, with
99 respect to its own contribution to the Subject Software, hereby grants to each Recipient under Covered
100 Patents a non-exclusive, world-wide, royalty-free license to engage in the following activities
101 pertaining to the Subject Software:
102
103 1. Use
104
105 2. Distribution
106
107 3. Reproduction
108
109 4. Sale
110
111 5. Offer for Sale
112
113 C. The rights granted under Paragraph B. also apply to the combination of a Contributor's Modification
114 and the Subject Software if, at the time the Modification is added by the Contributor, the addition of
115 such Modification causes the combination to be covered by the Covered Patents. It does not apply to
116 any other combinations that include a Modification.
117
118 D. The rights granted in Paragraphs A. and B. allow the Recipient to sublicense those same rights.
119 Such sublicense must be under the same terms and conditions of this Agreement.
120
121 3. OBLIGATIONS OF RECIPIENT
122
123 A. Distribution or Redistribution of the Subject Software must be made under this Agreement except for
124 additions covered under paragraph 3H.
125
126 1. Whenever a Recipient distributes or redistributes the Subject Software, a copy of this Agreement
127 must be included with each copy of the Subject Software; and
128
129 2. If Recipient distributes or redistributes the Subject Software in any form other than source code,
130 Recipient must also make the source code freely available, and must provide with each copy of the
131 Subject Software information on how to obtain the source code in a reasonable manner on or through a
132 medium customarily used for software exchange.
133
134 B. Each Recipient must ensure that the following copyright notice appears prominently in the Subject
135 Software:
136
137 No copyright is claimed pursuant to 17 USC � 105. All Other Rights Reserved.
138
139 C. Each Contributor must characterize its alteration of the Subject Software as a Modification and
140 must identify itself as the originator of its Modification in a manner that reasonably allows
141 subsequent Recipients to identify the originator of the Modification. In fulfillment of these
142 requirements, Contributor must include a file (e.g., a change log file) that describes the alterations
143 made and the date of the alterations, identifies Contributor as originator of the alterations, and
144 consents to characterization of the alterations as a Modification, for example, by including a
145 statement that the Modification is derived, directly or indirectly, from Original Software provided by
146 Government Agency. Once consent is granted, it may not thereafter be revoked.
147
148 D. A Contributor may add its own copyright notice to the Subject Software. Once a copyright notice has
149 been added to the Subject Software, a Recipient may not remove it without the express permission of
150 the Contributor who added the notice.
151
152 E. A Recipient may not make any representation in the Subject Software or in any promotional,
153 advertising or other material that may be construed as an endorsement by Government Agency or by any
154 prior Recipient of any product or service provided by Recipient, or that may seek to obtain commercial
155 advantage by the fact of Government Agency's or a prior Recipient's participation in this Agreement.
156
157 F. In an effort to track usage and maintain accurate records of the Subject Software, each Recipient,
158 upon receipt of the Subject Software, is requested to register with Government Agency by visiting the
159 following website: https://naspi.tva.com/Registration/. Recipient's name and personal information
160 shall be used for statistical purposes only. Once a Recipient makes a Modification available, it is
161 requested that the Recipient inform Government Agency at the web site provided above how to access the
162 Modification.
163
164 G. Each Contributor represents that that its Modification does not violate any existing agreements,
165 regulations, statutes or rules, and further that Contributor has sufficient rights to grant the rights
166 conveyed by this Agreement.
167
168 H. A Recipient may choose to offer, and to charge a fee for, warranty, support, indemnity and/or
169 liability obligations to one or more other Recipients of the Subject Software. A Recipient may do so,
170 however, only on its own behalf and not on behalf of Government Agency or any other Recipient. Such a
171 Recipient must make it absolutely clear that any such warranty, support, indemnity and/or liability
172 obligation is offered by that Recipient alone. Further, such Recipient agrees to indemnify Government
173 Agency and every other Recipient for any liability incurred by them as a result of warranty, support,
174 indemnity and/or liability offered by such Recipient.
175
176 I. A Recipient may create a Larger Work by combining Subject Software with separate software not
177 governed by the terms of this agreement and distribute the Larger Work as a single product. In such
178 case, the Recipient must make sure Subject Software, or portions thereof, included in the Larger Work
179 is subject to this Agreement.
180
181 J. Notwithstanding any provisions contained herein, Recipient is hereby put on notice that export of
182 any goods or technical data from the United States may require some form of export license from the
183 U.S. Government. Failure to obtain necessary export licenses may result in criminal liability under
184 U.S. laws. Government Agency neither represents that a license shall not be required nor that, if
185 required, it shall be issued. Nothing granted herein provides any such export license.
186
187 4. DISCLAIMER OF WARRANTIES AND LIABILITIES; WAIVER AND INDEMNIFICATION
188
189 A. No Warranty: THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY KIND, EITHER
190 EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY THAT THE SUBJECT
191 SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
192 PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL BE ERROR
193 FREE, OR ANY WARRANTY THAT DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE. THIS
194 AGREEMENT DOES NOT, IN ANY MANNER, CONSTITUTE AN ENDORSEMENT BY GOVERNMENT AGENCY OR ANY PRIOR
195 RECIPIENT OF ANY RESULTS, RESULTING DESIGNS, HARDWARE, SOFTWARE PRODUCTS OR ANY OTHER APPLICATIONS
196 RESULTING FROM USE OF THE SUBJECT SOFTWARE. FURTHER, GOVERNMENT AGENCY DISCLAIMS ALL WARRANTIES AND
197 LIABILITIES REGARDING THIRD-PARTY SOFTWARE, IF PRESENT IN THE ORIGINAL SOFTWARE, AND DISTRIBUTES IT
198 "AS IS."
199
200 B. Waiver and Indemnity: RECIPIENT AGREES TO WAIVE ANY AND ALL CLAIMS AGAINST GOVERNMENT AGENCY, ITS
201 AGENTS, EMPLOYEES, CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY PRIOR RECIPIENT. IF RECIPIENT'S USE
202 OF THE SUBJECT SOFTWARE RESULTS IN ANY LIABILITIES, DEMANDS, DAMAGES, EXPENSES OR LOSSES ARISING FROM
203 SUCH USE, INCLUDING ANY DAMAGES FROM PRODUCTS BASED ON, OR RESULTING FROM, RECIPIENT'S USE OF THE
204 SUBJECT SOFTWARE, RECIPIENT SHALL INDEMNIFY AND HOLD HARMLESS GOVERNMENT AGENCY, ITS AGENTS,
205 EMPLOYEES, CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY PRIOR RECIPIENT, TO THE EXTENT PERMITTED BY
206 LAW. THE FOREGOING RELEASE AND INDEMNIFICATION SHALL APPLY EVEN IF THE LIABILITIES, DEMANDS, DAMAGES,
207 EXPENSES OR LOSSES ARE CAUSED, OCCASIONED, OR CONTRIBUTED TO BY THE NEGLIGENCE, SOLE OR CONCURRENT, OF
208 GOVERNMENT AGENCY OR ANY PRIOR RECIPIENT. RECIPIENT'S SOLE REMEDY FOR ANY SUCH MATTER SHALL BE THE
209 IMMEDIATE, UNILATERAL TERMINATION OF THIS AGREEMENT.
210
211 5. GENERAL TERMS
212
213 A. Termination: This Agreement and the rights granted hereunder will terminate automatically if a
214 Recipient fails to comply with these terms and conditions, and fails to cure such noncompliance within
215 thirty (30) days of becoming aware of such noncompliance. Upon termination, a Recipient agrees to
216 immediately cease use and distribution of the Subject Software. All sublicenses to the Subject
217 Software properly granted by the breaching Recipient shall survive any such termination of this
218 Agreement.
219
220 B. Severability: If any provision of this Agreement is invalid or unenforceable under applicable law,
221 it shall not affect the validity or enforceability of the remainder of the terms of this Agreement.
222
223 C. Applicable Law: This Agreement shall be subject to United States federal law only for all purposes,
224 including, but not limited to, determining the validity of this Agreement, the meaning of its
225 provisions and the rights, obligations and remedies of the parties.
226
227 D. Entire Understanding: This Agreement constitutes the entire understanding and agreement of the
228 parties relating to release of the Subject Software and may not be superseded, modified or amended
229 except by further written agreement duly executed by the parties.
230
231 E. Binding Authority: By accepting and using the Subject Software under this Agreement, a Recipient
232 affirms its authority to bind the Recipient to all terms and conditions of this Agreement and that
233 Recipient hereby agrees to all terms and conditions herein.
234
235 F. Point of Contact: Any Recipient contact with Government Agency is to be directed to the designated
236 representative as follows: J. Ritchie Carroll <mailto:jrcarrol@tva.gov>.
237
238*/
239#endregion
240
241#region [ Contributor License Agreements ]
242
243//******************************************************************************************************
244//
245// Copyright � 2011, Grid Protection Alliance. All Rights Reserved.
246//
247// The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may
248// not use this file except in compliance with the License. You may obtain a copy of the License at:
249//
250// http://www.opensource.org/licenses/eclipse-1.0.php
251//
252// Unless agreed to in writing, the subject software distributed under the License is distributed on an
253// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
254// License for the specific language governing permissions and limitations.
255//
256//******************************************************************************************************
257
258#endregion
259
260using GSF.Units;
261using System;
262using System.Threading;
263
264namespace GSF.Threading
265{
266 #region [ Enumerations ]
267
268 /// <summary>
269 /// Managed Thread Types
270 /// </summary>
271 public enum ThreadType
272 {
273 /// <summary>Standard thread created with public constructor</summary>
274 StandardThread,
275 /// <summary>Queued thread added into managed thread pool</summary>
276 QueuedThread
277 }
278
279 /// <summary>
280 /// Managed Thread States
281 /// </summary>
282 public enum ThreadStatus
283 {
284 /// <summary>Thread created, not started</summary>
285 Unstarted,
286 /// <summary>Thread queued for execution</summary>
287 Queued,
288 /// <summary>Thread start requested, execution pending</summary>
289 Started,
290 /// <summary>Thread executing</summary>
291 Executing,
292 /// <summary>Thread completed</summary>
293 Completed,
294 /// <summary>Thread aborted</summary>
295 Aborted
296 }
297
298 #endregion
299
300 /// <summary>
301 /// Defines a managed thread
302 /// </summary>
303 /// <remarks>
304 /// This class works like any normal thread but provides the benefit of automatic tracking
305 /// through the ManagedThreads collection, total thread runtime and the ability to run
306 /// the thread in an alternate execution context
307 /// </remarks>
308 public sealed class ManagedThread
309 {
310 #region [ Members ]
311
312 // Fields
313 private Thread m_thread;
314 private ThreadType m_type;
315 private ThreadStatus m_status;
316 private string m_name;
317 private Ticks m_startTime;
318 private Ticks m_stopTime;
319 private ContextCallback m_ctxCallback;
320 private ThreadStart m_tsCallback;
321 private ParameterizedThreadStart m_ptsCallback;
322 private ExecutionContext m_ctx;
323 private object m_state;
324 private object m_tag;
325
326 #endregion
327
328 #region [ Constructors ]
329
330 /// <summary>
331 /// Initializes a new instance of the ManagedThread class.
332 /// </summary>
333 /// <param name="callback">A <see cref="ThreadStart"/> object.</param>
334 public ManagedThread(ThreadStart callback)
335 : this(ThreadType.StandardThread, callback, null, null)
336 {
337 m_thread = new Thread(HandleItem);
338 }
339
340 /// <summary>
341 /// Initializes a new instance of the ManagedThread class, specifying a delegate that allows an object to be passed to the thread when the thread is started.
342 /// </summary>
343 /// <param name="callback">A <see cref="ParameterizedThreadStart"/> object.</param>
344 public ManagedThread(ParameterizedThreadStart callback)
345 : this(ThreadType.StandardThread, callback, null, null)
346 {
347 m_thread = new Thread(HandleItem);
348 }
349
350#if !MONO
351 /// <summary>
352 /// Initializes a new instance of the ManagedThread class, specifying a delegate that allows an object to be passed to the thread when the thread is started
353 /// and allowing the user to specify an alternate execution context for the thread.
354 /// </summary>
355 /// <param name="callback">A <see cref="ContextCallback"/> delgate to execute.</param>
356 /// <param name="ctx">An <see cref="ExecutionContext"/>.</param>
357 public ManagedThread(ContextCallback callback, ExecutionContext ctx)
358 : this(ThreadType.StandardThread, callback, null, ctx)
359 {
360 m_thread = new Thread(HandleItem);
361 }
362#endif
363
364 internal ManagedThread(ThreadType type, ThreadStart callback, object state, ExecutionContext ctx)
365 {
366 m_type = type;
367 m_status = (type == ThreadType.QueuedThread ? ThreadStatus.Queued : ThreadStatus.Unstarted);
368 m_tsCallback = callback;
369 m_state = state;
370 m_ctx = ctx;
371 }
372
373 internal ManagedThread(ThreadType type, ParameterizedThreadStart callback, object state, ExecutionContext ctx)
374 {
375 m_type = type;
376 m_status = (type == ThreadType.QueuedThread ? ThreadStatus.Queued : ThreadStatus.Unstarted);
377 m_ptsCallback = callback;
378 m_state = state;
379 m_ctx = ctx;
380 }
381
382 internal ManagedThread(ThreadType type, ContextCallback callback, object state, ExecutionContext ctx)
383 {
384 m_type = type;
385 m_status = (type == ThreadType.QueuedThread ? ThreadStatus.Queued : ThreadStatus.Unstarted);
386 m_ctxCallback = callback;
387 m_state = state;
388 m_ctx = ctx;
389 }
390
391 #endregion
392
393 #region [ Properties ]
394
395 /// <summary>
396 /// An object containing data to be used by the thread's execution method.
397 /// </summary>
398 public object State
399 {
400 get
401 {
402 return m_state;
403 }
404 set
405 {
406 m_state = value;
407 }
408 }
409
410 /// <summary>
411 /// An object that allows additional user defined information to be tracked along with this thread.
412 /// </summary>
413 public object Tag
414 {
415 get
416 {
417 return m_tag;
418 }
419 set
420 {
421 m_tag = value;
422 }
423 }
424
425 /// <summary>
426 /// Returns the managed thread type (either StandardThread or QueuedThread)
427 /// </summary>
428 public ThreadType Type
429 {
430 get
431 {
432 return m_type;
433 }
434 }
435
436 /// <summary>
437 /// Gets a value containing the curretn status of the current thread.
438 /// </summary>
439 public ThreadStatus Status
440 {
441 get
442 {
443 return m_status;
444 }
445 internal set
446 {
447 m_status = value;
448 }
449 }
450
451 /// <summary>
452 /// Gets a value indicating the execution status of the current thread.
453 /// </summary>
454 public bool IsAlive
455 {
456 get
457 {
458 return (m_status == ThreadStatus.Started || m_status == ThreadStatus.Executing);
459 }
460 }
461
462 /// <summary>
463 /// Gets or sets the name of the thread.
464 /// </summary>
465 public string Name
466 {
467 get
468 {
469 return m_name;
470 }
471 set
472 {
473 m_name = value;
474
475 if (m_type == ThreadType.StandardThread)
476 m_thread.Name = value;
477 }
478 }
479
480 /// <summary>
481 /// Get the time, in ticks, that the thread started executing
482 /// </summary>
483 public Ticks StartTime
484 {
485 get
486 {
487 return m_startTime;
488 }
489 }
490
491 /// <summary>
492 /// Get the time, in ticks, that the thread finished executing
493 /// </summary>
494 public Ticks StopTime
495 {
496 get
497 {
498 return m_stopTime;
499 }
500 }
501
502 /// <summary>
503 /// Gets the total amount of time, in seconds, that the managed thread has been active.
504 /// </summary>
505 public Time RunTime
506 {
507 get
508 {
509 Ticks processingTime = 0;
510
511 if (m_startTime > 0)
512 {
513 if (m_stopTime > 0)
514 processingTime = m_stopTime - m_startTime;
515 else
516 processingTime = DateTime.UtcNow.Ticks - m_startTime;
517 }
518
519 if (processingTime < 0)
520 processingTime = 0;
521
522 return processingTime.ToSeconds();
523 }
524 }
525
526 /// <summary>
527 /// Gets or sets a value indicating the scheduling priority of a thread.
528 /// </summary>
529 /// <returns>One of the ThreadPriority values. The default value is Normal.</returns>
530 /// <remarks>
531 /// Changing of this value is only available to standard threads - you can't change the priorty of queued threads since they are already
532 /// allocated and owned by the .NET thread pool.
533 /// </remarks>
534 public ThreadPriority Priority
535 {
536 get
537 {
538 if (m_type == ThreadType.QueuedThread)
539 {
540 return ThreadPriority.Normal;
541 }
542 else
543 {
544 return m_thread.Priority;
545 }
546 }
547 set
548 {
549 if (m_type == ThreadType.QueuedThread)
550 throw new InvalidOperationException("Cannot change priority of a thread that was queued into thread pool");
551
552 m_thread.Priority = value;
553 }
554 }
555
556 internal Thread Thread
557 {
558 get
559 {
560 return m_thread;
561 }
562 set
563 {
564 m_thread = value;
565 }
566 }
567
568 #endregion
569
570 #region [ Methods ]
571
572 /// <summary>
573 /// Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread. Calling this method usually terminates the thread.
574 /// </summary>
575 public void Abort()
576 {
577 ManagedThreads.Cancel(this, true, null);
578 }
579
580 /// <summary>
581 /// Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread. Calling this method usually terminates the thread.
582 /// </summary>
583 /// <param name="stateInfo">An object that contains application-specific information, such as state, which can be used by the thread being aborted.</param>
584 public void Abort(object stateInfo)
585 {
586 ManagedThreads.Cancel(this, true, stateInfo);
587 }
588
589 /// <summary>
590 /// Causes a thread to be scheduled for execution.
591 /// </summary>
592 public void Start()
593 {
594 if (m_type == ThreadType.QueuedThread)
595 throw new InvalidOperationException("Cannot manually start a thread that was queued into thread pool");
596
597 ManagedThreads.Add(this);
598
599 m_thread.Start();
600 }
601
602 /// <summary>
603 /// Causes a thread to be scheduled for execution.
604 /// </summary>
605 /// <param name="parameter">An object that contains data to be used by the method the thread executes.</param>
606 public void Start(object parameter)
607 {
608 if (m_type == ThreadType.QueuedThread)
609 throw new InvalidOperationException("Cannot manually start a thread that was queued into thread pool");
610
611 m_state = parameter;
612
613 ManagedThreads.Add(this);
614
615 m_thread.Start();
616 }
617
618 /// <summary>
619 /// Blocks the calling thread until a thread terminates or the specified time elapses, while continuing to perform standard COM and SendMessage pumping.
620 /// </summary>
621 /// <remarks>
622 /// This is only available for standard threads - queued threads don't have an associated thread until they are executing.
623 /// </remarks>
624 public void Join()
625 {
626 if (m_type == ThreadType.QueuedThread)
627 throw new InvalidOperationException("Cannot join a thread that was queued into thread pool");
628
629 if (!IsAlive)
630 throw new InvalidOperationException("Cannot join a thread that has not been started");
631
632 m_thread.Join();
633 }
634
635 /// <summary>
636 /// Blocks the calling thread until a thread terminates or the specified time elapses, while continuing to perform standard COM and SendMessage pumping.
637 /// </summary>
638 /// <param name="millisecondsTimeout">The number of milliseconds to wait for the thread to terminate. </param>
639 /// <returns>true if the thread has terminated; false if the thread has not terminated after the amount of time specified by the millisecondsTimeout parameter has elapsed.</returns>
640 /// <remarks>
641 /// This is only available for standard threads - queued threads don't have an associated thread until they are executing.
642 /// </remarks>
643 public bool Join(int millisecondsTimeout)
644 {
645 if (m_type == ThreadType.QueuedThread)
646 throw new InvalidOperationException("Cannot join a thread that was queued into thread pool.");
647
648 if (!IsAlive)
649 throw new InvalidOperationException("Cannot join a thread that has not been started.");
650
651 return m_thread.Join(millisecondsTimeout);
652 }
653
654 /// <summary>
655 /// Blocks the calling thread until a thread terminates or the specified time elapses, while continuing to perform standard COM and SendMessage pumping.
656 /// </summary>
657 /// <param name="timeout">A TimeSpan set to the amount of time to wait for the thread to terminate. </param>
658 /// <returns>true if the thread terminated; false if the thread has not terminated after the amount of time specified by the timeout parameter has elapsed.</returns>
659 /// <remarks>
660 /// This is only available for standard threads - queued threads don't have an associated thread until they are executing.
661 /// </remarks>
662 public bool Join(TimeSpan timeout)
663 {
664 return Join((int)timeout.TotalMilliseconds);
665 }
666
667 internal void HandleItem()
668 {
669 // Set start state
670 m_startTime = DateTime.UtcNow.Ticks;
671 m_status = ThreadStatus.Executing;
672
673 try
674 {
675 // Invoke the user's call back function
676 if ((object)m_ctx == null)
677 {
678 if ((object)m_tsCallback != null)
679 {
680 m_tsCallback.Invoke();
681 }
682 else if ((object)m_ptsCallback != null)
683 {
684 m_ptsCallback.Invoke(m_state);
685 }
686 else
687 {
688 m_ctxCallback.Invoke(m_state);
689 }
690 }
691#if !MONO
692 else
693 {
694 // If user specified an alternate execution context, we invoke
695 // their delegate under that context
696 ExecutionContext.Run(m_ctx, m_ctxCallback, m_state);
697 }
698#endif
699 }
700 finally
701 {
702 // Set finish state
703 if (m_status == ThreadStatus.Executing)
704 {
705 m_status = ThreadStatus.Completed;
706 }
707 m_stopTime = DateTime.UtcNow.Ticks;
708
709 ManagedThreads.Remove(this);
710 }
711 }
712
713 #endregion
714 }
715}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t