0

I am fairly new to Producer-Consumer problem so kindly excuse me. I created a sample example- I have 5 tasks in Total- 4 Tasks for producers and 1 Task for consumer as follows:

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()));

}

I want to save these values in my database so i created Table in sqlserver: Numb having 4 columns- id,Addition,Multiply,Divide,Subtraction. So the consumer adds the value in collection into Table for specific datacell example: if the key was "addition", then bc1.value should be inserted at datacell=1 and so-on for others.

Questions:

How do i implement this?

Also how do i maintain consistency in the database?

jazb
  • 5,498
  • 6
  • 37
  • 44
  • Database consistency is enforced by all of your tables' constraints. So you should have proper schema constraints that properly cover your use cases to ensure that your tables only move from valid states to other valid states. – ethane Jan 24 '19 at 06:21
  • How will i perform insertion into database through the consumer? Do you any code sample that i can refer to – Ruchika Mattoo Jan 24 '19 at 07:21

0 Answers0