小编A P*_*rov的帖子

多线程时如何传递不同的实例?

我正在建造一个刮刀.我的目标是启动X浏览器(其中X是线程数)并继续通过在X部分中拆分该列表来抓取每个URL的URL列表.

我决定使用3个线程(3个浏览器)和10个URL列表.

问题:如何在浏览器之间分离每个任务,如下所示:

  1. Browser1将列表中的项从0擦除到3

  2. Browser2将列表中的项目从4扫描到7

  3. Browser3将列表中的项目从8个擦除到10个

所有浏览器都应该同时抓取传递的URL列表.

我已经有了这个BlockingCollection:

BlockingCollection<Action> _taskQ = new BlockingCollection<Action>();

public Multithreading(int workerCount)
{
        // Create and start a separate Task for each consumer:
        for (int i = 0; i < workerCount; i++)
            Task.Factory.StartNew(Consume);
}

public void Dispose() { _taskQ.CompleteAdding(); }

public void EnqueueTask(Action action) { _taskQ.Add(action); }

void Consume()
{
// This sequence that we’re enumerating will block when no elements
// are available and will end when CompleteAdding is called. 
foreach (Action action …
Run Code Online (Sandbox Code Playgroud)

c# multithreading

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

如何从C#中的PostgreSQL获取所选行的值?

我正在使用带有C#和Npgsql库的PostgreSQL数据库。

现在,我可以选择表中的最后一行,但是我不知道如何为它分配C#变量。我知道我的选择有效,因为我之前已经成功编辑了最后一个条目。

您可以在下面找到我的代码。请注意,我没有粘贴其余方法,因为我认为它们无关紧要。

public void myMethod()
{
    this.OpenConn(); //opens the connection

    string sql = "SELECT id FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'customers' ORDER BY id DESC, LIMIT 1";

    using (NpgsqlCommand command = new NpgsqlCommand(sql, conn))
    {
        int id = 0; //instead of '0' I want it to be equal to the ID value from the row
        //something like "int id = sqlSelection.id;" -- this obviously doesn't work

        this.CloseConn(); //close the current connection
    }
}
Run Code Online (Sandbox Code Playgroud)

c# sql postgresql npgsql

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

运算符'=='不能应用于'bool'类型的操作数?和'System.Windows.Forms.DialogResult

当用户单击该表单上的"确定"按钮时,我试图获取一个窗口来返回值.

我正在尝试使用此代码作为指南,但我不断从我的标题中得到错误:

Operator '==' cannot be applied to operands of type 'bool?' and 'System.Windows.Forms.DialogResult'
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

using (var form = new MyNewForm())
{
    if (form.ShowDialog() == DialogResult.OK) //line with error
    {
        string val = form.ReturnValue1;        
        string dateString = form.ReturnValue2;
        //do stuff
    }
}
Run Code Online (Sandbox Code Playgroud)

已经读过这个帖子,这就是为什么我改变了ifto form.ShowDialog(),但错误仍然出现.

c# wpf

0
推荐指数
1
解决办法
1248
查看次数

标签 统计

c# ×3

multithreading ×1

npgsql ×1

postgresql ×1

sql ×1

wpf ×1