Commits

factormystic  committed 2655418

Initial import

  • Participants

Comments (0)

Files changed (3)

+using System;
+using System.ComponentModel;
+using System.Diagnostics;
+using System.Runtime.InteropServices;
+using System.Windows.Forms;
+using System.Threading;
+
+namespace background
+{
+    static class Program
+    {
+        #region API
+        [DllImport("kernel32", SetLastError = true)]
+        static extern bool AttachConsole(int dwProcessId);
+
+        [DllImport("kernel32.dll", SetLastError = true)]
+        static extern bool FreeConsole();
+        
+        [DllImport("user32.dll")]
+        static extern IntPtr GetForegroundWindow();
+
+        [DllImport("user32.dll", SetLastError = true)]
+        static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
+
+        [DllImport("kernel32.dll", SetLastError = true)]
+        static extern bool SetPriorityClass(IntPtr hProcess, uint dwPriorityClass);
+
+        static uint PROCESS_MODE_BACKGROUND_BEGIN = 0x00100000;
+        //static uint PROCESS_MODE_BACKGROUND_END = 0x00200000;
+        #endregion
+
+        public static string version = "1.2";
+        static bool attached = false;
+
+        [STAThread]
+        static int Main(string[] args)
+        {
+            //string[] args = Environment.GetCommandLineArgs();
+
+            attached = AttachToConsole();
+            if (!attached) return 0;
+            Thread.Sleep(10000);
+
+            //only show info if run via command line with no arguments
+            if (args.Length == 0 || (args.Length > 1 && (args[1] == "--help" || args[1] == "--h" || args[1] == "-help" || args[1] == "-h" || args[1] == "--?" || args[1] == "-?" || args[1] == "/help" || args[1] == "/h" || args[1] == "/?")))
+            {
+                Console.WriteLine(ShowInfo(false));
+            }
+            else
+            {
+                string program_path = Environment.ExpandEnvironmentVariables(args[1]);
+                string program_args = (args.Length > 2) ? Environment.CommandLine.Substring(Environment.CommandLine.IndexOf(args[1]) + args[1].Length + 1) : string.Empty;
+
+                Console.WriteLine();
+                Console.WriteLine("background.exe v"+version+"\thttp://factormystic.net/utils");
+                Console.WriteLine();
+                Console.WriteLine("Setting process to background priority...");
+                
+                if (!SetPriorityClass(Process.GetCurrentProcess().Handle, PROCESS_MODE_BACKGROUND_BEGIN))
+                {
+                    Win32Exception ex = new Win32Exception(Marshal.GetLastWin32Error());
+
+                    Console.WriteLine("Error setting process priority:");
+                    Console.WriteLine(ex.Message);
+                }
+
+                Console.WriteLine("Starting " + program_path + "...");
+
+                try
+                {
+                    Process program = Process.Start(program_path, program_args);
+                }
+                catch (Exception e)
+                {
+                    Console.WriteLine("Error: " + e.Message);
+                }
+
+                Console.WriteLine("Done.");
+            }
+            FreeConsole();
+            return 0;
+        }
+
+        private static bool AttachToConsole()
+        {
+        //    int foreground;
+        //    GetWindowThreadProcessId(GetForegroundWindow(), out foreground);
+        //    return AttachConsole(Process.GetProcessById(foreground).Id);
+            
+            ////attempt to attach to parent console
+            if (!AttachConsole(-1))
+            {
+                //not started from console
+                MessageBox.Show(ShowInfo(true), "background.exe usage");
+                return false;
+            }
+            else return true;
+        }
+
+        private static string ShowInfo(bool extra)
+        {
+            string info = string.Empty;
+
+            if (!extra) info += "\n";
+            info += "background.exe v" + version + "\thttp://factormystic.net/utils" + "\n";
+            info += "Start programs with background I/O and CPU priority.\n";
+
+            if (extra) info += "\n" + "This program is designed to be launched from the command line, or by a shortcut.\n";
+
+            info += "\n";
+            info += "Usage:\tbackground [options] program [parameters]\n";
+            info += "\t-cpu\tManually set process CPU priority. Valid options are:\n";
+            info += "\t\tidle, below, normal, above, realtime\n";
+            info += "\t\tIf this option is omitted, idle priority will be used.\n";
+            info += "\n";
+            info += "\t-io\tManually set process I/O priority. Valid options are:\n";
+            info += "\t\tidle, below, normal, above, realtime\n";
+            info += "\t\tIf this option is omitted, idle priority will be used.";
+
+            if(!extra) info += "\n";
+
+            return info;
+        }
+    }
+}

File Properties/AssemblyInfo.cs

+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following 
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("background")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("background")]
+[assembly: AssemblyCopyright("Copyright ©  2008")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible 
+// to COM components.  If you need to access a type in this assembly from 
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("5a736537-d0da-4e70-9307-7d2aaaa034f9")]
+
+// Version information for an assembly consists of the following four values:
+//
+//      Major Version
+//      Minor Version 
+//      Build Number
+//      Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers 
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.2.*")]
+[assembly: AssemblyFileVersion("1.2.0.0")]

File background.csproj

+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProductVersion>9.0.30729</ProductVersion>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{222A0EF9-CF25-4151-B878-C19016560A7B}</ProjectGuid>
+    <OutputType>WinExe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>background</RootNamespace>
+    <AssemblyName>background</AssemblyName>
+    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+    <StartupObject>
+    </StartupObject>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <UseVSHostingProcess>false</UseVSHostingProcess>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+    <Reference Include="System.Core">
+      <RequiredTargetFramework>3.5</RequiredTargetFramework>
+    </Reference>
+    <Reference Include="System.Xml.Linq">
+      <RequiredTargetFramework>3.5</RequiredTargetFramework>
+    </Reference>
+    <Reference Include="System.Data.DataSetExtensions">
+      <RequiredTargetFramework>3.5</RequiredTargetFramework>
+    </Reference>
+    <Reference Include="System.Data" />
+    <Reference Include="System.Deployment" />
+    <Reference Include="System.Drawing" />
+    <Reference Include="System.Windows.Forms" />
+    <Reference Include="System.Xml" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Program.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+</Project>