示例#1
0
        private void InitializeConsole()
        {
            using (_logger.CreateCallstack())
            {
                int attempts = 0;
                Random random = new Random();
                int process = Process.GetCurrentProcess().Id;

                do
                {
                    if (attempts > MaxAttempts)
                    {
                        throw new SessionLocalException(_session, "Cannot find unique name for event object.");
                    }

                    int instanceNumber = random.Next(1000);

                    _instanceName = string.Format(CultureInfo.InvariantCulture, "_{0}_{1}_{2}", process, GetHashCode(), instanceNumber);
                    _logger.WriteLine("Trying event {0}", _instanceName);
                    if (!TryCreateEvent(ConsoleEventRequest + _instanceName, out _requestEvent))
                    {
                        _logger.WriteLine("Event {0} is not unique", _instanceName);
                        _requestEvent.Close();
                        _requestEvent = null;
                    }
                    else
                    {
                        _logger.WriteLine("Event {0} is unique", _instanceName);
                        _responseEvent = CreateEvent(ConsoleEventResponse + _instanceName);
                        _cancelEvent = CreateEvent(ConsoleEventCancel + _instanceName);
                        string fileMappingName = ConsoleMapping + _instanceName;
                        _fileMapping = CreateFileMapping(fileMappingName);
                        if (Marshal.GetLastWin32Error() == UnsafeNativeMethods.ERROR_ALREADY_EXISTS)
                        {
                            throw new SessionLocalException(_session, string.Format(CultureInfo.InvariantCulture, "File mapping {0} already exists", fileMappingName));
                        }
                        if (_fileMapping.IsInvalid)
                        {
                            throw new SessionLocalException(_session, string.Format(CultureInfo.InvariantCulture, "Cannot create file mapping {0}", fileMappingName));
                        }
                    }
                    ++attempts;
                }
                while (_requestEvent == null);

                using (ConsoleCommStruct commStruct = AcquireCommStruct())
                {
                    commStruct.InitHeader();
                }

                if (_session.GuardProcessWithJobInternal)
                {
                    string jobName = ConsoleJob + _instanceName;
                    _job = new Job(_logger, jobName);
                }
            }
        }
示例#2
0
 public void Dispose()
 {
     using (_logger.CreateCallstack())
     {
         lock (_lock)
         {
             if (_session.TestHandlesClosedInternal)
             {
                 _logger.WriteLine("Will test that handles are closed");
             }
             _abort = true;
             if (_thread != null)
             {
                 _thread.Join();
                 _thread = null;
             }
             if (_process != null)
             {
                 _process.Dispose();
                 _process = null;
             }
             if (_requestEvent != null)
             {
                 _requestEvent.Close();
                 TestEventClosed(ConsoleEventRequest + _instanceName);
             }
             if (_responseEvent != null)
             {
                 _responseEvent.Close();
                 TestEventClosed(ConsoleEventResponse + _instanceName);
             }
             if (_cancelEvent != null)
             {
                 _cancelEvent.Close();
                 TestEventClosed(ConsoleEventCancel + _instanceName);
             }
             if (_fileMapping != null)
             {
                 _fileMapping.Dispose();
                 _fileMapping = null;
                 if (_session.TestHandlesClosedInternal)
                 {
                     _logger.WriteLine("Testing that file mapping is closed");
                     string fileMappingName = ConsoleMapping + _instanceName;
                     SafeFileHandle fileMapping = CreateFileMapping(fileMappingName);
                     if (Marshal.GetLastWin32Error() == UnsafeNativeMethods.ERROR_ALREADY_EXISTS)
                     {
                         _logger.WriteLine("Exception: File mapping {0} was not closed yet", fileMappingName);
                     }
                     if (!fileMapping.IsInvalid)
                     {
                         fileMapping.Dispose();
                     }
                 }
             }
             if (_inputEvent != null)
             {
                 _inputEvent.Close();
                 _inputEvent = null;
             }
             if (_job != null)
             {
                 _job.Dispose();
                 _job = null;
             }
         }
     }
 }