我有这个小模块管理片:
const WorkspaceSlice = createSlice({
name: "workspace",
initialState: {
activeModule: null,
modules:
{
["someModule"]: { ...moduleRenderingData },
},
},
reducers: {
setActiveModule(state, action)
{
state.activeModule = action.payload;
},
addModule(state, action)
{
const { moduleName, renderingData } = action.payload;
if (!state.modules[moduleName])
{
state.modules[moduleName] = renderingData;
}
// state.activeModule = moduleName; <- basically the same as 'setActiveModule'
this.setActiveModule(state, action.payload.module); // <- 'this' is undefined
},
},
});
export default WorkspaceSlice;
Run Code Online (Sandbox Code Playgroud)
我想要做的是setActiveModule从内部调用,addModule这样我就不会有重复的代码,但我收到一个错误,因为this未定义。
有没有办法从另一个内部调用一个减速器?那的语法是什么?
如果没有,是否有可能以另一种方式实现此功能,假设我想同时保留addModule和setActiveModule …
我正在写一个小的winforms应用程序,打开另一个程序的几个实例.为了优化性能,我正在寻找一种方法来找到最少使用的CPU核心来分配流程.
我还希望能够看到每个核心的使用百分比.没什么好看的,TextBox或Label都没关系.
PerformanceCounter在遇到这些答案之后,我一直在尝试使用:
我尝试实现如下:
StatusBarOutput.Text = "";
//ignoring posible hyper-threading for simplicity's sake
var coreUsages = new PerformanceCounter[Environment.ProcessorCount];
for (var i = 0; i < coreUsages.Length; i++)
{
coreUsages[i] = new PerformanceCounter("Processor", "% Processor Time", i.ToString());
//using the status bar as output for now, doesn't really matter
StatusBarOutput.Text += " | " + coreUsages[i].CounterName + " ~ " + coreUsages[i].NextValue();
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:

同时,任务经理正在显示:

不知道我在这里缺少什么.