使用多个生产者中的BlockingCollection将数据记录插入数据库-单个消费者

时间:2019-01-24 06:00:11

标签: c# .net sql-server task-parallel-library producer-consumer

我对生产者-消费者问题还很陌生,所以请原谅。 我创建了一个示例示例- 我总共有5个任务-针对生产者的4个任务和针对消费者的1个任务,如下所示:

SqlConnection sqlConn = new SqlConnection(PANELSPLIT.Properties.Settings.Default.dbConn);
BlockingCollection<KeyValuePair<string, string>> dataItems = new BlockingCollection<KeyValuePair<string, string>>(54);
private KeyValuePair<string, string> bc1;
private delegate void updateTextBox1(string imagepath);
private delegate void updateTextBox2(string imagepath);
private delegate void updateTextBox3(string imagepath);
private delegate void updateTextBox4(string imagepath);

private void Form1_Load(object sender, EventArgs e)
{
    var  t1 = Task.Factory.StartNew(()=> addition());
    var t2 = Task.Factory.StartNew(() => subtraction());
    var t3 = Task.Factory.StartNew(() => multiply());
    var t4 = Task.Factory.StartNew(() => divide());
    var t5 = Task.Factory.StartNew(() => consumer());

}

private void consumer()
{


    while (true)
    {
        if (dataItems.TryTake(out bc1)){
            Console.WriteLine(bc1.ToString());
            if(bc1.Key== "addition")
            {
                updateTextBox1 tb1 = new updateTextBox1(updatetb1);
                this.textBox1.BeginInvoke(tb1, bc1.Value);
            }
            else if(bc1.Key == "Multiply")
            {
                updateTextBox1 tb2 = new updateTextBox1(updatetb2);
                this.textBox2.BeginInvoke(tb2, bc1.Value);
            }
            else if (bc1.Key == "Divide")
            {
                updateTextBox1 tb3 = new updateTextBox1(updatetb3);
                this.textBox1.BeginInvoke(tb3, bc1.Value);
            }
            else
            {
                 updateTextBox1 tb4 = new updateTextBox1(updatetb4);
                this.textBox1.BeginInvoke(tb4, bc1.Value);
            }

            }
        else
        {
            Console.WriteLine("Empty");

        }
    }  
}

private void updatetb4(string imagepath)
{
    this.textBox4.Text = imagepath;
}

private void updatetb3(string imagepath)
{
    this.textBox3.Text = imagepath;
}

private void updatetb2(string imagepath)
{
    this.textBox2.Text = imagepath;
}

private void updatetb1(string imagepath)
{
    this.textBox1.Text = imagepath;
}

private void divide()
{

    int a = 6;
    int b = 2;
    dataItems.Add(new KeyValuePair<string, string>("Divide", (a/b).ToString()));

}

private void multiply()
{
    int a = 6;
    int b = 2;
    dataItems.Add(new KeyValuePair<string, string>("Multiply", (a * b).ToString()));

}

private void subtraction()
{
    int a = 6;
    int b = 2;
    dataItems.Add(new KeyValuePair<string, string>("subtraction", (a - b).ToString()));
     }

private void addition()
{
    int a = 6;
    int b = 2;
    dataItems.Add(new KeyValuePair<string, string>("addition", (a + b).ToString()));

}

我想将这些值保存在数据库中,所以我在sqlserver中创建了Table:Numb有4列-id,Addition,Multiply,Divide,Subtraction。 因此,对于特定的数据单元示例,使用者将集合中的值添加到Table中:如果键是“ addition”,则bc1.value应该在datacell = 1处插入,其他情况则如此。

问题

我该如何实现?

我还如何保持数据库的一致性?

0 个答案:

没有答案