小编c0l*_*row的帖子

如何在C#中正确锁定List <String> getter

我想知道如何正确锁定类型的吸气剂List<String>.我有一个看起来像这样的静态类:

class FirstClass {
static private locker = new object();   
static private List<String> _my_list;

public static List<String> my_list {
    get {
        lock(locker) {
            return my_list;
        }
    }
}

private static void thread_func() {
    // do something periodicaly with the list
    // for example:

    lock(locker){
        _my_list.Add();
        _my_list.RemoveAt();
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

}

然后,我有另一个类看起来像这样:

class SecondClass {
private void thread_func() {
    foreach(string s in FirstClass.my_list) {
        // read every item in the list
    }
}
Run Code Online (Sandbox Code Playgroud)

}

因此,第一类有一个第二类使用的公共列表.第一类定期更新一个线程中的列表,第二个类在第二个线程上以随机间隔读取列表.

这个锁定机制是否确保第一个类在第二个类读取时不会修改列表,反之亦然?

c# locking list

7
推荐指数
1
解决办法
3429
查看次数

BeautifulSoup解析器将分号附加到裸露的&符号,修改URL?

我试图解析python中的一些网站,其中链接到其他网站,但在纯文本,而不是"a"标签.使用BeautifulSoup我得到了错误的答案.考虑以下代码:

import BeautifulSoup

html = """<html>
            <head>
              <title>Test html</title>
            </head>
            <body>
              <div>
                example.com/a.php?b=2&c=15
              </div>
            </body>
          </html>"""

parsed = BeautifulSoup.BeautifulSoup(html)
print parsed
Run Code Online (Sandbox Code Playgroud)

当我运行上面的代码时,我得到以下输出:

<html>
  <head>
    <title>Test html</title>
  </head>
  <body>
    <div>
      example.com/a.php?b=2&c;=15
    </div>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

注意"div"中的链接和b = 2&c; = 15的部分.它与原始HTML不同.为什么BeautifulSoup会以这种方式搞乱链接.它是否试图自动创建HTML entites?怎么预防这个?

python beautifulsoup

7
推荐指数
1
解决办法
1573
查看次数

检查firefox安装最可靠的方法是什么?

我正在制作一个与firefox扩展捆绑在一起的软件.在我的安装脚本中,我想提示用户想要安装扩展程序的天气.但为此,我需要:

  1. 一种判断是否安装了firefox的方法
  2. 一种告诉安装了哪个版本的firefox的方法

我如何在InnoSetup或C#中做这两件事?我尝试手动检查默认firefox安装目录的路径或检查注册表中的卸载记录.我认为这些方法根本不可靠.

installation firefox inno-setup

5
推荐指数
1
解决办法
5455
查看次数

python中结构中的动态数组和结构

我试图使用ctypes在python中实现这个C结构:

struct _rows {
    int cols_count;
    char *cols[];
}

struct _unit {
    int rows_count;
    struct _rows *rows;
}

int my_func(struct _unit *param);
Run Code Online (Sandbox Code Playgroud)

问题是_rows.cols是一个动态大小的char指针数组,_unit.rows是一个动态大小的_rows结构数组.如何在python中使用ctypes实现这一点?

我能够定义一个函数,它将返回一个带有可变数量的char指针的_rows结构:

def get_row(cols):
    class Row(ctypes.Structure):
        _fields_ = [("cols_count", ctypes.c_int), 
                    ("cols", ctypes.c_char_p * cols)
                   ]
Run Code Online (Sandbox Code Playgroud)

我不知道该怎么做nex,它有点模糊,ctypes文档没有帮助.

python ctypes

5
推荐指数
1
解决办法
4221
查看次数

标签 统计

python ×2

beautifulsoup ×1

c# ×1

ctypes ×1

firefox ×1

inno-setup ×1

installation ×1

list ×1

locking ×1