我有一张表格,上面有两个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);
}
}
}
答案 0 :(得分:0)
我相信你的问题是你在迭代循环时试图添加/删除列表框项(这两个动作都发生在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;
}
}
}
答案 1 :(得分:0)
您可以尝试以下代码:
listBox.Dispatcher.Invoke(ew Action(()=>listBox.Item.Add(item))