| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1240 人关注过本帖
标题:点“MessageBox”上的确认后出现异常
只看楼主 加入收藏
caixiaoyi101
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-5-10
结帖率:0
收藏
已结贴  问题点数:10 回复次数:5 
点“MessageBox”上的确认后出现异常
timer1控制picturebox1的背景色不停变换,就像灯在闪烁一样。
使用的是异步网络通信。
当接收到数据时timer1启动,同时MessageBox.Show"连接成功"。
可是点了MessageBox上的确认后灯就停止闪烁了,此时timer1还是Enalbed,这是为什么呀?
搜索更多相关主题的帖子: MessageBox 
2010-05-17 21:48
封闭
Rank: 9Rank: 9Rank: 9
来 自:广东省汕头市
等 级:蜘蛛侠
威 望:1
帖 子:501
专家分:1084
注 册:2007-9-14
收藏
得分:5 
点MessageBox不可能会出错的。

肯定是你其它地方的代码错了。。

还是把代码贴上来吧。。
2010-05-17 23:27
caixiaoyi101
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-5-10
收藏
得分:0 
回复 楼主 caixiaoyi101
程序代码:
using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using using using System.Threading;

namespace 异步通信服务器
{
    public partial class Form1 : Form
    {
        private Socket serversocket,clientsocket;
        private const int dataSize=1024;
        private byte[] data = new byte[dataSize];
        public String message1="123#";
        public int num = 0;
           
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.button1.Enabled = false;
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            IPEndPoint server = new IPEndPoint(ip, 8000);
            serversocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            serversocket.Bind(server);
            serversocket.Listen(10);
            serversocket.BeginAccept(new AsyncCallback(AcceptConnection), serversocket);
        }
        private void AcceptConnection(IAsyncResult ar)
        {
            Socket oldServer = (Socket)ar.AsyncState;
            //异步接受传入的连接,并创建新的Socket来处理远程主机通信
            clientsocket = oldServer.EndAccept(ar);
            MessageBox.Show("与客户建立连接!");
            clientsocket.BeginReceive(data, 0, dataSize, SocketFlags.None, new AsyncCallback(ReceiveData), clientsocket);
        }
    
        private void ReceiveData(IAsyncResult ar)
        {
            Socket client = (Socket)ar.AsyncState;
            try
            {
                int receiveDataLength = client.EndReceive(ar);
                String str =Encoding.ASCII.GetString(data, 0, receiveDataLength);

                if (str == message1)
                {
                    this.timer1.Enabled = true;
                }
                else
                {
                    this.timer1.Enabled = false;
                }
                MessageBox.Show("接受成功!");

                clientsocket.BeginReceive(data, 0, dataSize, SocketFlags.None, new AsyncCallback(ReceiveData), clientsocket);
            }
            catch
            {
                client.Close();
                MessageBox.Show("等待新客户!");
                serversocket.BeginAccept(new AsyncCallback(AcceptConnection), serversocket);
            }
        }


        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                serversocket.Close();
                MessageBox.Show("停止监听!");
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message,"错误");
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            num++;
            if (num <= 10)
            {
                this.pictureBox1.BackColor = Color.Red;
            }
            if ((num > 10) && (num < 20))
            {
                this.pictureBox1.BackColor = Color.DimGray;
            }
            if (num == 20)
            {
                num = 0;
            } 
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

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


namespace 异步通信客户端
{
    public partial class Form1 : Form
    {
        private Socket clientsocket;
        private const int datasize = 1024;
        private byte[] data = new byte[datasize];
        public int num = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            IPEndPoint server = new IPEndPoint(ip, 8000);
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.BeginConnect(server, new AsyncCallback(ConnectServer), socket);
        }
        private void ConnectServer(IAsyncResult ar)
        {
            clientsocket = (Socket)ar.AsyncState;
            try
            {
                clientsocket.EndConnect(ar);
                MessageBox.Show("与服务器连接成功!");
                clientsocket.BeginReceive(data, 0, datasize, SocketFlags.None, new AsyncCallback(ReceiveData), clientsocket);
            }
            catch
            {
                MessageBox.Show("与服务器连接失败!");
            }

        }
        private void ReceiveData(IAsyncResult ar)
        {
            try
            {
                Socket server = (Socket)ar.AsyncState;
                int receiveDataLength = server.EndReceive(ar);
                string str =Encoding.ASCII.GetString(data, 0, receiveDataLength);
                if (str == "fire")
                {
                    timer1.Enabled = true;
                }
                if (str == "error")
                {
                    timer1.Enabled = false;
                }
                MessageBox.Show("接收到指令");
                clientsocket.BeginReceive(data, 0, datasize, SocketFlags.None, new AsyncCallback(ReceiveData), clientsocket);
            }
            catch
            { }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                int bytes = 0;
                Byte[] SendBytes = new Byte[256];
                string SendString = "123#";
                SendBytes = Encoding.ASCII.GetBytes(SendString.ToCharArray());
                bytes = clientsocket.Send(SendBytes, SendBytes.Length, 0);
             }
            catch
            {
                MessageBox.Show("发送失败!");
            }
        }
        


        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                int bytes = 0;
                Byte[] SendBytes = new Byte[256];
                string SendString = "456#";
                SendBytes = Encoding.ASCII.GetBytes(SendString.ToCharArray());
                bytes = clientsocket.Send(SendBytes, SendBytes.Length, 0);
              
            }
            catch
            {
                MessageBox.Show("发送失败!");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                int bytes = 0;
                Byte[] SendBytes = new Byte[256];
                string SendString = "01#";
                SendBytes = Encoding.ASCII.GetBytes(SendString.ToCharArray());
                bytes = clientsocket.Send(SendBytes, SendBytes.Length, 0);

            }
            catch
            {
                MessageBox.Show("发送失败!");
            }
        }
       
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                clientsocket.Close();
                MessageBox.Show("与服务器断开连接!");
            }
            catch
            {
                MessageBox.Show("连接尚未开始");
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            num++;
            if (num <= 10)
            {
                this.pictureBox1.BackColor = Color.Red;
            }
            if ((num > 10) && (num < 20))
            {
                this.pictureBox1.BackColor = Color.DimGray;
            }
            if (num == 20)
            {
                num = 0;
            }
        }
    }
}
麻烦帮我看下,上面是服务器,下面是客户端,其他的参数都正确的,就是和timer有关的操作不正确。
2010-05-18 15:58
xydddaxia
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:33
帖 子:466
专家分:2307
注 册:2009-3-20
收藏
得分:5 
先看看接收消息这个函数
程序代码:
 private void ReceiveData(IAsyncResult ar)
        {
            Socket client = (Socket)ar.AsyncState;
            try
            {
                int receiveDataLength = client.EndReceive(ar);
                String str =Encoding.ASCII.GetString(data, 0, receiveDataLength);

                if (str == message1)
                {
                    this.timer1.Enabled = true;
                }
                else
                {
                    this.timer1.Enabled = false;
                }
                MessageBox.Show("接受成功!");

                clientsocket.BeginReceive(data, 0, dataSize, SocketFlags.None, new AsyncCallback(ReceiveData), clientsocket);
            }
            catch
            {
                client.Close();
                MessageBox.Show("等待新客户!");
                serversocket.BeginAccept(new AsyncCallback(AcceptConnection), serversocket);
            }
}
        
MessageBox.Show("接受成功!")弹出对话框后,time1已经启动,当点了确定后,这个方法继续往后执行,而后面调用的函数又是接收消息,如果接收不到的话,那么整个函数就处于等待状态,当中没有用到线程说明是单线程的函数,那么整个程序应该在等待这个函数执行完(假死状态),所以看不到变化,解决的办法嘛就应当把接收消息放在一个单独的线程里处理,各干各的互不影响



站在春哥的肩膀上
2010-05-18 23:08
caixiaoyi101
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-5-10
收藏
得分:0 
回复 3楼 caixiaoyi101
看的不是很明白。这里用的是异步通信的方法。那么在BeginReceive以后不是不影响其他操作的么?能否给我写个例子看看?
2010-05-19 19:32
xydddaxia
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:33
帖 子:466
专家分:2307
注 册:2009-3-20
收藏
得分:0 
TestApplication.rar (36.54 KB)

站在春哥的肩膀上
2010-05-20 09:05
快速回复:点“MessageBox”上的确认后出现异常
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.049119 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved