检查网络流是否为空. [英] Check if network stream is empty.

查看:87
本文介绍了检查网络流是否为空.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用TCP套接字连接的服务器和客户端.当我单击按钮并返回数据时,我会沿着网络流传递数据.我的问题是如何阻止用户向该按钮发送垃圾邮件.当它多次单击过快时,我得到一个错误,我认为这是因为它接收太多数据的速度太快了.我想停止用户向按钮发送垃圾邮件,但是他们确实需要多次使用它.在允许用户执行任何操作之前,是否有办法检测所有发送和接收操作是否已完成.谢谢.

I have a server and a client that connect using TCP sockets. I pass data along the network stream when I click a button and receive data in return. My question is how can I stop the user spamming the button. When its clicked multiple times too quickly I get an error, and I think it''s because its receiving too much data too fast. I want to stop the user spamming the button, but they do need to use it multiple times. Is there a way to detect if all send and receive operations are finished before I allow the user to do anything. Thanks.

推荐答案

您可以在发送或接收操作发生时将标志布尔类型设置为false.喜欢

bool标志= true;

void SendOperation()
{
标志=假;
//您的操作
标志= true;
}

void ReceiveOperation()
{
标志=假;
//您的操作
标志= true;
}

当用户单击按钮1st时,检查标志状态,如果标志状态为true,则执行操作,否则向用户发送消息,对不起,请等待.
you can take a flag bool type when send or receive operation occur the flag will be false. like

bool flag = true;

void SendOperation()
{
flag = false;
//your operation
flag = true;
}

void ReceiveOperation()
{
flag = false;
//your operation
flag = true;
}

when user click the button 1st check the flag status, if the flag status is true then your perform your operation, otherwise give a message to the user, sorry, please wait.


您可以简单地禁用按钮,直到操作完成.

只要操作繁忙,就可以向设置为true的表单中添加一个布尔值,并且当单击按钮时,仅在此值为false时才执行该操作.

更好的方法是向执行该操作的类添加一个属性.这样比较好,因为即使从代码的另一个位置调用了启动操作的方法,也可以确保该操作仅启动一次.这样,您以后就可以避免发生错误,例如可以使用按钮也可以通过菜单项开始操作.

祝你好运!
You can simply disable the button until the operation is done.

You can add a boolean to the form that you set to true as long as the operation is busy and when the button is clicked you only do the operation if this value is false.

A better method is to to add a property to the class that is doing the operation. This is better because this will ensure the operation to only start once, even if the method to start the operation is called from another place in the code. This way you prevent errors in the future when you could for example start the operation using a button but also from a menuitem.

Good luck!


这篇关于检查网络流是否为空.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆