开发者

creating a new row in ListView and then writing to COM port delayed ListView loadtime, solution?

the listview renders the row item quite fast at normal state. but when I call a function to write to a COM port (POLE DISPLAY) . the load time of list view increases and slows down whole process. I've first called a function to add an item to the listview. The list view uses addrange method to add items. then the vfd function is called which will open a COM port first and initialize a port then write to the port.

Here is a full sample code:

//function to add new row item in the listview
private void ListFunction(int id)
  {
   ListViewItem checkitem = listView1.FindItemWithText(GetProduct(id));
   if (checkitem != null)
   {
    //MessageBox.Show("It Already Exist");
    int itemvalue = int.Parse(checkitem.SubItems[2].Text) + 1;
    checkitem.SubItems[2].Text = itemvalue.ToString();
                int ttlamt = GetPrice(id) * int.Parse(checkitem.SubItems[2].Text);
                string totalamount = ttlamt.ToString();
                checkitem.SubItems[4].Text = totalamount;
   }
   else
   {
                          // if it doesnot exist
                int ttlamt = GetPrice(id);
    ListViewItem item1 = new ListViewItem("1");
    item1.SubItems.Add(GetProduct(id));
    item1.SubItems.Add开发者_Go百科("1");
    item1.SubItems.Add(GetPrice(id).ToString());
                string totalamount = ttlamt.ToString();
                item1.SubItems.Add(totalamount);
    listView1.Items.AddRange(new ListViewItem[] { item1 });
   }
       // Write to port
public void VFD(int id)
  {
   SerialPort VFD = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One);
            VFD.Open();
   VFD.Write(initialize);
   VFD.Write(GetProduct(id) + ":" + GetPrice(id));
   VFD.Write("\x1B\x6c\x09\x02");
   VFD.Write("Total: " + totalAmt.Text);
      VFD.Close();


  }

private void button1_Click(object sender, EventArgs e)
  {
   int id = 10001;
//add to list
   ListFunction(id);


   // VFD Display 
   VFD(id);;
  }

All of the items are called from the database.

This slows down the list render performance.

How to get rid of this?


You could send the data to the COM port on a separate thread so this one isn't held up, but be careful to make your code thread-safe.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜