Different behaviour when in debug mode vs deployed

Ok,
This is an odd one, at least to me.

A very quick test application has the following code:

using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using System.IO.Ports;
using System.Threading;

namespace Arendee.XBeeToRS232
{
    public class MainWindow : Window
    {
        private const int READ_DELAY = 200;

        private SerialPort xbeePort;
        private SerialPort rs232Port;
        private Text countText;
        private ulong counter;

        public MainWindow()
        {
            Initialize();
            
            Thread worker = new Thread(new ThreadStart(() => Run()));
            worker.Start();            
        }

        private void Initialize()
        {
            Width = SystemMetrics.ScreenWidth;
            Height = SystemMetrics.ScreenHeight;

            var panel = new StackPanel(Orientation.Vertical);
            
            Font smallFont = Resources.GetFont(Resources.FontResources.small);
            Font largeFont = Resources.GetFont(Resources.FontResources.large);
            var titleText = new Text(smallFont, "XBEE TO RS232 GATEWAY v1.0.0.1")
            {
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center
            };

            countText = new Text(largeFont, "0")
            {
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center
            };

                        
            panel.Children.Add(titleText);
            panel.Children.Add(countText);
            Child = panel;

            Visibility = Visibility.Visible;

            
        }

        private void Run()
        {
            xbeePort = new SerialPort("COM2",9600);
            xbeePort.DataReceived += xbeePort_DataReceived;

            rs232Port = new SerialPort("COM3", 115200);
            rs232Port.DataReceived += rs232Port_DataReceived;

            xbeePort.Open();
            rs232Port.Open();

            Thread.Sleep(Timeout.Infinite);
        }
      
        void xbeePort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            ProcessData(xbeePort, rs232Port);
        }
        
        void rs232Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            ProcessData(rs232Port, xbeePort);
        }
        
        private void ProcessData(SerialPort source, SerialPort destination)
        {
            Thread.Sleep(READ_DELAY);

            var buffer = new byte[source.BytesToRead];
            var read = source.Read(buffer, 0, buffer.Length);

            destination.Write(buffer, 0, read);

            UpdateCounter();
        }

        private void UpdateCounter()
        {
            counter++;
            Debug.Print(counter.ToString());
           Dispatcher.Invoke(new TimeSpan(0,0,0,5,0), new DispatcherOperationCallback((o) =>            
            {
                countText.TextContent = counter.ToString();
                return null;
            }), null);
        }
    }
}

(Quick & dirty, I know, but not the point here)

When I run this on a ChipWorkX by debugging in VS.NET,
the serial ports will receive and send data and update the display.

As soon as I do not run this with a debugger attached,
the screen will still be displayed, but neither ports ever receive
any data and the display is never updated.

What gives?

UPDATE:
[ulist]
If you start by debugging in VS2010 and then press STOP
the debugger is disconnected and the application keeps running and it works.
As soon as you press the reset button, the application starts again but it doesn’t work anymore
[/ulist]

Try a small 100ms delay in between initialise port and open port and one in between both port.open() statements.

As this seems to be the only place it could trip up.

Cheers Ian

Hey,

I tried but it does not seem to help.
Even went crazy and tried this:


        private void Run()
        {
            xbeePort = new SerialPort("COM2",9600);
            Thread.Sleep(100);
            xbeePort.DataReceived += xbeePort_DataReceived;
            Thread.Sleep(100);
            rs232Port = new SerialPort("COM3", 115200);
            Thread.Sleep(100);
            rs232Port.DataReceived += rs232Port_DataReceived;
            Thread.Sleep(100);
            Thread.Sleep(100);           
            xbeePort.Open();
            Thread.Sleep(100);
            rs232Port.Open();

            Thread.Sleep(Timeout.Infinite);
        }

Try between the thread intialise and thread start.

Cheers Ian

You have to open port and then subscribe for the events.

Hah,
Indeed, very odd.
If I open then subscribe it works.
Any reason why it works the other way around in debug mode though ?

Thnx to you both in any case!

Different timings I guess.

Tanks I had the same problem, I’m used to subscribe to this events before opening the port, so it was a nightmare to find this error. Things are not exactly the same in NETMF and the regular C# framework.
I wonder if there are more pits like this, that should be obvious.
Great thing that this forum exist.