Form not updating from seperate thread

J

jayderk

I am trying to get a datagrid on a form to repopulate when a certain event
occurs on a polling thread for a comm unit.
I have verified the code... and it all works accept for repopulating the
datagrid.

here is the other functions and declares

public class frmOrders: Yada Yada
{
private EventHandler myDelegate; // predefined delegate

public frmOrders(){
//
// Required for Windows Form Designer support
//
InitializeComponent();
// delegate instance to invoke
myDelegate = new EventHandler(LoadGrid);

..
..
..



private void LoadGrid(object sender,EventArgs e)
{
DataRow newRow;
myDataTable.Clear();
if(!grid_needs_to_be_loaded ||
valid_orders == null || valid_orders.Count <= 0)
return;

grid_needs_to_be_loaded = false;
foreach(ClipOrderObj order in valid_orders)
{
newRow = myDataTable.NewRow();
newRow["Description"] = order.Order_Desc.ToString();
newRow["Quantity"] = order.Order_Quantity;
myDataTable.Rows.Add(newRow);
}
}


this code works perfect. it does this.invoke(myDelegate).. goes to the
function, updates, and returns .. like it should

private void poll_function()
{
this.Invoke(myDelegate);
return;
}




this code does NOT work.. it gets down to this.Invoke(myDelegate) and locks
up.
Any suggestions would be great.

private void poll_function()
{
string COM_PORT = "COM8:\0";
string twoAsbyte = Convert.ToString((char)0x02);
bool comm_port_return = false;
// try
// {
// comm_port_return = Bluetooth.OpenCommPort(COM_PORT);
// }
// catch(Exception e)
// {
// MessageBox.Show("Could not open comm port\nMessage:" +
e.Message.ToString() );
// }

while(!terminate && !comm_port_return){
byte[] thebuff = new byte[100];
// try{thebuff = Bluetooth.ReadPortWord();}
// catch(Exception commex)
// {
// bool open_comm_try = Bluetooth.OpenCommPort(COM_PORT);
// continue;
// }

if(cboCustomer.SelectedIndex < 0)
{
Thread.Sleep(4000);
continue;
}
// if (thebuff[4] == 0)
// continue;
// string bufferData = Bluetooth.convertDllData(thebuff);
// string cleanBuffer = bufferData.Replace("\r\n"," ");
// string stringToSplit = cleanBuffer.Replace(twoAsbyte,"");

string[] goodData = new string[50];
goodData[0] = "11326812";
goodData[1] = "11326773";
goodData[2] = "11326804";
goodData[3] = "11326819";

// goodData = stringToSplit.Split(' ');
for(int i=0; i < goodData.Length-1; i++)
{
string mydata =
goodData.ToString().Trim().Substring(0,goodData.Length - 4);
if( mydata .Length <= 0)
continue;

int index = IsInCurrentOrder(mydata );

if( index >= 0 )
{// it is in the valid_list
last_row_hit = index;
}
else if( index == -1)
{// it is new
ClipOrderObj orderRec = new ClipOrderObj();
orderRec.Code = mydata;
cm.GetProductInformation(orderRec);
if(orderRec.Order_Desc.Length <= 0)
{
MessageBox.Show(barcode + " is not in current database","Item
Not found",

MessageBoxButtons.OK,MessageBoxIcon.Hand,MessageBoxDefaultButton.Button1);
continue;
}

orderRec.Customer_Id =
((CustomerInfo)customer_list[cboCustomer.SelectedIndex]).Customer_Id.ToStrin
g();
orderRec.Order_Quantity = 1;
orderRec.Order_Status = "N";
valid_orders.Add(orderRec);
}
}

grid_needs_to_be_loaded = true;


this.Invoke(myDelegate);

}
 
S

Serg Kuryata [MS]

Could you please send me a working application that reproduces the problem
so I could investigate it?

Thank you,
Sergiy.

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| From: "jayderk" <[email protected]>
| Subject: Form not updating from seperate thread
| Date: Tue, 16 Sep 2003 08:55:43 -0500
| Lines: 146
| Organization: n/a
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| NNTP-Posting-Host: 209.150.197.226
| Path:
cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08.
phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa07.phx.gbl
microsoft.public.dotnet.framework.compactframework:33486
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| I am trying to get a datagrid on a form to repopulate when a certain event
| occurs on a polling thread for a comm unit.
| I have verified the code... and it all works accept for repopulating the
| datagrid.
|
| here is the other functions and declares
|
| public class frmOrders: Yada Yada
| {
| private EventHandler myDelegate; // predefined delegate
|
| public frmOrders(){
| //
| // Required for Windows Form Designer support
| //
| InitializeComponent();
| // delegate instance to invoke
| myDelegate = new EventHandler(LoadGrid);
|
| .
| .
| .
|
|
|
| private void LoadGrid(object sender,EventArgs e)
| {
| DataRow newRow;
| myDataTable.Clear();
| if(!grid_needs_to_be_loaded ||
| valid_orders == null || valid_orders.Count <= 0)
| return;
|
| grid_needs_to_be_loaded = false;
| foreach(ClipOrderObj order in valid_orders)
| {
| newRow = myDataTable.NewRow();
| newRow["Description"] = order.Order_Desc.ToString();
| newRow["Quantity"] = order.Order_Quantity;
| myDataTable.Rows.Add(newRow);
| }
| }
|
|
| this code works perfect. it does this.invoke(myDelegate).. goes to the
| function, updates, and returns .. like it should
|
| private void poll_function()
| {
| this.Invoke(myDelegate);
| return;
| }
|
|
|
|
| this code does NOT work.. it gets down to this.Invoke(myDelegate) and
locks
| up.
| Any suggestions would be great.
|
| private void poll_function()
| {
| string COM_PORT = "COM8:\0";
| string twoAsbyte = Convert.ToString((char)0x02);
| bool comm_port_return = false;
| // try
| // {
| // comm_port_return = Bluetooth.OpenCommPort(COM_PORT);
| // }
| // catch(Exception e)
| // {
| // MessageBox.Show("Could not open comm port\nMessage:" +
| e.Message.ToString() );
| // }
|
| while(!terminate && !comm_port_return){
| byte[] thebuff = new byte[100];
| // try{thebuff = Bluetooth.ReadPortWord();}
| // catch(Exception commex)
| // {
| // bool open_comm_try = Bluetooth.OpenCommPort(COM_PORT);
| // continue;
| // }
|
| if(cboCustomer.SelectedIndex < 0)
| {
| Thread.Sleep(4000);
| continue;
| }
| // if (thebuff[4] == 0)
| // continue;
| // string bufferData = Bluetooth.convertDllData(thebuff);
| // string cleanBuffer = bufferData.Replace("\r\n"," ");
| // string stringToSplit = cleanBuffer.Replace(twoAsbyte,"");
|
| string[] goodData = new string[50];
| goodData[0] = "11326812";
| goodData[1] = "11326773";
| goodData[2] = "11326804";
| goodData[3] = "11326819";
|
| // goodData = stringToSplit.Split(' ');
| for(int i=0; i < goodData.Length-1; i++)
| {
| string mydata =
| goodData.ToString().Trim().Substring(0,goodData.Length - 4);
| if( mydata .Length <= 0)
| continue;
|
| int index = IsInCurrentOrder(mydata );
|
| if( index >= 0 )
| {// it is in the valid_list
| last_row_hit = index;
| }
| else if( index == -1)
| {// it is new
| ClipOrderObj orderRec = new ClipOrderObj();
| orderRec.Code = mydata;
| cm.GetProductInformation(orderRec);
| if(orderRec.Order_Desc.Length <= 0)
| {
| MessageBox.Show(barcode + " is not in current database","Item
| Not found",
|
| MessageBoxButtons.OK,MessageBoxIcon.Hand,MessageBoxDefaultButton.Button1);
| continue;
| }
|
| orderRec.Customer_Id =
|
((CustomerInfo)customer_list[cboCustomer.SelectedIndex]).Customer_Id.ToStrin
| g();
| orderRec.Order_Quantity = 1;
| orderRec.Order_Status = "N";
| valid_orders.Add(orderRec);
| }
| }
|
| grid_needs_to_be_loaded = true;
|
|
| this.Invoke(myDelegate);
|
| }
|
|
|
 
Top