static void OnTimerCallback(object state)
    {
        if (System.Threading.Interlocked.CompareExchange(ref lockTimerCounter, 1, 0) == 0)
        {
            if (pcReqsPerSec == null)
            {
                pcReqsPerSec = new System.Diagnostics.PerformanceCounter("W3SVC_W3WP", "Requests / Sec", "_Total", true);
            }

            if (pcReqsPerSec != null)
            {
                try
                {
                    lock (threadLock)
                    {
                        CurrentRequestsPerSecondValue = Convert.ToDecimal(pcReqsPerSec.NextValue().ToString("N2"));
                    }
                }
                catch (Exception) {
                    // We had problem, just get rid of the performance counter and we'll rebuild it next revision
                    if (pcReqsPerSec != null)
                    {
                        pcReqsPerSec.Close();
                        pcReqsPerSec.Dispose();
                        pcReqsPerSec = null;
                    }
                }
            }

            stateCounter++;

            //Check every 5 seconds or so if anybody is still monitoring the server PerformanceCounter, if not shut down our PerformanceCounter
            if (stateCounter % 5 == 0)
            {
                if (System.Environment.TickCount - LastRequestTicks > 5000)
                {
                    Console.WriteLine("Stopping Poll Timer");

                    pollingTimer.Dispose();
                    pollingTimer = null;

                    if (pcReqsPerSec != null)
                    {
                        pcReqsPerSec.Close();
                        pcReqsPerSec.Dispose();
                        pcReqsPerSec = null;
                    }
                }
            }

            System.Threading.Interlocked.Add(ref lockTimerCounter, -1);
        }
    }
 public void Dispose()
 {
     if (_source != null)
     {
         _source.Dispose();
     }
 }
Beispiel #3
0
        /// <summary>
        /// Releases the unmanaged resources used by the <see cref="PerformanceCounter" /> object and optionally
        /// releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                try
                {
                    // This will be done regardless of whether the object is finalized or disposed.
                    if (disposing)
                    {
                        // This will be done only when the object is disposed by calling Dispose().
                        if ((object)m_counter != null)
                        {
                            m_counter.Dispose();
                        }

                        m_counter = null;
                    }

                    Reset();
                }
                finally
                {
                    m_disposed = true;          // Prevent duplicate dispose.
                }
            }
        }
Beispiel #4
0
        private int __available() //MB単位
        {
            if (NWEnviroment.isWindows())
            {
                string mem      = "Memory";
                string countMem = "Available Mbytes";
                System.Diagnostics.PerformanceCounter pcMem = new System.Diagnostics.PerformanceCounter(mem, countMem);
                float available = pcMem.NextValue();
                pcMem.Close();
                pcMem.Dispose();
                return((int)available);
            }
            else
            {
                string free = LinuxCommand.execute("free -m");
                using (StringReader sr = new StringReader(free)){
                    string line = "";
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line.Contains("-/+"))
                        {
                            string[] parts     = Regex.Split(line, @"\s+");
                            int      available = int.Parse(parts[parts.Length - 1]);
                            sr.Close();
                            sr.Dispose();
                            return(available);
//                            Console.WriteLine("rate:{0}",(int)(100*int.Parse(parts[2])/(int.Parse(parts[3])+int.Parse(parts[2]))));
                        }
                    }
                }
            }
            return(0);//TODO: Exception?
        }
Beispiel #5
0
        private decimal GetProcessorUsage()
        {
            System.Diagnostics.PerformanceCounter cpuCounter = null;
            try
            {
                cpuCounter = new System.Diagnostics.PerformanceCounter
                {
                    CategoryName = "Processor",
                    CounterName  = "% Processor Time",
                    InstanceName = "_Total"
                };

                cpuCounter.NextValue();
                Thread.Sleep(500);
                return(Convert.ToDecimal(Math.Round(cpuCounter.NextValue(), 4)));
            }
            catch (Exception ex)
            {
                throw new RMSAppException(this, "0500", "GetProcessorUsage failed. " + ex.Message, ex, false);
            }
            finally
            {
                if (cpuCounter != null)
                {
                    cpuCounter.Dispose();
                }
            }
        }
Beispiel #6
0
        #pragma warning restore 1591

        //------------------------------------------------------------------------------
        //
        // Method: Dispose
        //
        //------------------------------------------------------------------------------
        /// <summary>
        /// Provides a method to free unmanaged resources used by this class.
        /// </summary>
        /// <param name="disposing">Whether the method is being called as part of an explicit Dispose routine, and hence whether managed resources should also be freed.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    // Free other state (managed objects).
                }
                // Free your own state (unmanaged objects).
                if (performanceCounter != null)
                {
                    performanceCounter.Dispose();
                }
                // Set large fields to null.
                performanceCounter = null;

                disposed = true;
            }
        }
Beispiel #7
0
        private decimal GetAvailableMemory()
        {
            System.Diagnostics.PerformanceCounter ramCounter = null;

            try
            {
                ramCounter = new System.Diagnostics.PerformanceCounter("Memory", "Available MBytes");

                return(Convert.ToDecimal(Math.Round(ramCounter.NextValue(), 0)));
            }
            catch (Exception ex)
            {
                throw new RMSAppException(this, "0500", "GetAvailableMemory failed. " + ex.Message, ex, false);
            }
            finally
            {
                if (ramCounter != null)
                {
                    ramCounter.Dispose();
                }
            }
        }
Beispiel #8
0
        private decimal GetAvailableMemory()
        {
            System.Diagnostics.PerformanceCounter ramCounter = null;

            try
            {
                ramCounter = new System.Diagnostics.PerformanceCounter("Memory", "Available MBytes");

                return(Convert.ToDecimal(Math.Round(ramCounter.NextValue(), 0)));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return(0);
            }
            finally
            {
                if (ramCounter != null)
                {
                    ramCounter.Dispose();
                }
            }
        }
Beispiel #9
0
        public void MainFunction()
        {
            IEnumerable <string> files;
            int       count            = 0;
            const int leftoverMemoryMB = 9000;

            files = Directory.EnumerateFiles(Compare.workingDirectory);

            Application.Current.Dispatcher.Invoke((Action) delegate
            {
                ProgressBar.Text = "Progress: 0/" + files.Count();
            }, System.Windows.Threading.DispatcherPriority.Background);

            //int memoryUsage;
            List <Match> matches = new List <Match>();

            count = 0;
            for (int i = 1; i < files.Count(); i++)
            {
                int j = files.Count() - i;
                count += j;
            }

            System.Diagnostics.Process proc = System.Diagnostics.Process.GetCurrentProcess();

            int memsize = 0; // memsize in MB

            System.Diagnostics.PerformanceCounter PC = new System.Diagnostics.PerformanceCounter
            {
                CategoryName = "Process",
                CounterName  = "Working Set - Private",
                InstanceName = proc.ProcessName
            };
            memsize = Convert.ToInt32(PC.NextValue()) / (int)(1048576);

            DateTime dt1 = DateTime.Now;

            Task <List <Match> >[] taskArray = new Task <List <Match> > [files.Count()];
            for (int i = 0; i < taskArray.Count(); i++)
            {
                ulong availableMemory = new Microsoft.VisualBasic.Devices.ComputerInfo().AvailablePhysicalMemory / 1048576; // memsize in MB
                while (availableMemory <= leftoverMemoryMB)
                {
                    Thread.Sleep(100);
                    availableMemory = new Microsoft.VisualBasic.Devices.ComputerInfo().AvailablePhysicalMemory / 1048576; // memsize in MB
                }

                var newArray = files.ToList().GetRange(i, files.Count() - i);
                taskArray[i] = Task <List <Match> > .Factory.StartNew(() => Compare.Comparerer(newArray, 16));

                Thread.Sleep(50);
            }

            List <int> ignore = new List <int>();

            while (!taskArray.Last().IsCompleted)
            {
                for (int i = 0; i < taskArray.Count(); i++)
                {
                    if (!ignore.Contains(i) && taskArray[i].IsCompleted)
                    {
                        for (int j = 0; j < taskArray[i].Result.Count; j++)
                        {
                            //Causes the UI thread to throw an exeption
                            //MatchingFiles.Add(new Files(taskArray[i].Result[j].FileName1, taskArray[i].Result[j].FileName2));

                            //Tells the UI thread (dispatcher) to do something to the UI
                            Application.Current.Dispatcher.Invoke((Action) delegate
                            {
                                MatchingFiles.Add(new Files(taskArray[i].Result[j].FileName1, taskArray[i].Result[j].FileName2));
                            }, System.Windows.Threading.DispatcherPriority.Loaded);
                        }
                        ignore.Add(i);
                        Application.Current.Dispatcher.Invoke((Action) delegate
                        {
                            ProgressBar.Text = "Progress: " + ignore.Count + "/" + files.Count();
                        }, System.Windows.Threading.DispatcherPriority.Loaded);
                    }
                }
                Task.WaitAll(taskArray, 100);
            }

            Task.WaitAll(taskArray);
            Application.Current.Dispatcher.Invoke((Action) delegate
            {
                ProgressBar.Text = "Progress: " + taskArray.Count() + "/" + files.Count();
            }, System.Windows.Threading.DispatcherPriority.Background);

            PC.Close();
            PC.Dispose();

            for (int i = 0; i < taskArray.Length; i++)
            {
                matches.AddRange(taskArray[i].Result);
                taskArray[i].Dispose();
            }

            matches.Sort();
            foreach (Match item in matches)
            {
                File.AppendAllText(Compare.outputDirectory + "\\" + Compare.workingDirectory.Substring(Compare.workingDirectory.LastIndexOf('\\') + 1) + ".txt", "Files " + item.FileName1 + " and " + item.FileName2 + " are " + item.EqualElements.ToString("P") + " equal" + Environment.NewLine);
                if (item.MarkForDeletion && deleteMode)
                {
                    Compare.Delete(item);
                }
            }

            TimeSpan timeSpan = DateTime.Now - dt1;

            Application.Current.Dispatcher.Invoke((Action) delegate
            {
                MatchingFiles.Add(new Files("Done", "Execution time: " + timeSpan.TotalMinutes.ToString("#.##") + " min"));
            });

            return;
        }
Beispiel #10
0
 private int __available()
 {
     //MB単位
     if(NWEnviroment.isWindows()){
         string mem = "Memory";
         string countMem = "Available Mbytes";
         System.Diagnostics.PerformanceCounter pcMem = new System.Diagnostics.PerformanceCounter(mem,countMem);
         float available = pcMem.NextValue();
         pcMem.Close();
         pcMem.Dispose();
         return (int)available;
     }else{
         string free =LinuxCommand.execute("free -m");
         using(StringReader sr = new StringReader(free)){
             string line = "";
             while((line=sr.ReadLine())!=null){
                 if(line.Contains("-/+")){
                     string[] parts = Regex.Split(line,@"\s+");
                     int available = int.Parse(parts[parts.Length-1]);
                     sr.Close();
                     sr.Dispose();
                     return available;
     //                            Console.WriteLine("rate:{0}",(int)(100*int.Parse(parts[2])/(int.Parse(parts[3])+int.Parse(parts[2]))));
                 }
             }
         }
     }
     return 0;//TODO: Exception?
 }
Beispiel #11
0
 public void Dispose()
 {
     counter.Dispose();
 }
Beispiel #12
0
        static void Main(string[] args)
        {
            IEnumerable <string> files;
            int count = 0;

            if (args.Count() >= 1)
            {
                if (args.Contains("-h"))
                {
                    Console.WriteLine("Usage: ImageComparison [Input path] [Output path] -d -h --debug\n" +
                                      "\nOptions:" +
                                      "\n\t-d\t\tDeletes matches" +
                                      "\n\t-h\t\tDisplays the help" +
                                      "\n\t--debug\t\tEnables debug mode");
                    return;
                }
                if (args.Contains("-d"))
                {
                    deleteMode = true;
                }
                if (args.Count() == 2 && !args.Contains("--debug"))
                {
                    workingDirectory = args[0];
                    outputDirectory  = args[1];

                    if (workingDirectory.Last() == '\\')
                    {
                        workingDirectory.Remove(workingDirectory.Count(), 1);
                    }
                    if (outputDirectory.Last() == '\\')
                    {
                        outputDirectory.Remove(outputDirectory.Count(), 1);
                    }
                }
                else if (args.Count() == 1)
                {
                    workingDirectory = args[0];
                }
            }

            Compare.workingDirectory = workingDirectory;
            Compare.outputDirectory  = outputDirectory;

            files = Directory.EnumerateFiles(workingDirectory);
            //int memoryUsage;
            List <Match> matches = new List <Match>();

            count = 0;
            for (int i = 1; i < files.Count(); i++)
            {
                int j = files.Count() - i;
                count += j;
            }

            System.Diagnostics.Process proc = System.Diagnostics.Process.GetCurrentProcess();

            int memsize = 0; // memsize in MB

            System.Diagnostics.PerformanceCounter PC = new System.Diagnostics.PerformanceCounter
            {
                CategoryName = "Process",
                CounterName  = "Working Set - Private",
                InstanceName = proc.ProcessName
            };
            memsize = Convert.ToInt32(PC.NextValue()) / (int)(1048576);

            DateTime tt1 = DateTime.Now;

            Task <List <Match> >[] taskArray = new Task <List <Match> > [files.Count()];
            for (int i = 0; i < taskArray.Count(); i++)
            {
                ulong availableMemory = new Microsoft.VisualBasic.Devices.ComputerInfo().AvailablePhysicalMemory / 1048576; // memsize in MB
                while (availableMemory <= leftoverMemoryMB)
                {
                    Thread.Sleep(100);
                    availableMemory = new Microsoft.VisualBasic.Devices.ComputerInfo().AvailablePhysicalMemory / 1048576; // memsize in MB
                }

                var newArray = files.ToList().GetRange(i, files.Count() - i);
                taskArray[i] = Task <List <Match> > .Factory.StartNew(() => Compare.Comparerer(newArray, 16));

                Thread.Sleep(50);
            }

            List <int> ignore = new List <int>();

            while (!taskArray.Last().IsCompleted)
            {
                for (int i = 0; i < taskArray.Count(); i++)
                {
                    if (!ignore.Contains(i) && taskArray[i].IsCompleted)
                    {
                        for (int j = 0; j < taskArray[i].Result.Count; j++)
                        {
                            Console.WriteLine("Files {0} and {1} are equal", taskArray[i].Result[j].FileName1, taskArray[i].Result[j].FileName2);
                        }
                        ignore.Add(i);
                    }
                }
                Task.WaitAll(taskArray, 5000);
            }

            Task.WaitAll(taskArray);
            PC.Close();
            PC.Dispose();

            for (int i = 0; i < taskArray.Length; i++)
            {
                matches.AddRange(taskArray[i].Result);
                taskArray[i].Dispose();
            }

            matches.Sort();
            foreach (Match item in matches)
            {
                File.AppendAllText(outputDirectory + "\\" + workingDirectory.Substring(workingDirectory.LastIndexOf('\\') + 1) + ".txt", "Files " + item.FileName1 + " and " + item.FileName2 + " are " + item.EqualElements.ToString("P") + " equal" + Environment.NewLine);
                if (item.MarkForDeletion && deleteMode)
                {
                    Compare.Delete(item);
                }
            }

            DateTime tt2       = DateTime.Now;
            TimeSpan ttimeSpan = tt2 - tt1;

            Console.WriteLine("Execution time: {0} sec", (ttimeSpan.TotalSeconds).ToString("N2"));

            Console.Write("\nDone.");
            Console.ReadLine();
            return;
        }
Beispiel #13
0
 void IDisposable.Dispose()
 {
     _read.Dispose();
     _write.Dispose();
     _image.Dispose();
 }