ListBox does not get updated

本文关键字:updated get not does ListBox | 更新日期: 2023-09-27 18:19:05

我有一个表格,上面有两个ListBoxes。我有removeFromBoxWaitin g监视在目录中创建新文件。
一旦创建了文件,它就会打印出来,然后将文件名添加到列表框中。
我的问题是,列表框没有得到更新。项目实际上已经添加,但他们没有显示,我也尝试过update(),但它没有工作。所以任何建议都会很感激。提前谢谢。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using System.Threading;
using System.Configuration;
namespace PrntToKitchen
{
public partial class PrintFiles : DevExpress.XtraBars.Ribbon.RibbonForm
{
    FileSystemWatcher filesWatcher = new FileSystemWatcher();
    public void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
    {          
        MessageBox.Show(e.Item.Name);
        if (Properties.Settings.Default[e.Item.Name + "Read"].ToString().Length > 1 && Properties.Settings.Default[e.Item.Name + "ExtIn"].ToString().Length > 1)
        {
            filesWatcher.Path = Properties.Settings.Default[e.Item.Name + "Read"].ToString();
            filesWatcher.Created += new System.IO.FileSystemEventHandler(eventG);
            //filesWatcher.Deleted += new System.IO.FileSystemEventHandler(addToBoxFinished);
            filesWatcher.EnableRaisingEvents = true;                    
        }
    }
    public void eventG(object sender, FileSystemEventArgs e)
    {
        string CurrentPrinter = "";
        string findPrt = e.FullPath.Substring(0, e.FullPath.Length-(e.Name.Length + 1));
        string findPrtExt = e.Name.Substring(e.Name.LastIndexOf("."));
        for (int i = 1; i <= Properties.Settings.Default.NumberOfPrinters; i++)
        {
            string testPrt = Properties.Settings.Default["Printer" + i + "Read"].ToString();
            string testExt = Properties.Settings.Default["Printer" + i + "ExtIn"].ToString();
            if (testPrt == findPrt && testExt == findPrtExt)
            {
                CurrentPrinter = "Printer" + i.ToString();
                POSPrinter printer = new POSPrinter(Properties.Settings.Default["Printer" + i + "Port"].ToString(), (int)Properties.Settings.Default["Printer" + i + "Speed"]);
                addToBoxWaiting(e.FullPath.ToString());
                string file = e.FullPath;
                printer.BeginPrint();
                printer.PrintFile(file);
                printer.EndPrint();
                printer.Dispose();
                if ((bool)Properties.Settings.Default[CurrentPrinter + "Delete"])
                {
                    IsFileLocked(file);
                    System.IO.File.Delete(file);
                }
                else
                {
                    System.IO.File.Move(file, Properties.Settings.Default[CurrentPrinter + "Store"] + "''" + e.Name + Properties.Settings.Default[CurrentPrinter + "ExtOut"]);
                }
                removeFromBoxWaiting(e.FullPath.ToString());
                addToBoxFinished(e.FullPath.ToString());
                busy = false;
                break;
            }
        }
    }
    void addToBoxWaiting(string text)
    {
        listBox1.Items.Add(text);
    }
    void removeFromBoxWaiting(string text)
    {
        listBox1.Items.Remove(text);
    }
    public void addToBoxFinished(string destination)
    {
       listBox2.Items.Add(destination);
    }
}

}

ListBox does not get updated

我相信你的问题是,你试图添加/删除列表框项目,而通过循环迭代(这两个动作都发生在UI线程)。您应该将for循环移动到一个单独的线程中,然后您的addToBoxWaiting方法将看起来像这样:

private void AddToListBox(string item)
{
    MethodInvoker del = delegate
    {
        listBox1.Items.Add(item);
    };
    BeginInvoke(del);
}

编辑。添加线程代码。

    public void eventG(object sender, FileSystemEventArgs e)
    {
        Thread eventThread = new Thread(ThreadProcEventG);
        eventThread.Start(e);
    }
    private void ThreadProcEventG(object eventArgs)
    {
        var e = (FileSystemEventArgs)eventArgs;
        string CurrentPrinter = "";
        string findPrt = e.FullPath.Substring(0, e.FullPath.Length-(e.Name.Length + 1));
        string findPrtExt = e.Name.Substring(e.Name.LastIndexOf("."));
        for (int i = 1; i <= Properties.Settings.Default.NumberOfPrinters; i++)
        {
            string testPrt = Properties.Settings.Default["Printer" + i + "Read"].ToString();
            string testExt = Properties.Settings.Default["Printer" + i + "ExtIn"].ToString();
            if (testPrt == findPrt && testExt == findPrtExt)
            {
                CurrentPrinter = "Printer" + i.ToString();
                POSPrinter printer = new POSPrinter(Properties.Settings.Default["Printer" + i + "Port"].ToString(), (int)Properties.Settings.Default["Printer" + i + "Speed"]);
                addToBoxWaiting(e.FullPath.ToString());
                string file = e.FullPath;
                printer.BeginPrint();
                printer.PrintFile(file);
                printer.EndPrint();
                printer.Dispose();
                if ((bool)Properties.Settings.Default[CurrentPrinter + "Delete"])
                {
                    IsFileLocked(file);
                    System.IO.File.Delete(file);
                }
                else
                {
                    System.IO.File.Move(file, Properties.Settings.Default[CurrentPrinter + "Store"] + "''" + e.Name + Properties.Settings.Default[CurrentPrinter + "ExtOut"]);
                }
                removeFromBoxWaiting(e.FullPath.ToString());
                addToBoxFinished(e.FullPath.ToString());
                busy = false;
                break;
            }
        }
    }

您可以尝试以下代码:

listBox.Dispatcher.Invoke(ew Action(()=>listBox.Item.Add(item))