Example #1
0
        //3.	Implement the constructor of the Job class, that accepts a string called “name”
        public Job(string name)
        {
            /*  a.   Call NativeJob.CreateJobObject passing IntPtr.Zero
             *       and the name argument.
             *       Store the returning handle in _hJob
             */
            _hJob = NativeJob.CreateJobObject(IntPtr.Zero, name);

            //b.	If the handle is zero (IntPtr.Zero),
            //      throw an InvalidOperationException
            if (_hJob == IntPtr.Zero)
            {
                throw new InvalidOperationException("IntPtr is zero");
            }
            if (_processes == null)
            {
                _processes = new List <Process>();
            }

            //c.	Create the _processes object
            _processes = new List <Process>();

            //b.	Add a call in the Job ctor to GC.AddMemoryPressure(sizeInByte)
            //      and display a message that the Job was created
            GC.AddMemoryPressure(_sizeInBytes);
            Console.WriteLine($"job {name} created");
        }
Example #2
0
 protected void AddProcessToJob(IntPtr hProcess)
 {
     if (!NativeJob.AssignProcessToJobObject(_hJob, hProcess))
     {
         throw new InvalidOperationException("Failed to add process to job");
     }
 }
Example #3
0
 protected void AddProcessToJob(IntPtr hProcess)
 {
     CheckIfDisposed();
     if (!NativeJob.AssignProcessToJobObject(_hJob, hProcess))
     {
         throw new InvalidOperationException("Adding process to job failed!!");
     }
 }
 public Job(string name)
 {
     _hJob = NativeJob.CreateJobObject(IntPtr.Zero, name);
     if (_hJob == IntPtr.Zero)
     {
         throw new InvalidOperationException("can't create job");
     }
     _processes = new List <Process>();
 }
Example #5
0
 private void InitJob(string name)
 {
     _hJob      = NativeJob.CreateJobObject(IntPtr.Zero, name);
     _processes = new List <Process>();
     if (_hJob == IntPtr.Zero)
     {
         throw new InvalidOperationException("The job not created!");
     }
 }
Example #6
0
        public void Kill()
        {
            if (_hJob != null)
            {
                NativeJob.TerminateJobObject(_hJob, 0);
            }

            _hJob = IntPtr.Zero;
        }
Example #7
0
 public Job(string name)
 {
     _hJob = NativeJob.CreateJobObject(IntPtr.Zero, name);
     if (_hJob == IntPtr.Zero)
     {
         throw new InvalidOperationException();
     }
     GC.AddMemoryPressure(10485760);
     _processes = new List <Process>();
 }
Example #8
0
 public void Dispose()
 {
     NativeJob.CloseHandle(_hJob);
     foreach (Process process in _processes)
     {
         process.Dispose();
     }
     GC.SuppressFinalize(this);
     _hJob = IntPtr.Zero;
 }
Example #9
0
 public Job(string name)
 {
     _hJob = NativeJob.CreateJobObject(IntPtr.Zero, name);
     if (_hJob == IntPtr.Zero)
     {
         throw new InvalidOperationException();
     }
     _processes = new List <Process>();
     _disposed  = false;
 }
Example #10
0
        public Job(string name, long size)
        {
            _sizeInByte = size;
            _hJob       = NativeJob.CreateJobObject(IntPtr.Zero, name);

            if (_hJob == IntPtr.Zero)
            {
                throw new InvalidOperationException();
            }
            _processes = new List <Process>();
            GC.AddMemoryPressure(_sizeInByte);
            Console.WriteLine("job was created.");
        }
Example #11
0
 protected virtual void Dispose(bool disposing)
 {
     if (!Disposed)
     {
         if (disposing)
         {
             foreach (Process process in _processes)
             {
                 process.Dispose();
             }
         }
         NativeJob.CloseHandle(_hJob);
         Disposed = true;
     }
 }
Example #12
0
        public void Dispose(bool disposing)
        {
            if (_hJob == IntPtr.Zero)
            {
                return;
            }

            if (disposing)
            {
                ReleaseManaged();
                GC.SuppressFinalize(this);
            }

            NativeJob.CloseHandle(_hJob);
            _hJob = IntPtr.Zero;
        }
Example #13
0
        protected void AddProcessToJob(IntPtr hProcess)
        {
            try
            {
                CheckIfDisposed();
            }
            catch (ObjectDisposedException ex)
            {
                Trace.TraceError(ex.Message);
            }

            if (!NativeJob.AssignProcessToJobObject(_hJob, hProcess))
            {
                throw new InvalidOperationException("Failed to add process to job");
            }
        }
Example #14
0
        protected virtual void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (disposing)
            {
                foreach (var process in _processes)
                {
                    process.Dispose();
                }
            }
            NativeJob.CloseHandle(_hJob);
            _disposed = true;
        }
Example #15
0
        public Job(string name, long sizeInByte)
        {
            _name = name;

            _hJob = NativeJob.CreateJobObject(IntPtr.Zero, name);
            if (_hJob == IntPtr.Zero)
            {
                throw new InvalidOperationException();
            }
            if (sizeInByte <= 0)
            {
                throw new ArgumentException("you can't send a size that is smaller then 1");
            }
            GC.AddMemoryPressure(sizeInByte);
            _sizeInByte = sizeInByte;
            _processes  = new List <Process>();
            Console.WriteLine("Job created");
        }
Example #16
0
 protected virtual void Dispose(bool disposing)
 {
     if (_disposed)
     {
         return;
     }
     if (disposing)
     {
         foreach (var process in _processes)
         {
             process.Dispose();
         }
         _processes = null;
     }
     NativeJob.CloseHandle(_hJob);
     GC.RemoveMemoryPressure(_sizeInByte);
     _disposed = true;
 }
Example #17
0
        /// <summary>
        /// 3.	Implement the constructor of the Job class,
        /// that accepts a string called “name”:
        /// a.   Call NativeJob.CreateJobObject passing IntPtr.Zero and the name argument.
        ///      Store the returning handle in _hJob.
        /// b.   If the handle is zero (IntPtr.Zero), throw an InvalidOperationException.
        /// c.   Create the _processes object.
        /// </summary>
        /// <param name="name"></param>
        public Job(string name, int sizeInByte)
        {
            _hJob = NativeJob.CreateJobObject(IntPtr.Zero, name);
            if (_hJob == IntPtr.Zero)
            {
                throw new InvalidOperationException();
            }
            _processes = new List <Process>();

            _sizeInByte = sizeInByte;

            if (_sizeInByte > 0)
            {
                GC.AddMemoryPressure(sizeInByte);
            }

            Console.WriteLine($"A Job with the name {name} has created with ask for {sizeInByte} (sizeInByte) memory from the GC ");
        }
Example #18
0
        private bool disposedValue = false; // To detect redundant calls

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    foreach (var process in _processes)
                    {
                        process.Dispose();
                    }
                }

                // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
                // TODO: set large fields to null.

                NativeJob.CloseHandle(_hJob);
                disposedValue = true;
            }
        }
Example #19
0
        public Job(string name, long sizeInByte)
        {
            if (sizeInByte > 0)
            {
                SizeInByte = sizeInByte;
                _hJob      = NativeJob.CreateJobObject(IntPtr.Zero, name);
                if (_hJob.Equals(IntPtr.Zero))
                {
                    throw new InvalidOperationException();
                }

                _processes = new List <Process>();
                Disposed   = false;
                GC.AddMemoryPressure(SizeInByte);
                Console.WriteLine("The Job was created");
            }
            else
            {
                throw new ArgumentOutOfRangeException();
            }
        }
        private void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }
            if (disposing)
            {
                foreach (Process p in _processes)
                {
                    p.Dispose();
                }
                _processes = null;
            }
            NativeJob.CloseHandle(_hJob);
            if (_size > 0)
            {
                GC.RemoveMemoryPressure(_size);

                _disposed = true;
            }
        }
Example #21
0
        protected virtual void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }
            if (disposing)
            {
                foreach (var proc in _processes)
                {
                    proc.Dispose();
                }
            }
            NativeJob.CloseHandle(_hJob);

            if (_sizeInByte > 0)
            {
                GC.RemoveMemoryPressure(_sizeInByte);
            }
            Console.WriteLine("Job was released");

            _disposed = true;
        }
Example #22
0
 private void FreeResource()
 {
     NativeJob.CloseHandle(_hJob);
     _hJob = IntPtr.Zero;
 }
Example #23
0
 public void Kill()
 {
     CheckIfDisposed();
     NativeJob.TerminateJobObject(_hJob, 0);
 }
Example #24
0
 public void Kill()
 {
     NativeJob.TerminateJobObject(_hJob, 0);
 }
Example #25
0
 private void Close()
 {
     NativeJob.CloseHandle(_hJob);
     _hJob = IntPtr.Zero;
 }
Example #26
0
 public Job(string name)
 {
     this._hJob = NativeJob.CreateJobObject(IntPtr.Zero, name);
     IsHandleNotZero(this._hJob);
     _processes = new List <Process>();
 }