UpdateFrame on multiple forms

157 Views Asked by At

I have made a Brain-Computer-Interface (BCI) in C#, there are flickering pictureboxes on each of the forms. However, it is only in the main form where the flickering occurs. Does any one know how to get the updateframe to work on the other forms as well?

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Security;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DTU_BCI_SPELLER_BioRadio_Interface
{
    static class Program
    {
               [StructLayout(LayoutKind.Sequential)]
        private struct Message
        {
            public IntPtr hWnd;
            public int msg;
            public IntPtr wParam;
            public IntPtr lParam;
            public uint time;
            public Point p;
        }

        [return: MarshalAs(UnmanagedType.Bool)]
        [SuppressUnmanagedCodeSecurity, DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern bool PeekMessage(out Message msg, IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags);

        public static ABCDE form;


        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            form = new ABCDE();

            Application.Idle += new EventHandler(Application_Idle);
            Application.Run(form);


            //udpsock2.Close();


        }

        static void Application_Idle(object sender, EventArgs e)
        {
            Message message;
            while (!PeekMessage(out message, IntPtr.Zero, 0, 0, 0))
            {
                form.UpdateFrame();

            }
        }
    }

}

Main Form:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Windows.Threading;
using System.Runtime.InteropServices;
using System.Security;
//using DTU_BCI_SPELLER_BioRadio_Interface.FlickerTimer;

namespace DTU_BCI_SPELLER_BioRadio_Interface
{
    public partial class ABCDE : Form
    {


        Stopwatch stopwatch = new Stopwatch();
        private byte[] dataStream = new byte[1024];
        private Socket udpSock1;

        private byte[] buffer;

        public delegate void DisplayMessageDelegate(string message);
        private DisplayMessageDelegate displayMessageDelegate = null;
        public EndPoint newClientEP = null;


        private string Avalue;
        public string AVALUE
        {
            get { return Avalue; }
            set { Avalue = value; }
        }


        public ABCDE()
        {   
            InitializeComponent();
            stopwatch.Start();
            this.displayMessageDelegate = new DisplayMessageDelegate(this.DisplayMessage);
            udpSock1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            udpSock1.Bind(new IPEndPoint(IPAddress.Any, 8050));
            buffer = new byte[1024];

            newClientEP = new IPEndPoint(IPAddress.Any, 0);
            udpSock1.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock1);


        }

        private void DoReceiveFrom(IAsyncResult iar)
        {
            try
            {
                Socket recvSock = (Socket)iar.AsyncState;
                EndPoint clientEP = new IPEndPoint(IPAddress.Any, 0);
                int msgLen = recvSock.EndReceiveFrom(iar, ref clientEP);
                byte[] localMsg = new byte[msgLen];
                Array.Copy(buffer, localMsg, msgLen);

                EndPoint newClientEP = new IPEndPoint(IPAddress.Any, 0);
                udpSock1.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock1);

                if (msgLen > 0)
                {
                    string textToprint = Encoding.UTF8.GetString(localMsg, 0, msgLen);
                    this.Invoke(this.displayMessageDelegate, new object[] { textToprint });
                }
            }
            catch (ObjectDisposedException)
            {
            }

        }
        private void DisplayMessage(string messge)
        {
            int numVal;
            //SPELL.Text += messge + Environment.NewLine;

            numVal = Convert.ToInt32(messge);
            udpSock1.Close();
            if (numVal == 83)
            {

                //Application.SetSuspendState();
                BCI1 form = new BCI1();
                //Application.Run(form);
                //form.ShowDialog();
                //stopwatch.Start();
                SPELL.Text = SPELL.Text + form.LET;

                udpSock1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                udpSock1.Bind(new IPEndPoint(IPAddress.Any, 8050));
                buffer = new byte[1024];

                newClientEP = new IPEndPoint(IPAddress.Any, 0);
                udpSock1.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock1);

                //udpSock.Disconnect(true);
                //udpSock.Connect(new IPEndPoint(IPAddress.Any, 8050));
            }
            else if (numVal == 76)
            {
                stopwatch.Stop();
                //pictureBox2.PerformClick();
                BCI2 l = new BCI2();
                l.ShowDialog();
                stopwatch.Start();
                SPELL.Text = SPELL.Text + l.LET;
                udpSock1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                udpSock1.Bind(new IPEndPoint(IPAddress.Any, 8050));
                buffer = new byte[1024];

                newClientEP = new IPEndPoint(IPAddress.Any, 0);
                udpSock1.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock1);
                //udpSock.Close();
            }
            else if (numVal == 70)
            {
                //pictureBox3.PerformClick();
                BCI3 l = new BCI3();
                l.ShowDialog();
                SPELL.Text = SPELL.Text + l.LET;
                udpSock1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                udpSock1.Bind(new IPEndPoint(IPAddress.Any, 8050));
                buffer = new byte[1024];

                newClientEP = new IPEndPoint(IPAddress.Any, 0);
                udpSock1.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock1);
                //udpSock.Close();
            }
            else if (numVal == 65)
            {
                //pictureBox4.PerformClick();
                            BCI4 l = new BCI4();
            l.ShowDialog();
            SPELL.Text = SPELL.Text + l.LET;
            udpSock1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            udpSock1.Bind(new IPEndPoint(IPAddress.Any, 8050));
            buffer = new byte[1024];

            newClientEP = new IPEndPoint(IPAddress.Any, 0);
            udpSock1.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock1);
                //udpSock.Close();
            }
            else if (numVal == 61)
            {
                //pictureBox5.PerformClick();
                            BCI5 l = new BCI5();
            l.ShowDialog();
            SPELL.Text = SPELL.Text + l.LET;
            udpSock1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            udpSock1.Bind(new IPEndPoint(IPAddress.Any, 8050));
            buffer = new byte[1024];

            newClientEP = new IPEndPoint(IPAddress.Any, 0);
            udpSock1.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock1);
                //udpSock.Close();
            }
            else if (numVal == 55)
            {
                //pictureBox6.PerformClick();
                            BCI6 l = new BCI6();
            l.ShowDialog();
            SPELL.Text = SPELL.Text + l.LET;
            udpSock1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            udpSock1.Bind(new IPEndPoint(IPAddress.Any, 8050));
            buffer = new byte[1024];

            newClientEP = new IPEndPoint(IPAddress.Any, 0);
            udpSock1.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock1);
                //udpSock.Close();
            }
            else if (numVal == 48)
            {
                //pictureBox7.PerformClick();
                            BCI7 l = new BCI7();
            l.ShowDialog();
            SPELL.Text = SPELL.Text + l.LET;
            udpSock1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            udpSock1.Bind(new IPEndPoint(IPAddress.Any, 8050));
            buffer = new byte[1024];

            newClientEP = new IPEndPoint(IPAddress.Any, 0);
            udpSock1.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock1);
                //udpSock.Close();
            }
            else if (numVal == 45)
            {
                //pictureBox8.PerformClick();
                String text = SPELL.Text;
                if (!String.IsNullOrEmpty(text)) SPELL.Text = text.Substring(0, text.Length - 1);
                //SPELL.Text = SPELL.Text;
                udpSock1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                udpSock1.Bind(new IPEndPoint(IPAddress.Any, 8050));
                buffer = new byte[1024];

                newClientEP = new IPEndPoint(IPAddress.Any, 0);
                udpSock1.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock1);
                //udpSock.Close();
            }
            else if (numVal == 40)
            {
                //pictureBox9.PerformClick();
                this.Close();
                //udpSock.Close();
            }
        }
        public void UpdateFrame()
        {
            double cycleHz1 = 0.006;

            double wave1 = Math.Sin((stopwatch.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz1);

            if (wave1 > 0.0)
            {
                pictureBox1.BackColor = Color.Black;
            }
            else
            {
                pictureBox1.BackColor = Color.White;
            }
            double cycleHz2 = 0.0065;

            double wave2 = Math.Sin((stopwatch.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz2);

            if (wave2 > 0.0)
            {
                pictureBox2.BackColor = Color.Black;
            }
            else
            {
                pictureBox2.BackColor = Color.White;
            }
            double cycleHz3 = 0.007;

            double wave3 = Math.Sin((stopwatch.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz3);

            if (wave3 > 0.0)
            {
                pictureBox3.BackColor = Color.Black;
            }
            else
            {
                pictureBox3.BackColor = Color.White;
            }
            double cycleHz4 = 0.0075;

            double wave4 = Math.Sin((stopwatch.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz4);

            if (wave4 > 0.0)
            {
                pictureBox4.BackColor = Color.Black;
            }
            else
            {
                pictureBox4.BackColor = Color.White;
            }
            double cycleHz5 = 0.0082;

            double wave5 = Math.Sin((stopwatch.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz5);

            if (wave5 > 0.0)
            {
                pictureBox5.BackColor = Color.Black;
            }
            else
            {
                pictureBox5.BackColor = Color.White;
            }
            double cycleHz6 = 0.0093;

            double wave6 = Math.Sin((stopwatch.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz6);

            if (wave6 > 0.0)
            {
                pictureBox6.BackColor = Color.Black;
            }
            else
            {
                pictureBox6.BackColor = Color.White;
            }
            double cycleHz7 = 0.010;

            double wave7 = Math.Sin((stopwatch.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz7);

            if (wave7 > 0.0)
            {
                pictureBox7.BackColor = Color.Black;
            }
            else
            {
                pictureBox7.BackColor = Color.White;
            }
            double cycleHz8 = 0.011;

            double wave8 = Math.Sin((stopwatch.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz8);

            if (wave8 > 0.0)
            {
                pictureBox8.BackColor = Color.Black;
            }
            else
            {
                pictureBox8.BackColor = Color.White;
            }
            double cycleHz9 = 0.0125;

            double wave9 = Math.Sin((stopwatch.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz9);

            if (wave9 > 0.0)
            {
                pictureBox9.BackColor = Color.Black;
            }
            else
            {
                pictureBox9.BackColor = Color.White;
            }

        }
        private void ABCDE_Load(object sender, EventArgs e)
        {

        }

    }
}

Form2:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Windows.Threading;
//using DTU_BCI_SPELLER_BioRadio_Interface.FlickerTimer;


namespace DTU_BCI_SPELLER_BioRadio_Interface
{
    public partial class BCI1 : Form
    {


        Stopwatch stopwatch2 = new Stopwatch();
        private byte[] dataStream = new byte[1024];
        private Socket udpSock2;

        private byte[] buffer;

        public delegate void DisplayMessageDelegate(string message);
        private DisplayMessageDelegate displayMessageDelegate = null;
        private string letter;
        private bool BlinkOn = false;
        public string LET
        {
            get { return letter; }
            set { letter = value; }
        }

        public BCI1()
        {
            InitializeComponent();

            this.displayMessageDelegate = new DisplayMessageDelegate(this.DisplayMessage);
            stopwatch2.Start();
            udpSock2 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            udpSock2.Bind(new IPEndPoint(IPAddress.Any, 8050));
            buffer = new byte[1024];

            EndPoint newClientEP = new IPEndPoint(IPAddress.Any, 0);
            udpSock2.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock2);
        }
        private void DoReceiveFrom(IAsyncResult iar)
        {
            try
            {
                Socket recvSock = (Socket)iar.AsyncState;
                EndPoint clientEP = new IPEndPoint(IPAddress.Any, 0);
                int msgLen = recvSock.EndReceiveFrom(iar, ref clientEP);
                byte[] localMsg = new byte[msgLen];
                Array.Copy(buffer, localMsg, msgLen);

                EndPoint newClientEP = new IPEndPoint(IPAddress.Any, 0);
                udpSock2.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock2);

                if (msgLen > 0)
                {
                    string textToprint = Encoding.UTF8.GetString(localMsg, 0, msgLen);
                    this.Invoke(this.displayMessageDelegate, new object[] { textToprint });
                }
            }
            catch (ObjectDisposedException)
            {
            }

        }
        public void UpdateFrame()
        {
            double cycleHz1 = 0.006;

            double wave1 = Math.Sin((stopwatch2.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz1);

            if (wave1 > 0.0)
            {
                pictureBox1.BackColor = Color.Black;
            }
            else
            {
                pictureBox1.BackColor = Color.White;
            }
            double cycleHz2 = 0.0065;

            double wave2 = Math.Sin((stopwatch2.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz2);

            if (wave2 > 0.0)
            {
                pictureBox2.BackColor = Color.Black;
            }
            else
            {
                pictureBox2.BackColor = Color.White;
            }
            double cycleHz3 = 0.007;

            double wave3 = Math.Sin((stopwatch2.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz3);

            if (wave3 > 0.0)
            {
                pictureBox3.BackColor = Color.Black;
            }
            else
            {
                pictureBox3.BackColor = Color.White;
            }
            double cycleHz4 = 0.0075;

            double wave4 = Math.Sin((stopwatch2.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz4);

            if (wave4 > 0.0)
            {
                pictureBox4.BackColor = Color.Black;
            }
            else
            {
                pictureBox4.BackColor = Color.White;
            }
            double cycleHz5 = 0.0082;

            double wave5 = Math.Sin((stopwatch2.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz5);

            if (wave5 > 0.0)
            {
                pictureBox5.BackColor = Color.Black;
            }
            else
            {
                pictureBox5.BackColor = Color.White;
            }
            double cycleHz6 = 0.0093;

            double wave6 = Math.Sin((stopwatch2.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz6);

            if (wave6 > 0.0)
            {
                pictureBox6.BackColor = Color.Black;
            }
            else
            {
                pictureBox6.BackColor = Color.White;
            }
        }
        private void DisplayMessage(string messge)
        {
            int numVal;
            //SPELL.Text += messge + Environment.NewLine;

            numVal = Convert.ToInt32(messge);
            udpSock2.Close();
            if (numVal == 83)
            {

                //AB.PerformClick();
                //this.Hide();
                LET = label1.Text;
                stopwatch2.Stop();
                this.Hide();
                //udpSock.Disconnect(true);
                //udpSock.Connect(new IPEndPoint(IPAddress.Any, 8051));
            }
            else if (numVal == 76)
            {
                //BB.PerformClick();
                LET = label2.Text;
                stopwatch2.Stop();
                this.Hide();
                //udpSock2.Close();
            }
            else if (numVal == 70)
            {
                //C.PerformClick();
                LET = label3.Text;
                stopwatch2.Stop();
                this.Hide();
                //udpSock2.Close();
            }
            else if (numVal == 65)
            {
                //D.PerformClick();
                LET = label4.Text;
                stopwatch2.Stop();
                this.Hide();
                //udpSock2.Close();
            }
            else if (numVal == 61)
            {
                //E.PerformClick();
                LET = label5.Text;
                stopwatch2.Stop();
                this.Hide();
                //udpSock2.Close();
            }
            else if (numVal == 55)
            {
                //button1.PerformClick();
                this.Hide();
                //udpSock2.Close();
            }
        }
    }
}

I have 7 forms, which should be able to be opened and hide after selecting a character inside the form.

1

There are 1 best solutions below

3
On BEST ANSWER

try like this:

    public static ABCDE form;
    public static BCI1 form1;

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        form = new ABCDE();
        form1 = new BCI1();
        form.Show();
        form1.Show();
        Application.Idle += new EventHandler(Application_Idle);
        Application.Run();
    }

    static void Application_Idle(object sender, EventArgs e)
    {
        Message message;
        while (!PeekMessage(out message, IntPtr.Zero, 0, 0, 0))
        {
            form.UpdateFrame();
            form1.UpdateFrame();
        }
    }

to set it to green:

long timeToBeGreenInMiliseconds = 10;
long timeWhenValue83WasSent;
bool shouldBeGreen;

void CallThisFunctionWhenValue83IsSent()
{
    timeWhenValue83WasSent = stopwatch.ElapsedMilliseconds;
    shouldBeGreen = true;
}

public void UpdateFrame()
{
    if( shouldBeGreen )
    {
        pictureBox1.BackColor = Color.Green;
        if( stopwatch.ElapsedMilliseconds - timeWhenValue83WasSent > timeToBeGreenInMiliseconds )
        {
            shouldBeGreen = false;
        }
    }
    else
    {
        double cycleHz1 = 0.006;

        double wave1 = Math.Sin((stopwatch.ElapsedMilliseconds * 2.0 * Math.PI) * cycleHz1);

        if (wave1 > 0.0)
        {
            pictureBox1.BackColor = Color.Black;
        }
        else
        {
            pictureBox1.BackColor = Color.White;
        }
    }
}