Skip to content

Commit

Permalink
网页端新增支持进程优先级设置
Browse files Browse the repository at this point in the history
  • Loading branch information
autodotua committed May 15, 2023
1 parent 25ed184 commit 024b932
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 67 deletions.
4 changes: 4 additions & 0 deletions SimpleFFmpegGUI.Core/IPipeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,9 @@ public interface IPipeService
void CancelQueueSchedule();

DateTime? GetQueueScheduleTime();

public int GetDefaultProcessPriority();

public void SetDefaultProcessPriority(int priority);
}
}
1 change: 1 addition & 0 deletions SimpleFFmpegGUI.Core/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ private void AddLog(char type, string message, TaskInfo task = null)
};
Log?.Invoke(null, new LogEventArgs(log));
db.Logs.Add(log);
Debug.WriteLine($"[{type}] {message}");
}

private void Save()
Expand Down
36 changes: 29 additions & 7 deletions SimpleFFmpegGUI.Core/Manager/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,46 @@ namespace SimpleFFmpegGUI.Manager
{
public static class ConfigManager
{
public const string DefaultProcessPriorityKey = "DefaultProcessPriority";
public const string SyncModifiedTimeKey = "SyncModifiedTime";
private static readonly Dictionary<string, object> cache = new Dictionary<string, object>();

public static int DefaultProcessPriority
{
get => GetConfig(DefaultProcessPriorityKey, 2);
set => SetConfig(DefaultProcessPriorityKey, value);
}

public static bool SyncModifiedTime
{
get => GetConfig(SyncModifiedTimeKey, true);
set => SetConfig(SyncModifiedTimeKey, value);
}

public static T GetConfig<T>(string key, T defaultValue)
{
if (cache.ContainsKey(key))
{
return (T)cache[key];
}
using var db = FFmpegDbContext.GetNew();
var item = db.Configs.Where(p => p.Key == key).FirstOrDefault();
if (item == null)
{
return defaultValue;
}
return Parse<T>(item.Value);
}
T value = Parse<T>(item.Value);
cache.Add(key, value);

using Logger logger = new Logger();
logger.Info($"读取配置:[{key}]={value}");
return value;
}
public static void SetConfig<T>(string key, T value)
{
if (cache.ContainsKey(key))
{
cache[key] = value;
}
using var db = FFmpegDbContext.GetNew();
var item = db.Configs.Where(p => p.Key == key).FirstOrDefault();
if (item == null)
Expand All @@ -49,16 +68,19 @@ public static void SetConfig<T>(string key, T value)
}

db.SaveChanges();
}

private static T Parse<T>(string data)
{
return JsonConvert.DeserializeObject<T>(data);
using Logger logger = new Logger();
logger.Info($"写入配置:[{key}]={value}");
}

private static string GetString<T>(T data)
{
return JsonConvert.SerializeObject(data);
}

private static T Parse<T>(string data)
{
return JsonConvert.DeserializeObject<T>(data);
}
}
}
37 changes: 31 additions & 6 deletions SimpleFFmpegGUI.Core/Manager/FFmpegProcess.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -10,13 +11,19 @@ namespace SimpleFFmpegGUI.Manager
/// </summary>
public class FFmpegProcess
{
public ProcessPriorityClass priority = ProcessPriorityClass.Normal;
private readonly Process process = new Process();

private ProcessPriorityClass priority = default;

private bool started = false;

private TaskCompletionSource<bool> tcs;

public FFmpegProcess(string argument)
private FFmpegProcess()
{
Priority = ConfigManager.DefaultProcessPriority;
}
public FFmpegProcess(string argument) : this()
{
process.StartInfo = new ProcessStartInfo()
{
Expand Down Expand Up @@ -47,7 +54,7 @@ public FFmpegProcess(string argument)
/// </summary>
public int Id => !started ? throw new Exception("进程还未开始运行") : process.Id;

public ProcessPriorityClass Priority
public int Priority
{
get
{
Expand All @@ -56,18 +63,36 @@ public ProcessPriorityClass Priority
throw new Exception("进程已经退出");
}

return priority;
return priority switch
{
ProcessPriorityClass.RealTime => 5,
ProcessPriorityClass.High => 4,
ProcessPriorityClass.AboveNormal => 3,
ProcessPriorityClass.Normal => 2,
ProcessPriorityClass.BelowNormal => 1,
ProcessPriorityClass.Idle => 0,
_ => throw new InvalidEnumArgumentException()
};
}
set
{
if (started && process.HasExited)
{
throw new Exception("进程已经退出");
}
priority = value;
priority = value switch
{
5 => ProcessPriorityClass.RealTime,
4 => ProcessPriorityClass.High,
3 => ProcessPriorityClass.AboveNormal,
2 => ProcessPriorityClass.Normal,
1 => ProcessPriorityClass.BelowNormal,
0 => ProcessPriorityClass.Idle,
_ => ProcessPriorityClass.Normal,
};
if (started)
{
process.PriorityClass = value;
process.PriorityClass = priority;
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions SimpleFFmpegGUI.Host/PipeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,5 +252,19 @@ public string GetSingleFileInDir(string dir, string name)
}
return files.First();
}

public int GetDefaultProcessPriority()
{
return ConfigManager.DefaultProcessPriority;
}

public void SetDefaultProcessPriority(int priority)
{
ConfigManager.DefaultProcessPriority = priority;
foreach (var task in manager.Managers)
{
task.Process.Priority=priority;
}
}
}
}
6 changes: 0 additions & 6 deletions SimpleFFmpegGUI.WPF/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public class Config : IJsonSerializable, INotifyPropertyChanged

private DefaultOutputDirType defaultOutputDirType = DefaultOutputDirType.InputDir;

private int defaultProcessPriority = 2;
private bool rememberLastArguments = true;

private List<RemoteHost> remoteHosts = new List<RemoteHost>();
Expand Down Expand Up @@ -87,11 +86,6 @@ public DefaultOutputDirType DefaultOutputDirType
get => defaultOutputDirType;
set => this.SetValueAndNotify(ref defaultOutputDirType, value, nameof(DefaultOutputDirType));
}
public int DefaultProcessPriority
{
get => defaultProcessPriority;
set => this.SetValueAndNotify(ref defaultProcessPriority, value, nameof(DefaultProcessPriority));
}

public Dictionary<TaskType, OutputArguments> LastOutputArguments { get; set; } = new Dictionary<TaskType, OutputArguments>();

Expand Down
2 changes: 1 addition & 1 deletion SimpleFFmpegGUI.WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ private void ResetUI(bool force = false)
grdLeft.RowDefinitions[2].Height = new GridLength(IsUiCompressMode ? 384 : 0);
grdLeft.RowDefinitions[2].MinHeight = IsUiCompressMode ? 384 : 0;
leftSplitter.Visibility = IsUiCompressMode ? Visibility.Visible : Visibility.Collapsed;
statusPanel.Margin = new Thickness(12, IsUiCompressMode ? 12 : 44, 12, 12);
statusPanel.Margin = new Thickness(12, IsUiCompressMode ? 12 : 44, 12, IsUiCompressMode ? 12 : 42);
UiCompressModeChanged?.Invoke(this, new EventArgs());
void RemoveFromGrid()
{
Expand Down
24 changes: 2 additions & 22 deletions SimpleFFmpegGUI.WPF/Model/UITaskInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class UITaskInfo : ModelBase, INotifyPropertyChanged

private FFmpegManager processManager;

private int processPriority = Config.Instance.DefaultProcessPriority;
private int processPriority = ConfigManager.DefaultProcessPriority;

private StatusDto processStatus;

Expand Down Expand Up @@ -236,33 +236,13 @@ public int ProcessPriority
get
{
return processPriority;
// ProcessManager.Process.Priority switch
//{
// ProcessPriorityClass.RealTime => 0,
// ProcessPriorityClass.High => 1,
// ProcessPriorityClass.AboveNormal => 2,
// ProcessPriorityClass.Normal => 3,
// ProcessPriorityClass.BelowNormal => 4,
// ProcessPriorityClass.Idle => 5,
// _ => throw new InvalidEnumArgumentException(nameof(ProcessPriorityClass)),
//};
}
set
{
processPriority = value;
if (ProcessManager.Process != null)
{

ProcessManager.Process.Priority = value switch
{
5 => ProcessPriorityClass.RealTime,
4 => ProcessPriorityClass.High,
3 => ProcessPriorityClass.AboveNormal,
2 => ProcessPriorityClass.Normal,
1 => ProcessPriorityClass.BelowNormal,
0 => ProcessPriorityClass.Idle,
_ => ProcessPriorityClass.Normal,
};
ProcessManager.Process.Priority = value;
}
this.Notify(nameof(Manager), nameof(ProcessPriority));
}
Expand Down
8 changes: 7 additions & 1 deletion SimpleFFmpegGUI.WPF/Pages/SettingPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public SettingPageViewModel()
public IEnumerable DefaultOutputDirTypes => Enum.GetValues<DefaultOutputDirType>();

public ObservableCollection<RemoteHost> ObservableRemoteHosts { get; set; }

public int DefaultProcessPriority
{
get => ConfigManager.DefaultProcessPriority;
set => ConfigManager.DefaultProcessPriority = value;
}
}

/// <summary>
Expand Down Expand Up @@ -85,7 +91,7 @@ private void BrowseSpecialDirPathButton_Click(object sender, RoutedEventArgs e)
{
CommonOpenFileDialog dialog = new CommonOpenFileDialog();
var path = dialog.GetFolderPath();
if(path != null)
if (path != null)
{
ViewModel.DefaultOutputDirSpecialDirPath = path;
}
Expand Down
10 changes: 10 additions & 0 deletions SimpleFFmpegGUI.Web/src/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,14 @@ export function postSchedule(time:any): Promise<AxiosResponse<any>> {
export function postCancelSchedule(): Promise<AxiosResponse<any>> {
return Vue.axios
.post(getUrl("Queue/CancelSchedule"))
}

export function getDefaultProcessPriority(): Promise<AxiosResponse<any>> {
return Vue.axios
.get(getUrl("Power/DefaultProcessPriority"))
}

export function postDefaultProcessPriority(priority:number): Promise<AxiosResponse<any>> {
return Vue.axios
.post(getUrl("Power/DefaultProcessPriority?priority="+priority))
}
Loading

0 comments on commit 024b932

Please sign in to comment.