J
Jamie Risk
I have a form with controls that bind to an object declared in
the form.
I have a communications class that is receiver driven. (to get
data I have to send a command).
How can I have the receiver thread manipulate the object my form
controls are bound to?
Eg.
namespace myApp
{
public partial class myMainForm : Form
{
public dataClass myData = new dataClass();
// Constructor
public myMainForm()
{
InitializeComponent();
this.myDataBindingSource.DataSource = this.myData;
}
// ...
private void Read_button_Click(object sender,
EventArgs e)
{
comm.myDevice dev = comm.myDevice();
dev.Open();
if (dev.IsOpen)
{
byte[] packet;
... // Create packet
dev.Transaction(packet);
}
}
}
namespace comm
{
private class responseData
{
public bool IsValid { ... }
}
public class myDevice : SerialPort
{
public void Transaction(byte[] packet)
{
try { base.Write(packet, 0, packet.Length); }
catch (TimeoutException) { return; }
System.Threading.Thread rdThrd;
rdThrd = new System.Threading.Thread(Response);
rdThrd.Start();
}
public void Response()
{
repsonseData resposne = new responseData();
while (!response.IsValid)
{
byte[] data = new byte[512];
try { base.Read(data, 0,
Math.Min(this.BytesToRead,512)); }
catch (TimeoutException) { return; }
response.scan(data);
}
// ****************************************
// bind something akin to this:
// ****************************************
myApp.myMainForm.myData.element1 = response.element1;
}
}
}
}
the form.
I have a communications class that is receiver driven. (to get
data I have to send a command).
How can I have the receiver thread manipulate the object my form
controls are bound to?
Eg.
namespace myApp
{
public partial class myMainForm : Form
{
public dataClass myData = new dataClass();
// Constructor
public myMainForm()
{
InitializeComponent();
this.myDataBindingSource.DataSource = this.myData;
}
// ...
private void Read_button_Click(object sender,
EventArgs e)
{
comm.myDevice dev = comm.myDevice();
dev.Open();
if (dev.IsOpen)
{
byte[] packet;
... // Create packet
dev.Transaction(packet);
}
}
}
namespace comm
{
private class responseData
{
public bool IsValid { ... }
}
public class myDevice : SerialPort
{
public void Transaction(byte[] packet)
{
try { base.Write(packet, 0, packet.Length); }
catch (TimeoutException) { return; }
System.Threading.Thread rdThrd;
rdThrd = new System.Threading.Thread(Response);
rdThrd.Start();
}
public void Response()
{
repsonseData resposne = new responseData();
while (!response.IsValid)
{
byte[] data = new byte[512];
try { base.Read(data, 0,
Math.Min(this.BytesToRead,512)); }
catch (TimeoutException) { return; }
response.scan(data);
}
// ****************************************
// bind something akin to this:
// ****************************************
myApp.myMainForm.myData.element1 = response.element1;
}
}
}
}