Click here to Skip to main content
15,899,825 members
Home / Discussions / C#
   

C#

 
AnswerRe: C# Design Patterns (Abstraction etc.) Pin
Kevin McFarlane14-Jun-06 5:51
Kevin McFarlane14-Jun-06 5:51 
GeneralRe: C# Design Patterns (Abstraction etc.) Pin
Mr. VB.NET14-Jun-06 6:08
Mr. VB.NET14-Jun-06 6:08 
GeneralRe: C# Design Patterns (Abstraction etc.) [modified] Pin
Josh Smith14-Jun-06 7:15
Josh Smith14-Jun-06 7:15 
GeneralRe: C# Design Patterns (Abstraction etc.) Pin
Mr. VB.NET14-Jun-06 10:56
Mr. VB.NET14-Jun-06 10:56 
GeneralRe: C# Design Patterns (Abstraction etc.) Pin
Kevin McFarlane14-Jun-06 23:19
Kevin McFarlane14-Jun-06 23:19 
AnswerRe: C# Design Patterns (Abstraction etc.) Pin
Kevin McFarlane14-Jun-06 6:02
Kevin McFarlane14-Jun-06 6:02 
QuestionInstaller Language settings [modified] Pin
Endren14-Jun-06 5:20
Endren14-Jun-06 5:20 
QuestionEvents, Threads & BindingSource Pin
iswoolley14-Jun-06 5:14
iswoolley14-Jun-06 5:14 
I'm trying to create (using VC# 2005 Express and .NET2) a simple application that monitors the rate that bytes are received by a serial port. I've written a class that opens the serial port, counts the incoming bytes, and every second updates a property called "Rate" which is the number of bytes received in the last second.

Here's the class:

class PortMonitor : INotifyPropertyChanged<br />
    {<br />
    public event PropertyChangedEventHandler PropertyChanged;<br />
<br />
    private SerialPort port;<br />
    private UInt64 count;<br />
    private UInt64 rate;<br />
    private Thread thread;<br />
<br />
    public PortMonitor ()<br />
      {<br />
      count = 0;<br />
      rate = 0;<br />
<br />
      port = new SerialPort("COM9",38400);<br />
      port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);<br />
      port.Open();<br />
<br />
      thread = new Thread(new ThreadStart(Run));<br />
      thread.Start();<br />
      }<br />
<br />
    public UInt64 Rate<br />
      {<br />
      get { return rate; }<br />
      }<br />
<br />
    private void Run ()<br />
      {<br />
      while(port.IsOpen)<br />
        {<br />
        count = 0;<br />
        Thread.Sleep(1000);<br />
        rate = count;<br />
        NotifyPropertyChanged("Rate");<br />
        }<br />
      }<br />
<br />
    private void port_DataReceived (object sender,SerialDataReceivedEventArgs e)<br />
      {<br />
      count+=Convert.ToUInt64(port.BytesToRead);<br />
      port.DiscardInBuffer();<br />
      }<br />
<br />
    private void NotifyPropertyChanged (string property)<br />
      {<br />
      if(PropertyChanged != null)<br />
        PropertyChanged(this,new PropertyChangedEventArgs(property));<br />
      }<br />
    }


At the user interface end of things I've got a form with a label on it. I want to bind the text of the label to the rate property of my monitoring object. I see that the recomendation for .NET2 is to use a BindingSource inbetween the label and the monitor.

Here's the UI:

PortMonitor monitor;<br />
    BindingSource source;<br />
    <br />
    public Form1 ()<br />
{<br />
      InitializeComponent();<br />
<br />
      monitor = new PortMonitor();<br />
      source = new BindingSource();<br />
      source.DataSource = monitor;<br />
<br />
      label1.DataBindings.Add("Text",source,"Rate");<br />
      }


But this throws an InvalidOperationException as soon as the monitor class tries to notify the BindingSource that the Rate property has changed. I guess this is a variation of the "one thread can't call methods on the UI thread" exception.

What do I need to do to get this working?

Iain
AnswerRe: Events, Threads & BindingSource Pin
Mr. VB.NET14-Jun-06 5:34
Mr. VB.NET14-Jun-06 5:34 
GeneralRe: Events, Threads & BindingSource Pin
iswoolley14-Jun-06 6:59
iswoolley14-Jun-06 6:59 
AnswerRe: Events, Threads & BindingSource Pin
Mr. VB.NET14-Jun-06 6:32
Mr. VB.NET14-Jun-06 6:32 
AnswerRe: Events, Threads & BindingSource Pin
Leslie Sanford14-Jun-06 10:20
Leslie Sanford14-Jun-06 10:20 
AnswerRe: Events, Threads & BindingSource Pin
iswoolley19-Jun-06 5:45
iswoolley19-Jun-06 5:45 
Questionwhich datatype will be appropriate for Ratio calculation in C# Pin
kumar.bs14-Jun-06 5:12
kumar.bs14-Jun-06 5:12 
AnswerRe: which datatype will be appropriate for Ratio calculation in C# Pin
User 665814-Jun-06 5:20
User 665814-Jun-06 5:20 
AnswerRe: which datatype will be appropriate for Ratio calculation in C# Pin
albCode14-Jun-06 5:21
albCode14-Jun-06 5:21 
GeneralRe: which datatype will be appropriate for Ratio calculation in C# Pin
MCSD-Gandalf14-Jun-06 5:50
MCSD-Gandalf14-Jun-06 5:50 
QuestionC++ -> C# VirtualAllocEx! Pin
suguimoto14-Jun-06 3:59
suguimoto14-Jun-06 3:59 
AnswerRe: C++ -> C# VirtualAllocEx! Pin
Josh Smith14-Jun-06 4:21
Josh Smith14-Jun-06 4:21 
GeneralRe: C++ -> C# VirtualAllocEx! Pin
suguimoto14-Jun-06 4:34
suguimoto14-Jun-06 4:34 
JokeTabStrip Control Downside Pin
Sravan Krishna14-Jun-06 3:30
Sravan Krishna14-Jun-06 3:30 
JokeRe: TabStrip Control Downside Pin
Mairaaj Khan14-Jun-06 3:39
professionalMairaaj Khan14-Jun-06 3:39 
GeneralRe: TabStrip Control Downside Pin
Sravan Krishna14-Jun-06 4:23
Sravan Krishna14-Jun-06 4:23 
QuestionMulticolumn Drop Down in DataGridView Pin
i_islamian14-Jun-06 3:28
i_islamian14-Jun-06 3:28 
QuestionAdding a TracBar to a ToolStripMemuItem Pin
smcneese14-Jun-06 3:07
smcneese14-Jun-06 3:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.