C#.net代码异常 [英] C#.net code Exception

查看:76
本文介绍了C#.net代码异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public   void 打开()
{
if this .serialPort.IsOpen == false
{
this .serialPort.Open();
}
}





this.serialPort.Open();

存取遭拒和IOException异常发生

解决方案
仅仅因为你可以创建一个的SerialPort对象和端口设置到适当的值并不意味着端口可用:物理端口不能由应用程序共享,因为这会使接收的数据被两个应用程序共享。这将导致所有数据都必须缓冲到两者 - 这是不现实的,因为Windows必须缓冲未知数量的应用程序,这需要时间,并且串行端口处理应该尽可能快以防止溢出 - 或者哪个应用程序得到哪些数据是随机的。



如前所述,另一个应用程序首先到达那里 - 所以你无法打开端口。使用 try ... catch 块来检测并向用户报告问题 - 您无法共享端口!


public void Opens()
{
    if (this.serialPort.IsOpen == false)
    {
        this.serialPort.Open();
    }
}



Showing Exception on this.serialPort.Open();
AccessDenied and IOException occured

解决方案

Just because you can create a SerialPort object and set the port to the appropriate value does not mean that the port is available: physical ports cannot be shared by applications since that would bean the received data being "shared" by two applications. This would beam that either all the data would have to be buffered to both - which is unrealistic since Windows would have to buffer an unknown number of applications, which takes time, and serial port processing is supposed to be as quick as possible to prevent overrun - or which application got which data would be random.

As has been suggested, another application got there first - so you cannot open the port. Use a try...catch block to detect this and report the problem to your user - you can't "share" ports!


这篇关于C#.net代码异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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