Hi folks,

I have written 2 applications on my till running windows 2000. One is an epos system and the other is a "driver" which parses the text from the touch screen and moves the mouse accordingly.

I'm having a very strange issue. I have recently added network functionality to my epos app. There is a classwhich keeps retrying to connect to the server incase the network is down. What I find is that during this loop (which is run in a seperate thread from my epos app's main thread), my touchscreen driver just doesn't move the mouse or just crashes if you just wait long enough (circa 1 minute or so) with a Send/Don't Send style message "system.indexoutofrangeexception". Once the epos app connects to the server, the touch screen drivers works again (provided it hasn't crashed already...).

Here is my "connector" class used in the epos app:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;

namespace Paypoint
{
    class Connector
    {
        Network networkinterface;
        IPEndPoint ipEnd;
        
        public Connector(Network network, IPEndPoint endpoint)
        {
            networkinterface = network;
            network.Connected = false;
            ipEnd = endpoint;
        }

        public void Start()
        {
            while (true)
            {
                if (networkinterface.Connected == false)
                {
                    networkinterface.initSocket();
                    connect();
                }
                Object objData = "ping";
                byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString());
                networkinterface.send("ping");
                Thread.Sleep(2000);
            }
        }

        private void connect()
        {
            
            while (!networkinterface.Connected)
            {
                try
                {
                    Network.m_clientSocket.Connect(ipEnd);
                    networkinterface.Connected = true;
                }
                catch
                {
                    networkinterface.Connected = false;
                    Thread.Sleep(10000);
                }
            }
        }
    }
}

Here is my TouchScreen "driver". It's a small app so I've included all of it:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.IO.Ports;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;

namespace EpsonTouchPanelDriver
{
    class Device
    {
        static SerialPort spt;
        public static int x;
        public static int y;

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const int MOUSEEVENTF_RIGHTUP = 0x10;


        public static void init()
        {
            spt = new SerialPort();
            if (spt.IsOpen) spt.Close();
            spt.PortName = "COM4";
            spt.BaudRate = 9200;
            spt.DataBits = 8;
            spt.StopBits = StopBits.One;
            spt.DataReceived += new SerialDataReceivedEventHandler(spt_DataReceived);
            spt.Open();
        }

        private static void spt_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            
                int i = 0;
                byte[] bytesToReadArray = new byte[1024];

                while (spt.BytesToRead > 0)
                {
                    bytesToReadArray.SetValue((byte)spt.ReadByte(), i);
                    i++;
                }
                string str;
                System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
                str = enc.GetString(bytesToReadArray);
                if (str.Substring(0, 1) == "T")
                {

                    char[] delimiterChars = {','};
                    double scalex;
                    double scaley;
                    if (str.Length > 1)
                    {
                        try
                        {
                            str = str.Substring(1, str.Length - 1);
                            string[] xy = str.Split(delimiterChars);
                            if (xy[1].Length > 4) xy[1] = xy[1].Substring(0, 4);
                            if (int.Parse(xy[0]) < 525) scalex = 0.61 - ((525 - int.Parse(xy[0])) * 0.0004);
                            else scalex = 0.61 + ((int.Parse(xy[0]) - 525) * 0.00005);
                            if ((1000 - int.Parse(xy[1])) < 500) scaley = 0.4873 - ((500 - (1000 - int.Parse(xy[1]))) * 0.00005);
                            else scaley = 0.4873 + (((1000 - int.Parse(xy[1])) - 500) * 0.00005);
                            x = (int)(int.Parse(xy[0]) * scalex);
                            y = (int)((1000 - int.Parse(xy[1])) * scaley);

                            if ((x > 0) && (y > 0))
                            {
                                Cursor.Position = new Point((int)x, (int)y);
                                mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, Cursor.Position.X, Cursor.Position.Y, 0, 0);
                                Thread.Sleep(100);
                            }
                        }
                        catch
                        {
                            MessageBox.Show("ERROR");
                        }

                    }
                }
            
        }
       
    
    
    }
}

I find this behaviour very strange as these are 2 different assemblies! I simply don't have the knowledge required to understand how one app can cause another to crash.

Thanks for your help. It's appreciated :)

Jonny

Ok so the suitation gets weirder. I have found out that during the time when my epos app is trying to connect to the server (hense also the time when to touchscreen won't work), the mouse CAN move if the "driver" Winform is in Focus (normally, it is minimised to the system tray..)

**UPDATE**

OK folks yet another update! If I put a Thread.Sleep(10) in my driver app just before the if statement, then it works just VERY slowly (Unuseable but hey, there's life!).

So some sort of system resource sharing between apps then?

Cheers

**UPATE**
And....another update! I feel so stupid! During the connect loop, my epos app is taking up 100% CPU! This must be the cause! Any ideas on what I'm doing wrong there? Thanks

OK folks, got this sorted.

It was actually another class (My listener class) in my epos app causing it to take up 100% of CPU time. All I needed to do was add a Thread.Sleep(100) to a infinate while loop

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.