我在这里严重问了一个问题锁定多个线程中的变量,所以为了清楚起见,我将在这里问它并希望我能正确地问它.
classA
creates instance of classB
has methodA that can update & uses classB's myVar
has methodB that can update & uses classB's myVar
classB
has a variable myVar
Run Code Online (Sandbox Code Playgroud)
methodA和methodB都可以在不同的线程中运行(在main的新线程中调用).我如何确保这是线程安全的?
我是C#的新手,我想询问我是否在MULTI THREADS(伪代码)中遇到这种情况:
public class ClassA
{
ClassB c = new ClassB();
public void someMethod()
{
c.myVar = 1;
// Some other stuff
c.myVar = 0;
}
}
public class ClassB
{
internal int myVar;
public void MethodA()
{
if(myVar = 1)
myVar = 0;
}
}
Run Code Online (Sandbox Code Playgroud)
如果someMethod()且MethodA()可以在单独的线程中处于活动状态,则MethodA()可以将if语句评估为true; 但在它设置之前myVar = 0,someMethod()设置myVar = 0为不正确设置myVar为0 MethodA()!!
基本上,我如何锁定myVar:
lock{}对myVar"财产(设置,获取) Interlock(Interlock …我是C#和多线程的新手,所以我很抱歉,如果这是一个重复的问题,但作为一个新手,看来我的问题与我读过的其他人略有不同.
我的GUI在一个(主)线程中运行.它调用一个后台任务(在一个dll中 - 我也在编写),它在一个单独的线程中运行.dll不了解GUI(即它不能引用GUI类).
现在,假设我想根据dll线程的状态更新GUI上的进度条 - >我正在做的是在dll中创建一个每X%将触发的事件,GUI将订阅此事件.触发事件时,GUI将更新进度条.
我的问题:
希望你能为我澄清一下!
谢谢
我正在使用 asyncio 进行一些 TCP 通信。我有一个无限循环的Receive()函数。read()这作为后台任务运行,使用asyncio.create_task(Receive()).
现在,如果连接被对等方关闭,则会引发异常(或者可能是任何其他异常),我在函数中捕获该异常Receive()。但是,我想重新引发该异常,以便外部代码可以决定要做什么(例如重新连接)。
由于异常是在任务中引发的,我不知道如何检索它。
我试图创建一个例子来说明我的意思:
import asyncio
async def divide(x):
try:
return 1/x
except Exception as e:
print("Divide inner exception: ", e)
raise # Re-raise so main() can handle it
async def someFn():
asyncio.create_task(divide(0)) # Exception is never retrieved
# await divide(0) # This will raise two exceptions - the original in divide() and in main()
async def main():
try:
await someFn()
# Do other things while someFn() runs …Run Code Online (Sandbox Code Playgroud) void MethodA()
{
Monitor.Enter(this);
if(someCondition)
{
Monitor.Exit(this);
// This point
MethodB();
}
else
{
// Set some values only
Monitor.Exit(this);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我有上面的方法可以在多线程中调用:
//This pointMonitor.Enter当线程1仍在时,另一个线程进入//This pointMethodB执行吗?如果是,是否有办法MethodB执行.我需要MethodA在执行前释放,MethodB()因为我不能等到发布之前MethodB完成MethodA.另外,我无法MethodB在新线程中开始.
我一直在阅读下拉菜单的语义 ui 远程内容文档(此处),但似乎无法弄清楚如何在我的情况下使用它。
我有一个函数可以查询 back4app(解析)所需的数据并将其转换为 JSON。如何将返回的数据填充到下拉列表中?我是否必须手动构建它,或者我可以以某种方式直接传递 JSON 吗?
有没有一种简单的方法从我的表单上的现有按钮创建按钮集合?(在c#中).
我的表单上已经有一系列按钮,我想使用索引来访问它们...例如:
myButtonArray[0].ForeColor ...// Do something with it
Run Code Online (Sandbox Code Playgroud)
可以这样做吗?
编辑:我可以将数组设置为具有通用的OnClick事件吗?然后确定点击阵列中的哪个按钮,然后改变它的颜色?
我有一个函数,可以运行查询(返回承诺),然后处理返回的数据。如何获得调用函数以访问已操纵的数据?
调用是这样的:
getAgeGroupList().then(function (result) {
// Would like to access manipulated data here.
});
Run Code Online (Sandbox Code Playgroud)
并且功能如下。该query.find()回报承诺的解析。
function getAgeGroupList(){
var query = new Parse.Query("Fixtures_and_results");
return query.find({
success: function(results) {
/* Manipulate results here, then return manipulated data */
// How do I return the manipulated data? e.g return(newArray)
},
error: function(error) {
}
});
}
Run Code Online (Sandbox Code Playgroud) c# ×5
javascript ×2
locking ×2
collections ×1
jquery ×1
json ×1
parse-server ×1
python ×1
semantic-ui ×1
variables ×1