A C# program for controlling computer fan speed (Hardware).
Initially, the plan was to use WMI for adjustments, but the WMI interface only provided monitoring capabilities without modification features. Therefore, OpenHardwareMonitorLib.dll is currently used as the interface, offering a range of commands for monitoring and adjusting hardware at a low level. To avoid issues with multiple instances running concurrently, the Mutex class is employed to ensure that only one instance of the program is running. Mutex is a synchronization primitive used to coordinate multiple threads' access to a shared resource.
The program tries to create a global mutex named "Global\OHWM_FanControl" when it runs. If the program is the first to attempt creating this mutex (i.e., createdNew
is true
), it gains initial ownership of the mutex and can continue executing. If an instance is already running (createdNew
is false
), the program outputs a message and exits. Running the program again will prompt the existing instance to adjust the fan speed. Any input in the main instance interface will close the main instance.
The original program, written in C#, requires administrative rights to take control of the fan. Two instances were created to send acceleration and deceleration signals, but these also required administrative rights to operate.
To avoid constant UAC prompts for administrative permission when sending signals, Task Scheduler is used to bypass the UAC prompts. (Currently, users need to manually create tasks named fanSlow and fanFast in the Task Manager.)
The two C# programs in the function folder respectively trigger the fanSlow and fanFast tasks in the system. In theory, there is no need to write .exe files, as .vbs files could suffice. The main reason for creating .exe files is to allow third-party programs to assign shortcuts to run these executables, which is not feasible with .vbs scripts.
Added functionality to control the speed of the water pump. The default is 25% (0.9L/min), and when the fan speed reaches above 70%, the water pump speed is adjusted to 35% (1.1L/min).
- Files must be deployed on a local disk as network-connected disks cannot initiate administrative rights.
- At least one main instance must be running to maintain the adjustment of fan speed.
- Due to the involvement of hardware device performance and administrative rights operations, please ensure to enable administrative rights or run the program as an administrator, otherwise, errors will occur.
- Manual operations are still needed after deployment to complete the automation setup. If time permits, future improvements will aim for a one-click lazy deployment.
Compilation needs to be done using the VS command line:
csc /out:fanSpeedFast.exe fanSpeedFast.cs
Alternatively, use the dotnet build
and publish
commands to generate executable files for fast.exe or slow.exe.
Q: Why not use PowerShell, Shortcut, or cmd? I personally tried all three and they would pop up a window briefly, but theoretically, it should be avoidable. However, vbs is simpler, so I used it.
Thanks to all the open-source developers and the creators and maintainers of OpenHardwareMonitor.