日期:2014-05-18  浏览次数:20941 次

C# TimerCallback 执行的方法为何无法正确改变全局变量呢?
NetStatus变量的值,按理应该是true才对的,为什么执行起来,总是false,可是F11调试时,也有时是true的,不明白这是为什么,多线程的问题吗?怎么解决呢?

代码如下:

C# code


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace TestConnSql
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();


            wait_conn();

        }
        private void wait_conn()

        {
            bool NetStatus, net = false;
            string DataS = "192.168.160.1";
            int ty;

            CheckIpConn conn = new CheckIpConn();
            ty = conn.TT;
            //net = conn.Connected;

            conn.CheckIp(DataS);

            //Thread.Sleep(2000);

            ty = conn.TT;
            NetStatus = conn.Connected;

            //net = CheckIpConn.CheckIp(DataS);

            // MessageBox.Show(net.ToString());

            //conn.setvalue();
            NetStatus = conn.Connected;

            if (!NetStatus)
                MessageBox.Show(conn.Connected.ToString() + ty.ToString());
        }
    }


}










C# code


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

namespace TestConnSql
{
    public class CheckIpConn
    {
        private int _port = 1433;
        private volatile bool _connect;
        private IPEndPoint _iPEndPoint;
        private TcpClient _client;
        private TimerCallback _timerCallBack;
        private System.Threading.Timer _timer;
        private int tt = 5;

        public int TT
        {
            get { return tt; }
        }

        public int Port
        {
            get { return _port; }
            set { _port = value; }
        }
        public bool Connected
        {
            get { return _connect; }
        }

        public void setvalue()
        {
            _connect = true;
        }

        public void CheckIp(string Ip)
        {
                _iPEndPoint = new IPEndPoint(IPAddress.Parse(Ip), _port);
                _timerCallBack = new TimerCallback(CheckConnect);
                _timer = new System.Threading.Timer(_timerCallBack, null, 10, 1000);
                _timer.Dispose();

            
            
            
        }

        public void CheckConnect(object o)
        {
            try
            {
                _client = new TcpClient();
                
                _client.Connect(_iPEndPoint);
                _connect = true;
                this._connect = true;
                tt = 50;
                //MessageBox.Show("cc" + _connect);
                _client.Close();
            }
            catch
            {
                _connect = false;
               //MessageBox.Show("cc" + _connect);

            }
        }


    }
}





------解决方案--------------------
conn.Connected 的值是不确定的,

加线程休眠 意思是等3秒,3秒之后获取结果 

能否连上是不确定的。
------解决方案--------------------
我一开始是假定你了解多线程的,因为你的代码里是开启了2个线程.

所以,我告诉你 conn.Connected 的值是不确定的,两个任务并行.

不通信的情况,就是不确定的.

可是后来我才发现 你不知道你的代码做了什么事

我觉得你没理解任务并行,以及当前"环境"