锁定内部线程未运行

时间:2018-11-29 20:56:50

标签: c#

请使用两个线程,我希望其中一个在Global(共享变量)上编写,第二个线程使用共享变量的值开始处理它的代码部分。

搜索后,我发现最安全的方法是使用线程锁定,但是我尝试了这个问题,但它根本无法正常工作,我真的不知道为什么吗?

我的代码如下:

    namespace WIN_PLC_REV00.SER_TO_PC_CONN
{

    public class SER_Con

    {
        static bool tm = false;
        static Object thisLock = new Object();

        public static  void Ser_Conn_Establish()
        {

            WFControl D = new WFControl(GLP_VAR.SER_CONN.Lib_Fom_Acce.Txt);

            Form1 Fom = (Form1)Application.OpenForms["Form1"];

            SER_GLP_VAR.ipad = IPAddress.Any;
            SER_GLP_VAR.serversocket = new TcpListener(SER_GLP_VAR.ipad, 2000);
            SER_GLP_VAR.clientsocket = default(TcpClient);

            SER_GLP_VAR.serversocket.Start();
            Fom.Invoke(D, new object[] { ">>>>>>_Server Started_<<<<<<<<<" + "\r\n" + SER_GLP_VAR.start.ToString() });

            lock (thisLock)
            {


                tm = true;
            }

            while (true)
            {


                SER_GLP_VAR.clientsocket = SER_GLP_VAR.serversocket.AcceptTcpClient();


                if (Fom.textBox1.InvokeRequired)
                {
                    Fom.Invoke(D, new object[] { "accepted connection from client" + "\r\n" + tm.ToString() });

                }


            }



        }





//////////////////////////////////////
//////// TO get data from client
/// ///////////////////////////////////////////////
        public static void TST()
        {

            int Rec_Msg_Len = 0;
            WFControl Y = new WFControl(GLP_VAR.SER_CONN.Lib_Fom_Acce.Txt);
            Form1 Fom = (Form1)Application.OpenForms["Form1"];


            Fom.Invoke(Y, new object[] { GLP_VAR.SER_CONN.SER_GLP_VAR.str_test.ToString() + "MAHER SECOND PHASE"});

            while (tm )
                {

                    try
                    {

                        NetworkStream networkstream = SER_GLP_VAR.clientsocket.GetStream();
                        byte[] bytesfrom = new byte[10025];
                        if (networkstream.DataAvailable)
                        {
                            networkstream.Read(bytesfrom, 0, (int)SER_GLP_VAR.clientsocket.ReceiveBufferSize);

                            string DataFromClient = System.Text.Encoding.ASCII.GetString(bytesfrom);
                            DataFromClient = DataFromClient.Substring(0, DataFromClient.IndexOf('\0'));
                            if (DataFromClient == "Conn")
                            {
                                Fom.Invoke(Y, new object[] { "connection is up to Data" + ": " + Rec_Msg_Len.ToString() });
                            }
                            else
                            {


                                Rec_Msg_Len = DataFromClient.Length;
                                Fom.Invoke(Y, new object[] { DataFromClient + ": " + Rec_Msg_Len.ToString() });
                            }
                            networkstream.Flush();

                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }


                }
        }


}

}

[tm]的值仅在方法[Ser_Conn_Create()]的线程中更改 但是我无法在方法[TST()]的线程内检测到它的值。 这对我来说是第一步,因为在那之后我需要将数据缓冲区移到全局字符数组,但在这一点上我仍然很困惑。

我确定我做错了事。

谢谢, Moelsayed

0 个答案:

没有答案