Exemple #1
0
        private bool CheckIfInvalidApartmentState()
        {
            if (Thread.CurrentThread.GetApartmentState() != ApartmentState.MTA)
            {
                var ex = new ThreadStateException("New-EtwUserProvider with the -ProviderName flag requires powershell.exe to be started with the -MTA flag.");
                var er = new ErrorRecord(ex, nameof(ThreadStateException), ErrorCategory.InvalidOperation, null);
                WriteError(er);

                return(true);
            }

            return(false);
        }
 /// <summary>
 /// Throw an exception if the current thread is not <see cref="ApartmentState.STA"/>
 /// </summary>
 /// <param name="callerName">(optional) The name of the caller member.</param>
 /// <param name="sourceFilePath">(optional) The path of the file who call this method.</param>
 /// <param name="sourceLineNumber">(optional) The line number of the fatal error.</param>
 internal static void ThrowIfNotStaThread([CallerMemberName] string callerName = null, [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
 {
     if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)
     {
         var exception = new ThreadStateException("STA thread required");
         if (Logger.InstanceLogSession != null)
         {
             Logger.Instance.Fatal(exception, callerName, sourceFilePath, sourceLineNumber);
         }
         else
         {
             throw exception;
         }
     }
 }
    public static int Main(String [] args)
    {
        int rValue = 100;
        ThreadStateException ta = null;

        Console.WriteLine("Test AutoResetEvent for expected NullRef Exceptions");
        Console.WriteLine( );

        try {
            ta.HelpLink = "Hello";
            rValue      = 1;
        }
        catch (NullReferenceException) {
            Console.WriteLine("Caught NullReferenceException   (ta.HelpLink = Hello)");
        }

        try {
            String s = ta.HelpLink;
            rValue = 2;
        }
        catch (NullReferenceException) {
            Console.WriteLine("Caught NullReferenceException   (string s = ta.HelpLink)");
        }

        try {
            Exception e = ta.InnerException;
            rValue = 3;
        }
        catch (NullReferenceException) {
            Console.WriteLine("Caught NullReferenceException   (ta.InnerException)");
        }

        try {
            String s = ta.Message;
            rValue = 4;
        }
        catch (NullReferenceException) {
            Console.WriteLine("Caught NullReferenceException   (ta.Message)");
        }

        try {
            String s = ta.Source;
            rValue = 5;
        }
        catch (NullReferenceException) {
            Console.WriteLine("Caught NullReferenceException   (ta.Source)");
        }

        try {
            String s = ta.StackTrace;
            rValue = 6;
        }
        catch (NullReferenceException) {
            Console.WriteLine("Caught NullReferenceException   (ta.StackTrace)");
        }

        // try {
        //  ta.TargetSite.ToString();
        //  rValue = 7;
        // }
        // catch (NullReferenceException) {
        //  Console.WriteLine("Caught NullReferenceException   (ta.TargetSite))");
        // }

        try {
            ta.Equals(new Exception("Hello"));
            rValue = 8;
        }
        catch (NullReferenceException) {
            Console.WriteLine("Caught NullReferenceException   (ta.Equals(new Exception()))");
        }

        try {
            ta.GetBaseException();
            rValue = 9;
        }
        catch (NullReferenceException) {
            Console.WriteLine("Caught NullReferenceException   (ta.GetBaseException())");
        }

        try {
            ta.GetHashCode();
            rValue = 10;
        }
        catch (NullReferenceException) {
            Console.WriteLine("Caught NullReferenceException   (ta.GetHasCode())");
        }

        // try {
        //  ta.GetObjectData(new SerializationInfo(1.GetType(),new FormatterConverter()),new StreamingContext(StreamingContextStates.All));
        //  rValue = 11;
        // }
        // catch (NullReferenceException) {
        //  Console.WriteLine("Caught NullReferenceException   (ta.ObjectData(SerializationInfo,StreamingContext))");
        // }

        try {
            ta.GetType();
            rValue = 12;
        }
        catch (NullReferenceException) {
            Console.WriteLine("Caught NullReferenceException   (ta.GetType())");
        }

        try {
            ta.ToString();
            rValue = 13;
        }
        catch (NullReferenceException) {
            Console.WriteLine("Caught NullReferenceException   (ta.ToString())");
        }


        Console.WriteLine("Return Code == {0}", rValue);
        return(rValue);
    }
 protected override void HandleThreadStateException(ThreadStateException exception)
 {
     ++HandleThreadStateExceptionCalled;
     base.HandleThreadStateException(exception);
 }
 protected virtual void HandleThreadStateException(ThreadStateException exception)
 {
 }