小编Asa*_*ner的帖子

是否可以使用 Redux Toolkit 从另一个减速器函数(在同一切片内)调用减速器函数?

我有这个小模块管理片:

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未定义。
有没有办法从另一个内部调用一个减速器?那的语法是什么?
如果没有,是否有可能以另一种方式实现此功能,假设我想同时保留addModulesetActiveModule …

javascript reactjs redux react-redux redux-toolkit

7
推荐指数
2
解决办法
2078
查看次数

使用c#查找使用最少的CPU内核

我正在写一个小的winforms应用程序,打开另一个程序的几个实例.为了优化性能,我正在寻找一种方法来找到最少使用的CPU核心来分配流程.

我还希望能够看到每个核心的使用百分比.没什么好看的,TextBox或Label都没关系.

PerformanceCounter在遇到这些答案之后,我一直在尝试使用:

寻找核心号码

CPU使用率超过2个核心

我尝试实现如下:

        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)

我得到的输出是:

产量

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

产量

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

c# cpu-usage winforms

4
推荐指数
1
解决办法
613
查看次数