c# - 基于标志的锁定
问题描述
我想根据flag
来自 App 的配置锁定几行代码。因此,基于此,flag
我是否异步运行应用程序。所以我需要通过检查来锁定几行代码的执行flag
。所以我需要重复编写代码。下面是示例
if (flag) {
lock(dataLock){
//few lines of code
}
} else {
//repeat the above code gain here (few lines of code)
}
有没有其他方法可以保存我的重复代码。
解决方案
if (flag)
Monitor.Enter(dataLock);
// few lines of code
if (Monitor.IsEntered(dataLock))
Monitor.Exit(dataLock);
推荐阅读
- javascript - 降低求平方整数的时间复杂度
- shell - 如果为空,则将日期字段替换为 Unix 中固定宽度格式文件中的另一个日期
- import - Importhtml 的数据行限制为 50 是否有解决方法
- javascript - 使用 React 钩子将数组元素分配给状态的正确方法是什么?
- python - alueError: 层序的输入 0 与 3D 自动编码器的层不兼容
- go - vscode打开golang项目,无法导入本地包
- c++ - 使用可变参数模板初始化向量
- django - b/w django-admin startapp 与 python manage.py startapp 有什么区别?
- python - 为什么 __eq__() 允许返回任意数据类型?
- python - 无法从函数中获得正确的输出