【问题标题】:Change Process priority does not work更改流程优先级不起作用
【发布时间】:2012-12-06 08:13:53
【问题描述】:

我运行一个音频中继器应用程序,它允许我同时通过耳机和扬声器播放声音。 应用程序本身有能力将自己设置为“实时”,但它只会将其设置为高,所以目前我必须自己在任务管理器中进行设置。

我决定将其自动化,所以我用 C# 编写了一个小脚本,它会改变我的进程优先级(我会在完成后添加以启动)

namespace ProcessRealtime
{
    class Program
    {
        static void Main(string[] args)
        {
            Process[] processes = Process.GetProcessesByName("audiorepeater");
            foreach (Process proc in processes)
            {
                Console.WriteLine("Changing Priority for: "+proc.Id+" To RealTime");
                proc.PriorityClass = ProcessPriorityClass.RealTime;
                if (proc.PriorityClass == ProcessPriorityClass.RealTime)
                {
                    Console.WriteLine("Worked");
                }
            }
            Console.ReadLine();
        }
    }
}

问题在于它没有应用更改。

有谁知道为什么这不起作用?

【问题讨论】:

  • 嗯,您可能会尝试以管理员身份运行您的程序。在某些情况下这可能是必要的。
  • 可以确认@JABFreeware 所说的是正确的。
  • @Simon Whitehead 谢谢,我添加了答案

标签: c# windows


【解决方案1】:

您需要以管理权限运行您的脚本。

【讨论】:

  • 我在 Win7 上通过以管理员身份运行 Visual Studio 并且正常运行对此进行了测试。由于管理员工作(而非管理员只是将其设置为高,根据 OP 的问题)。
  • 谢谢,我有点疏忽了,因为任务管理器似乎不需要管理员提升到实时但很酷。只需要找到一种无需用户交互即可使其工作的方法(允许以管理员身份运行)。我正在考虑将其作为一项服务。
  • @OliverBaker 如果您是管理用户,任务管理器可以执行“透明提升”以提升自身而无需 UAC 提示。 Only software signed by the "Microsoft Windows" certificate can do this.
【解决方案2】:

试试这个:

using (Process p = Process.GetCurrentProcess())
    p.PriorityClass = ProcessPriorityClass.High;  

【讨论】:

    【解决方案3】:

    您可以以管理员身份运行或删除 UAC,因为您需要访问您未运行的进程的权限。

    这对我有用:

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    // #define DEBUG
    
    namespace ProcessRealtime
    {
        class PUBG_RealTime
        {
            static string processName = "TslGame";
            static ProcessPriorityClass newPriority = ProcessPriorityClass.High;
    
            static void Main(string[] args)
            {
    #if DEBUG
                PutDebug("Start!");
    #endif
                Process[] processes = Process.GetProcessesByName(processName);
    #if DEBUG
                PutDebug(processes.Length + " processed found");
    #endif
                foreach (Process proc in processes)
                {
    #if DEBUG
                    PutDebug("New process found");
    #endif
                    Console.WriteLine("Changing Priority for id:" + proc.Id + " to " + newPriority.ToString());
                    proc.PriorityClass = newPriority;
    #if DEBUG
                    PutDebug("Changed priority for " + proc.Id);
    #endif
                }
    #if DEBUG
                PutDebug("No more processes..");
    #endif
                Console.Write("Press a key, it's over !");
                Console.ReadLine();
            }
    
    #if DEBUG
            static bool debug = true;
            static int debugInc = 1;
            static void PutDebug(string info = "")
            {
                if(debug){
                    Console.WriteLine("Debug" + debugInc + ": " + info);
                    debugInc++;
                }
            }
    #endif
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-02-01
      • 2021-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多