Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#移动扫描仪价值观怪异_C#_Delegates_Windows Mobile_Barcode Scanner - Fatal编程技术网

C#移动扫描仪价值观怪异

C#移动扫描仪价值观怪异,c#,delegates,windows-mobile,barcode-scanner,C#,Delegates,Windows Mobile,Barcode Scanner,因此,我编写了一个快速示例程序,以更好地理解Janam扫描器枪,但是我遇到了我从未见过的问题,尽管我相信可能是由于在不同的线程上以及在线程之间传递值造成的。因此,我认为我没有正确地使用代理。我们将不胜感激 using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Text; using System.Windows.Forms; u

因此,我编写了一个快速示例程序,以更好地理解Janam扫描器枪,但是我遇到了我从未见过的问题,尽管我相信可能是由于在不同的线程上以及在线程之间传递值造成的。因此,我认为我没有正确地使用代理。我们将不胜感激

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Scanner;

namespace ScannerTest
{
public partial class Form1 : Form
{
    //Delegates
    private delegate void RefreshValuesDelegate();
    private delegate void AddScannedItemDelegate(Item item);
    // SINGLETON //////////////////////////////////////
    private static Form1 instance = null;

    public static Form1 GetInstance()
    {
        if (instance == null)
            instance = new Form1();

        return instance;
    }
    ///////////////////////////////////////////////////

    public Form1()
    {
        InitializeComponent();
    }
    void Form1_Load(object sender, EventArgs e)
    {
        /**************/
        //SCANNER ACTIVATE                       

        GlobalScanner.GetInstance().Close();
        GlobalScanner.GetInstance().BarcodeDelegateDirector = new GlobalScanner.BarcodeDelegate(Form1.GetInstance().processScannedBarcode);
        GlobalScanner.GetInstance().Open();
        /**************/

    }
    // Add the new part unless the part number contains 
    // spaces. In that case cancel the add. 
    private void button1_Click(object sender, EventArgs e)
    {
        if (textBox1.Text.Equals("") || textBox2.Text.Equals(""))
        {
            MessageBox.Show("item names or barcodes cannot be blank.");
        }
        else
        {
            Item temp = new Item(textBox1.Text, textBox2.Text, DateTime.Now);
            if (temp.ItemCheck == true)
            {
                AddToList(temp);
            }
        }
    }
    public void processScannedBarcode(string scannedBarcode)
    {
        if (scannedBarcode != null && scannedBarcode.Length > 0) // 0 = SUCCESS Symbol.Results.SUCCESS
        {
            Item temp = new Item();
            temp.ItemName = "N/A";
            temp.BarcodeNumber = scannedBarcode;
            String tempDate = DateTime.Now.ToShortDateString();
            String tempTime = DateTime.Now.ToShortTimeString();
            temp.ScanDate = tempDate + tempTime;
            AddScannedItem(temp);
        }
    }
    private void AddScannedItem(Item item)
    {
        if (this.InvokeRequired == true)
        {
            this.Invoke(new AddScannedItemDelegate(AddScannedItem), new object[] { item });
        }
        else
        {
            this.textBox2.Text = item.BarcodeNumber;
            this.textBox1.Text = item.ItemName; // description not available
            item.ScanDate = DateTime.Now.ToLongDateString();
            //DateTime readDate = DateTime.Now;
            //cargo.SetReadDate(readDate);
            RefreshValues();
            AddToList(item);
        }
    }
    private void AddToList(Item item)
    {
        string tempItem = item.ItemName;
        string tempBarcode = item.BarcodeNumber;
        string tempDate = item.ScanDate;
        ListViewItem newRow = new ListViewItem(tempItem);
        newRow.SubItems.Add(tempBarcode);
        newRow.SubItems.Add(tempDate);
        listView1.Items.Add(newRow);
        RefreshValues();
        //MessageBox.Show(string.Format("TextBox1: {0} TextBox2: {1}", tempItem, tempBarcode));
        textBox2.Text = "";
        textBox1.Text = "";
    }
    public void RefreshValues()
    {
        if (this.InvokeRequired == true)
        {
            this.Invoke(new RefreshValuesDelegate(RefreshValues));
        }
        else
        {
            listView1.Refresh();
        }
    }
}

// A simple business object for example purposes. 
public class Item
{
    private string name;
    private string number;
    private string date;
    private bool check = true;

    public Item() { }
    public Item(string nameForItem, string numberForBarcode, DateTime scandate)
    {
        ItemName = nameForItem;
        BarcodeNumber = numberForBarcode;
        date = scandate.ToShortDateString();
    }

    public string ItemName
    {
        get 
        { 
            return name; 
        }
        set 
        {
            if (value.Length <= 45)
            {
                name = value;
            }
            else
            {
                MessageBox.Show("Item name to long. Must be less than 45 characters.");
                ItemCheck = false;
            }
        }
    }

    public string BarcodeNumber
    {
        get 
        { 
            return number; 
        }
        set 
        {
            if (value.Length <= 20)
            {
                number = value; 
            }
            else
            {
                MessageBox.Show("Barcode is to long. Must be less than 20 digits.");
                ItemCheck = false;
            }
        }
    }
    public string ScanDate
    {
        get
        {
            return date;
        }
        set
        {
            date = value;
        }
    }

    public bool ItemCheck
    {
        get
        {
            return check;
        }
        set
        {
            check = value;
        }
    }
}
}

如果您设备的扫描仪设置为在扫描结束后附加回车符,则您的文本框可能会将该行置空

例如,如果在文本框中写入“Hello World!\n”,会发生什么情况

更新:

查看设置以配置设备的扫描选项(在下图中称为解码)

这是针对我的Datalogic Falcon的,但您的XM66应该如下所示:

从这里开始,您可能需要调整扫描仪设置

同样,您的XM66将有所不同,如下所示:


如果设备的扫描仪设置为在扫描结束后附加回车符,则您的文本框可能会将该行置空

例如,如果在文本框中写入“Hello World!\n”,会发生什么情况

更新:

查看设置以配置设备的扫描选项(在下图中称为解码)

这是针对我的Datalogic Falcon的,但您的XM66应该如下所示:

从这里开始,您可能需要调整扫描仪设置

同样,您的XM66将有所不同,如下所示:


如果您认为这是一个线程间问题,可以使用锁对象来确保只有一个线程可以访问事件数据:

全球性的 对象锁定对象=新对象(); 必须定义并在AddToList中包含所有代码 锁定(锁定对象) { } 上述方法确保一次只运行一个AddToList


对于使用条形码扫描仪的代理,我看到的另一个区别是代理的用法:在我的代码中,我首先声明代理的一个新变量,然后调用该变量上的invoke。但这不会对函数产生影响。

如果您认为这是线程间的问题,可以使用lock对象来确保只有一个线程可以访问代理事件数据:

全球性的 对象锁定对象=新对象(); 必须定义并在AddToList中包含所有代码 锁定(锁定对象) { } 上述方法确保一次只运行一个AddToList


使用条形码扫描器的代理的另一个不同之处是代理的用法:在我的代码中,我首先声明了代理的一个新变量,然后调用该变量的调用。但这不会对函数产生影响。

因此,我终于找到了解决问题的方法,尽管我不完全理解为什么这样可以解决我的问题只有当我将所有textbox1.text设置为form1.GetInstance().textbox1.text时,我才偶然发现它,它开始使我的手动输入与扫描输入的操作方式相同。这让我相信我的form1.instance在设置表单上的项目时遇到了问题。对于这个问题的未来答案寻求者,我用以下更改修复了我的代码。 由此

/**************/
//SCANNER ACTIVATE                       
GlobalScanner.GetInstance().Close();
GlobalScanner.GetInstance().BarcodeDelegateDirector = new GlobalScanner.BarcodeDelegate(Form1.GetInstance().processScannedBarcode);
GlobalScanner.GetInstance().Open();
/**************/
对此

/**************/
//SCANNER ACTIVATE
GlobalScanner.GetInstance().Close();
GlobalScanner.GetInstance().BarcodeDelegateDirector = new GlobalScanner.BarcodeDelegate(processScannedBarcode);
GlobalScanner.GetInstance().Open();
/**************/

简单地删除Form1.GetInstance()就解决了这个问题,但是只有一个实例,所以应该没有什么区别。如果有人能为我和未来的答案寻求者解释这一点,我们将不胜感激。

因此,我最终找到了解决问题的方法,尽管我不完全理解为什么这样可以解决我的问题,只是在我将所有textbox1.text设置为form1.GetInstance()时偶然发现了它.textbox1.text开始使我的手动输入与扫描输入的作用相同。这让我相信我的form1.instance在设置表单上的项目时遇到了问题。对于这个问题的未来答案寻求者,我用以下更改修复了我的代码。 由此

/**************/
//SCANNER ACTIVATE                       
GlobalScanner.GetInstance().Close();
GlobalScanner.GetInstance().BarcodeDelegateDirector = new GlobalScanner.BarcodeDelegate(Form1.GetInstance().processScannedBarcode);
GlobalScanner.GetInstance().Open();
/**************/
对此

/**************/
//SCANNER ACTIVATE
GlobalScanner.GetInstance().Close();
GlobalScanner.GetInstance().BarcodeDelegateDirector = new GlobalScanner.BarcodeDelegate(processScannedBarcode);
GlobalScanner.GetInstance().Open();
/**************/

简单地删除Form1.GetInstance()就解决了这个问题,但是只有一个实例,所以应该没有什么区别。如果有人能为我和未来的答案寻求者解释这一点,我们将不胜感激。

问题是,每次调用GetInstance()时,实例都保持为空。GetInstance()递归地创建一个新的form1(在添加instance=this;之前,仅使用调试器并逐步执行以下代码):

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

namespace FormInstance
{
    public partial class Form1 : Form
    {
        private static Form1 instance = null;

        public static Form1 GetInstance()
        {
            if (instance == null)
                instance = new Form1();

            return instance;
        }

        public Form1()
        {
            InitializeComponent();
            // doTest();   //results in a recursive call!
            ///without the following instance will never be different than null, as GetInstance creates new Form on every call!
            instance = this;    
        }
        void doTest()
        {
            Form f1 = this;
            Form f2 = Form1.GetInstance();
            System.Diagnostics.Debug.WriteLine("f1=" + f2.GetType().FullName);
            System.Diagnostics.Debug.WriteLine("f2=" + f2.GetType().FullName);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            doTest();   //does also not return the current form!
        }
    }
}

问题是每次调用GetInstance()时,instance都会保持null。GetInstance()递归地创建一个新的form1(在添加instance=this之前,仅使用调试器并逐步遍历以下代码):

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

namespace FormInstance
{
    public partial class Form1 : Form
    {
        private static Form1 instance = null;

        public static Form1 GetInstance()
        {
            if (instance == null)
                instance = new Form1();

            return instance;
        }

        public Form1()
        {
            InitializeComponent();
            // doTest();   //results in a recursive call!
            ///without the following instance will never be different than null, as GetInstance creates new Form on every call!
            instance = this;    
        }
        void doTest()
        {
            Form f1 = this;
            Form f2 = Form1.GetInstance();
            System.Diagnostics.Debug.WriteLine("f1=" + f2.GetType().FullName);
            System.Diagnostics.Debug.WriteLine("f2=" + f2.GetType().FullName);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            doTest();   //does also not return the current form!
        }
    }
}

这也是在windows mobile 5.0上使用visual Studio 2008。您知道同步原语吗?互斥体、WaitHandles等?如果存在争用条件,您必须保护两个线程可以同时处理数据的区域。请参阅
System.Threading
-命名空间。除此之外,您能告诉我们争用的可能部分吗可能会出现这种情况?不,我目前不知道同步0原语或互斥。但我确实理解您的意图。我相信我的AddToList函数是它们可能重叠的唯一地方,但我不确定。为什么每次添加后都要刷新?这有必要吗?不过,我认为您没有种族条件ition,因为您正在按顺序执行所有操作,例如扫描一个项目,它将被放入列表中,扫描下一个项目,…对吗?不,我认为我的问题是添加扫描仪后列表没有更新,所以我测试了它,这不是问题所在。这也是使用windows mobile 5.0上的visual Studio 2008。您知道s吗同步原语?互斥体、WaitHandles等?如果存在竞态条件,则必须保护两个线程可以同时操作数据的区域。请参阅
System.Threading
-命名空间。除此之外,您能告诉竞态条件可能发生的部分吗?不,我目前不知道同步0