OperationCanceledException.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. /*============================================================
  5. **
  6. **
  7. **
  8. ** Purpose: Exception for cancelled IO requests.
  9. **
  10. **
  11. ===========================================================*/
  12. using System.Runtime.Serialization;
  13. using System.Threading;
  14. namespace System
  15. {
  16. [Serializable]
  17. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  18. public class OperationCanceledException : SystemException
  19. {
  20. [NonSerialized]
  21. private CancellationToken _cancellationToken;
  22. public CancellationToken CancellationToken
  23. {
  24. get => _cancellationToken;
  25. private set => _cancellationToken = value;
  26. }
  27. public OperationCanceledException()
  28. : base(SR.OperationCanceled)
  29. {
  30. HResult = HResults.COR_E_OPERATIONCANCELED;
  31. }
  32. public OperationCanceledException(string? message)
  33. : base(message)
  34. {
  35. HResult = HResults.COR_E_OPERATIONCANCELED;
  36. }
  37. public OperationCanceledException(string? message, Exception? innerException)
  38. : base(message, innerException)
  39. {
  40. HResult = HResults.COR_E_OPERATIONCANCELED;
  41. }
  42. public OperationCanceledException(CancellationToken token)
  43. : this()
  44. {
  45. CancellationToken = token;
  46. }
  47. public OperationCanceledException(string? message, CancellationToken token)
  48. : this(message)
  49. {
  50. CancellationToken = token;
  51. }
  52. public OperationCanceledException(string? message, Exception? innerException, CancellationToken token)
  53. : this(message, innerException)
  54. {
  55. CancellationToken = token;
  56. }
  57. protected OperationCanceledException(SerializationInfo info, StreamingContext context) : base(info, context)
  58. {
  59. }
  60. }
  61. }