GVKun编程网logo

在读取前冲洗/清除System.in(stdin)(在读取或执行并读取之前没有完成定义)

18

在这篇文章中,我们将为您详细介绍在读取前冲洗/清除System.in的内容,并且讨论关于stdin的相关问题。此外,我们还会涉及一些关于.net–System.Drawing(GDI)vsSystem

在这篇文章中,我们将为您详细介绍在读取前冲洗/清除System.in的内容,并且讨论关于stdin的相关问题。此外,我们还会涉及一些关于.net – System.Drawing(GDI)vs System.Windows.Media(WPF)vs Direct2D DirectWrite WIC、.net – ‘System.Windows.Data.Binding’类型的对象无法转换为’System.String’类型、AWK 怎么读取标准输入(STDIN)、C# Pen绘制虚线(System.Drawing.Pen与System.Windows.Media.Pen)的知识,以帮助您更全面地了解这个主题。

本文目录一览:

在读取前冲洗/清除System.in(stdin)(在读取或执行并读取之前没有完成定义)

在读取前冲洗/清除System.in(stdin)(在读取或执行并读取之前没有完成定义)

在工作中,我们有5个RFID阅读器连接到运行Linux的个人电脑上。 读者都被识别为键盘,并将它们的input(他们从芯片上读取的内容)作为键input事件序列发送。 为了能够告诉哪个阅读器发送了什么序列,我正在对/dev/input/XX进行原始读取,并以这种方式得到它们的input。

问题在于,由RFID阅读器生成的发送键盘事件仍然是“in”stdin,当我试图通过Scanner从system.in读取(这次input应该由普通键盘生成)时,我首先从读取器(包含十个hex十进制数字和一个换行符( n ))获得“挂起”input。

现在,问题是: @H_301_5@如何从标准input中清除所有这些“未决”input ,然后从键盘读取我真正想要的内容?

我试过了:

C ++控制台键盘事件

sendinput()和非英文字符和键盘布局

不能中断lock.acquire(),而我可以中断time.sleep()

数字键盘事件导致来自GetKeyboardState的卡住键

可以在Shift,Ctrl,Alt或Win键之一中使用其他用途,例如键入文本?

system.in.skip(system.in.available());

但是stdin不允许seek( skip抛出一个IOException )。

for (int i = 0; i < system.in.available(); i++){ system.in.read(); }

但available()不足够估计(之后仍然是stdin的东西)。

Scanner scanner = new Scanner(system.in); while (scanner.hasNextLine()){ scanner.nextLine(); } System.out.println("Clean!");

但hasNextLine()永远不会成为false (打印从不执行)。

BufferedReader in = new BufferedReader(new InputStreamReader(system.in)); String line; while ((line = in.readLine()) != null); System.out.println("Clean!");

同上。

任何人有更多的想法?

发送键盘到应用程序在c#(sendkeys,postmessage,sendmessage都不工作)

如何将键盘事件发送到Windows中的所有types的应用程序?

Python Tkinter使用键盘快捷方式去除窗口

用linux c shell上的向上箭头显示最后的命令

什么是Win32的虚拟键码0xFF用于和logging的地方?

根据@H_301_5@@Joni的建议,我把这些放在一起:

Scanner scanner = new Scanner(system.in); int choice = 0; while (scanner.hasNext()){ if (scanner.hasNextInt()){ choice = scanner.nextInt(); break; } else { scanner.next(); // Just discard this,not interested... } }

这将丢弃标准输入中已经“等待”的数据,并等待输入有效数据。 在这种情况下有效,意味着一个十进制整数。

没有内置的便携式方法来刷新输入流中的数据。 如果您知道未完成的数据以n结尾,为什么不读取,直到找到它?

这对我有效

system.in.read(new byte[system.in.available()])

设备通常使用定义好的协议发送数据,您可以使用该协议来解析数据段。

如果我是正确的,丢弃协议格式不正确的数据。 这可以让你过滤掉你不感兴趣的数据。

由于我不熟悉你使用的RFID扫描仪,我不能有更多的帮助,但这是我的建议。

你可以用多个线程来做到这一点。

您的真实应用程序将从连接到PipedOutputStream的PipedInputStream中读取

您需要连续从system.in读取一个线程。 只要真正的应用程序对来自system.in的数据不感兴趣(由一个布尔标志表示),该线程就会丢弃它读取的所有内容。 但是,当真正的应用程序设置标志来表明它对来自system.in的数据感兴趣时,则该线程将所有读取的数据发送到PipedOutputStream。

您的真实应用程序会打开标志来指示它对数据感兴趣,并在数据不再感兴趣时清除该标志。

这样,来自system.in的数据总是自动刷新/清除

.net – System.Drawing(GDI)vs System.Windows.Media(WPF)vs Direct2D DirectWrite WIC

.net – System.Drawing(GDI)vs System.Windows.Media(WPF)vs Direct2D DirectWrite WIC

对于处理图像并将用C#编写的Desktop Vista应用程序,您会推荐以下哪种技术(或者您推荐的组合:System.Drawing(GDI)vs System. Windows.Media vs Direct2D DirectWrite WIC.图像处理将涉及各种原始绘图,填充,使用文本,旋转,平移,缩放,直接使用指针处理像素数据.一般来说,上述技术在操作位图方面有多好?

此外,如果有人知道一个好的和详细的比较图表,列出了并排的不同功能和等价物,以及不支持的,这将是伟大的!例如,WPF和Direct2D中是否存在ColorMatrix等价物?例如,在研究了SharpDX对Direct2D的实现后,我留下的印象是即使微软将其定位为GDI的完全替代品,我也无法在Direct2D中找到所有GDI的功能.

另外,考虑到它是2014年,你会说至少对于图像处理(如果不是UI的一部分,WPF似乎是更好的选择),GDI在接下来的5年左右会好吗?或者我应该真的考虑使用所有3种技术,哪种方法效果最好?

谢谢.

解决方法

我目前在一个视觉密集型的C#/ WPF项目中工作,并且在 Python中使用图像处理做了一些最好的黑客攻击.

根据您的预期用途,我将使用以下方法:

>尽可能使用WPF名称空间,即System.Windows.Media.据我所知,System.Drawing和GDI通常使用非托管资源,这可能会产生不希望的,可避免的问题;
>尽可能严格地使用MVVM架构.也就是说,您有一个仅用于显示信息的View层,以及包含业务逻辑的Model和viewmodel层.虽然有些人可能会说视觉内容不属于viewmodel层,但如果你的应用程序处理图像操作,那么你的业务逻辑就像定义一样与View相关(我花了很长时间才弄明白这一点).
>如有疑问,请使用多维数组(在Model / viewmodel中)执行图像处理,并将这些数组绑定到View(使用ValueConverter通过DataBinding).

这样,例如,您可以使用ImageLayerviewmodel类来表示Layer,其中Data属性(或Value或PixelArray或Raster,无论如何)是double [height,width,depth].

我可以详细说明一下,但我不确定是否正确回答你的问题,所以如果你愿意,可以写任何评论.

.net – ‘System.Windows.Data.Binding’类型的对象无法转换为’System.String’类型

.net – ‘System.Windows.Data.Binding’类型的对象无法转换为’System.String’类型

我想知道是否有人可以帮忙.我现在已经半天不停地反对这个问题了,我一定是做错了.我有一个具有许多依赖项属性的自定义控件.

[TemplatePart(Name = informationBubble.informationBubbleTitlePart,Type = typeof(TextBlock))]
[TemplatePart(Name = informationBubble.informationBubbleProductimagePart,Type=typeof(Image))]

public class informationBubble : Control 
{
    #region Template Parts Name Constants

    /// <summary>
    /// Name constant for the information Bubble Title Part
    /// </summary>
    public const string informationBubbleTitlePart = "informationBubbleTitleText";

    /// <summary>
    /// Name constant for the information Bubble Product Image Part
    /// </summary>
    public const string informationBubbleProductimagePart = "informationBubbleProductimage";

    #endregion

    #region TemplateParts

    private TextBlock _Title;

    internal TextBlock Title
    {
        get { return _Title; }
        private set
        {
            _Title = value;

            if (_Title != null)
            {
                _Title.Text = this.ProductTitleText;       
            }
        }
    }

    private Image _Productimage;

    internal Image Productimage
    {
        get { return _Productimage; }
        private set
        {
            _Productimage = value;

            if (_Productimage != null)
            {
                _Productimage.source = this.ProductimageSource;
            }
        }
    }

    #endregion

    #region Public String Product Title 

    // Dependency properties declaration
    public static readonly DependencyProperty ProductTitleProperty = DependencyProperty.Register(
        "ProductTitle",typeof(string),typeof(informationBubble),new PropertyMetadata(string.Empty,new PropertyChangedCallback(OnProductTitleChanged)));

    public static void OnProductTitleChanged(DependencyObject sender,DependencyPropertyChangedEventArgs e)
    {
        informationBubble iBubble = sender as informationBubble;

        if (iBubble.Title != null)
        {
            iBubble.Title.Text = e.NewValue as string;
        }
    }

    public string ProductTitleText
    {
        get { return GetValue(ProductTitleProperty) as string; }
        set { SetValue(ProductTitleProperty,value); }
    }

    #endregion

    #region Public Image Source Product Image

    public static readonly DependencyProperty ProductimageSourceProperty = DependencyProperty.Register(
        "ProductimageSource",typeof(ImageSource),new PropertyMetadata(null,new PropertyChangedCallback(OnProductimageSourceChanged)));

    public static void OnProductimageSourceChanged(DependencyObject sender,DependencyPropertyChangedEventArgs e)
    {
        informationBubble iBubble = sender as informationBubble;

        if (iBubble.Productimage != null)
        {
            iBubble.Productimage.source = e.NewValue as ImageSource;
        }
    }

    public ImageSource ProductimageSource
    {
        get { return GetValue(ProductimageSourceProperty) as ImageSource; }
        set { SetValue(ProductimageSourceProperty,value); }
    }

    #endregion

    public informationBubble()
    {
         this.DefaultStyleKey = typeof(informationBubble);
    }

    #region Overrides

    public override void OnApplyTemplate()
    {       
        base.OnApplyTemplate();

        Title = GetTemplateChild(informationBubble.informationBubbleTitlePart) as TextBlock;
        Productimage = GetTemplateChild(informationBubble.informationBubbleProductimagePart) as Image;
    }

    #endregion

    #region Private Methods

    private void GoToState(string stateName,bool useTransitions)
    {
        visualstatemanager.GoToState(this,stateName,useTransitions);
    }

    #endregion
}

现在,如果我在我的xaml中的某个地方使用此控件,那么如果我执行此操作:

<controls:informationBubble 
        ProductimageSource="{Binding SelectedItem.normalImageSource}"
        ProductTitleText="Test Title"
        "/>

但是,如果我尝试将数据绑定到viewmodel中的SelectedItem对象的title属性的数据:

<controls:informationBubble 
            ProductimageSource="{Binding SelectedItem.normalImageSource}"
            ProductTitleText="{Binding SelectedItem.Title,Mode=TwoWay"
            "/>

我得到’System.Windows.Data.Binding’类型的对象不能转换为’System.String’类型. TextBlock的text属性是DependencyProperty,所​​以我必须在这里遗漏一些明显的东西.

非常感谢任何帮助或见解.

短剑的一种

解决方法

可能是该物业的名称是错误的.下面代码中的“ProductTitle”应该是“ProductTitleText”吗?

public static readonly DependencyProperty ProductTitleProperty = DependencyProperty.Register(
    "ProductTitle",// "ProductTitleText" ?
    typeof(string),new PropertyChangedCallback(OnProductTitleChanged)));

我想当你使用字符串常量时,WPF使用反射直接访问属性“public string ProductTitleText”. DependencyProperty被忽略,因为属性的名称不匹配(“ProductTitle”与“ProductTitleText”).

因此,对于标题,您有一个名为“ProductTitle”的依赖项属性和一个名为“ProductTitleText”的(字符串)属性.
对于Productimage,您有一个名为“ProductimageSource”的依赖项proeprty和一个名为“ProductimageSource”的属性(ImageSource类型).

是否有意义?

AWK 怎么读取标准输入(STDIN)

AWK 怎么读取标准输入(STDIN)

AWK 怎么读取标准输入(STDIN)AWK 怎么读取标准输入(STDIN)
在第一个例子下面,我们使用 dir -l 命令的输出作为 awk 命令的输入,这样就可以打印出文件拥有者的用户名,所属组组名以及在当前路径下他/她拥有的文件。

# dir -l | awk ''{print $3, $4, $9;}''

AWK 怎么读取标准输入(STDIN)AWK 怎么读取标准输入(STDIN)

列出当前路径下的用户文件

再来看另一个例子,我们使用 awk 表达式 ,在这里,我们想要在 awk 命令里使用一个表达式筛选出字符串来打印出属于 root 用户的文件。命令如下:

# dir -l | awk ''$3=="root" {print $1,$3,$4, $9;} ''

AWK 怎么读取标准输入(STDIN)AWK 怎么读取标准输入(STDIN)

列出 root 用户的文件

上面的命令包含了 (==) 来进行比较操作,这帮助我们在当前路径下筛选出 root 用户的文件。这是通过使用 $3=="root" 表达式实现的。

让我们再看另一个例子,我们使用一个 awk 比较运算符 来匹配一个确定的字符串。

这里,我们使用了 cat 命令 来浏览文件名为 tecmint_deals.txt 的文件内容,并且我们想要仅仅查看有字符串 Tech 的部分,所以我们会运行下列命令:

# cat tecmint_deals.txt
# cat tecmint_deals.txt | awk ''$4 ~ /tech/{print}''
# cat tecmint_deals.txt | awk ''$4 ~ /Tech/{print}''

AWK 怎么读取标准输入(STDIN)AWK 怎么读取标准输入(STDIN)

用 Awk 比较运算符匹配字符串

在上面的例子中,我们已经用了参数为 ~ /匹配字符/ 的比较操作,但是上面的两个命令给我们展示了一些很重要的问题。

当你运行带有 tech 字符串的命令时终端没有输出,因为在文件中没有 tech 这种字符串,但是运行带有 Tech 字符串的命令,你却会得到包含 Tech 的输出。

所以你应该在进行这种比较操作的时候时刻注意这种问题,正如我们在上面看到的那样,awk 对大小写很敏感。

你总是可以使用另一个命令的输出作为 awk 命令的输入来代替从一个文件中读取输入,这就像我们在上面看到的那样简单。

希望这些例子足够简单到可以使你理解 awk 的用法,如果你有任何问题,你可以在下面的评论区提问,记得查看 awk 系列接下来的章节内容,我们将关注 awk 的一些功能,比如变量,数字表达式以及赋值运算符。

免费提供最新Linux技术教程书籍,为开源技术爱好者努力做得更多更好:http://www.linuxprobe.com/

C# Pen绘制虚线(System.Drawing.Pen与System.Windows.Media.Pen)

C# Pen绘制虚线(System.Drawing.Pen与System.Windows.Media.Pen)

  一.绘制虚线的方法

  1.  GDI绘制,使用的是System.Drawing.Pen
        Pen pen = new Pen(Color.Red, 1);
            pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
            pen.DashPattern = new float[] { 5, 5 };
            Graphics gh = this.CreateGraphics();
            gh.DrawLine(pen, 10, 150, 500, 150);

    这种方法使用的Pen 是System.Drawing.Pen,它的DashStyle属性类型是个枚举类型,可以直接设置为DashStyle.Custom,这样就可以使用虚线了

    必须提出的一点是注意:这个更适合于WinForm工程使用,也是在我绘制虚线时查找资料提示最多的选项,但WPF类型显示是不能使用这个Pen类型的(System.Drawing)而是System.Windows.Media类型

 

  2.DrawingContext绘制,使用的是System.Windows.Media.Pen

    这种绘制一般都是Override OnRender方法时居多,我也是在这种场景下有个绘制虚线的需求,所以查了很多资料,先上代码

var pen = new Pen { Brush = Brushes.DeepSkyBlue, Thickness = 2};
                DashStyle dashStyle = new DashStyle();
                dashStyle.Offset = 0;
                dashStyle.Dashes = new DoubleCollection();
                dashStyle.Dashes.Add(2);
                dashStyle.Dashes.Add(5);
                pen.DashStyle = dashStyle;
                dc.DrawGeometry(null, pen, geometry);

    上述Pen类型为System.Windows.Media.Pen类型下的Pen,其DashStyle是个类,不是个枚举类型了,所以不能直接设置,需要实例化DashStyle对象,然后进行赋值使用

    附:DashStyle元数据,属性信息

    

 

     踩过坑,有个记录。

 

    

关于在读取前冲洗/清除System.instdin的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于.net – System.Drawing(GDI)vs System.Windows.Media(WPF)vs Direct2D DirectWrite WIC、.net – ‘System.Windows.Data.Binding’类型的对象无法转换为’System.String’类型、AWK 怎么读取标准输入(STDIN)、C# Pen绘制虚线(System.Drawing.Pen与System.Windows.Media.Pen)等相关内容,可以在本站寻找。

本文标签: