Touch Screen and Threads

The issue I have at the moment, I am running 2 threads one to receive packets from the ethernet and place them in a queue system which is running on the second thread. Once these threads get going I can no longer issue commands via the touch screen. I was hoping someone had a solution for this or workaround.

Can you provide us with the code, and you current hardware configuration.

http://www.tinyclr.com/forum/1/1904/

Let me apologize first for the code being a mess, I am working off an example for the FEZ Cobra. So once processClientData has started I cant issue commands through the touch screen anymore.

The CDataRecorderITouch sets up the buttons and event handler also places the controls on the screen.


public CDataRecorderITouch(Desktop window)
        {

            aButton[0] = new ActionButton("Start Data Recording", 10, 65);
            aButton[1] = new ActionButton("Stop Data Recording", 10, 105);

            screen.DrawImage(aButton[0].X, aButton[0].Y, aButton[0].Image, 0, 0, aButton[0].Width, aButton[0].Height);
            screen.DrawImage(aButton[1].X, aButton[1].Y, aButton[1].Image, 0, 0, aButton[1].Width, aButton[1].Height);

            // Reference to the Desktop window
            _window = window;
            StackPanel panel = new StackPanel(Orientation.Vertical);

            this.TouchDown += new TouchEventHandler(KeyPad_TouchDown);

            TitleBar titleBar = new TitleBar("Data Recorder");
            titleBar.HorizontalAlignment = HorizontalAlignment.Left;

            textFlow.ScrollingStyle = ScrollingStyle.LineByLine;
            textFlow.HorizontalAlignment = HorizontalAlignment.Center;
            textFlow.VerticalAlignment = VerticalAlignment.Top;
            textFlow.TextAlignment = TextAlignment.Left;

            this.Child = panel;

            panel.Children.Add(titleBar);
            panel.Children.Add(textFlow);

        }

The processClientData handles grabbing packets off the ethernet and places them in queue to be written to usb or sd.

public void processClientData(Socket cSocket)
        {
            byte[] recData = new byte[4096];


            byte[] startDRProcess = new byte[] { 0x01 };
          
            currentFileName = "Data_Log_" + DateTime.Now.Hour + "_" + DateTime.Now.Minute + "_" + DateTime.Now.Second + ".bin";
            string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
            FileHandle = new FileStream(rootDirectory + @ "\" + currentFileName, FileMode.Create, FileAccess.Write);
            startQueueSystem = new Thread(queueSystem);
            startQueueSystem.Start();

            cSocket.Send(startDRProcess);

            while (true)
            {

                Int32 bytesRead = cSocket.Receive(recData);

                byte[] tRecData = new byte[bytesRead];

                Array.Copy(recData, tRecData, bytesRead);

                if (stopFlag == false)
                {
                    qSystem.Enqueue(tRecData);
                }
                Thread.Sleep(sliderValue);
            }
 }

The queueSystem just pulls packets off the queue and writes them to usb or sd, after 5 minutes it will stop and drain the queue and close the file and start a new file.


private void queueSystem()
        {
            DateTime writeTime = DateTime.Now;
            int packets = 0;
            int packetTotal = 0;

            while (true)
            {
                if (qSystem.Count > 0)
                {
                    byte[] dataPacket = (byte[])qSystem.Dequeue();
                    DateTime startTimeDataWrite = DateTime.Now;
                    FileHandle.Write(dataPacket, 0, dataPacket.Length);
                    DateTime stopTimeDataWrite = DateTime.Now;
                    TimeSpan durationTimeDataWrite = stopTimeDataWrite - startTimeDataWrite;



                    Thread.Sleep(5);

                    DateTime currentWriteTime = DateTime.Now;
                    TimeSpan durWriteTime = currentWriteTime - writeTime;
                    if (durWriteTime.Minutes == 5)
                    {

                        lock (lockQue)
                        {
                            stopFlag = true;
                            while (qSystem.Count > 0)
                            {

                                dataPacket = (byte[])qSystem.Dequeue();
                                FileHandle.Write(dataPacket, 0, dataPacket.Length);
                                Thread.Sleep(5);


                            }
                            stopFlag = false;
                        }
                        FileHandle.Close();
                        currentFileName = "Data_Log_" + DateTime.Now.Hour + "_" + DateTime.Now.Minute + "_" + DateTime.Now.Second + ".bin";
                        string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
                        FileHandle = new FileStream(rootDirectory + @ "\" + currentFileName, FileMode.Create, FileAccess.Write);

                        writeTime = DateTime.Now;

                    }

                    sliderValue = (qSystem.Count * 2) + 10;
                    packets++;
                    packetTotal++;
                }


            }


        }

The KeyPad_TouchDown handles the touch screen events when a button is touched.

private void KeyPad_TouchDown(object sender, TouchEventArgs e)
        {

            int x, y;
            e.GetPosition((CDataRecorderITouch)sender, 0, out x, out y);
            string keyValue = e.Source.ToString();

            if (e.Source.ToString() == "GHIGraphicalDemo.Presentation.TitleBar")
            {

                Exit();

            }
            for (int i = 0; i < aButton.Length; i++)
            {
                if (x >= aButton[i].X && x <= aButton[i].X + aButton[i].Width &&
                        y >= aButton[i].Y && y <= aButton[i].Y + aButton[i].Height)
                {
                    switch (i)
                    {
                        case 0:
                            if (startRecordingOn == false)
                            {
                                startRecordingOn = true;
                                setupDataRecorderCommunication();

                            }
                            break;
                        case 1:
                            stopRecordingData();
                            startRecordingOn = false;
                            break;


                    }
                }
            }



        }

I am using the FEZ Cobra with the latest firmware and no hardware mods.

Where processClientData is called in the code? I don’t see it in the example.

Where do you call the processClientData method?. If its called on the main thread then there might be an issue since the while loop in the method will end up taking a lot of CPU time… Try spawning one more thread of this method in you main method. Also you could initialize your display on a separate thread and then block it so that events can be raised by the UI.