Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.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#,加载que中的第一项并刷新_C#_.net_Listbox_Task Queue - Fatal编程技术网

队列任务列表框c#,加载que中的第一项并刷新

队列任务列表框c#,加载que中的第一项并刷新,c#,.net,listbox,task-queue,C#,.net,Listbox,Task Queue,好的,在c#中,我尝试了多种方法让列表框获取由其他程序填充的文本文件的内容,以列出内容。如果某些条件为真,则根据文本文件列表框中第一项的内容执行程序。程序运行后,会从文本文件中删除执行行(这是我的程序无法继续的地方),我希望列表框刷新或重新加载文本文件,再次根据第一行文本文件的内容加载程序 现在,如果使用列表框不是办法,我洗耳恭听。我只想能够显示队列中的内容。我的程序的其余部分运行得很好,只是这一部分,我似乎不明白。我曾尝试使用FileWatcher和bindsource,但也许我做错了什么。我

好的,在c#中,我尝试了多种方法让列表框获取由其他程序填充的文本文件的内容,以列出内容。如果某些条件为真,则根据文本文件列表框中第一项的内容执行程序。程序运行后,会从文本文件中删除执行行(这是我的程序无法继续的地方),我希望列表框刷新或重新加载文本文件,再次根据第一行文本文件的内容加载程序

现在,如果使用列表框不是办法,我洗耳恭听。我只想能够显示队列中的内容。我的程序的其余部分运行得很好,只是这一部分,我似乎不明白。我曾尝试使用FileWatcher和bindsource,但也许我做错了什么。我对c#很陌生,所以非常感谢您的帮助。如果需要,我可以将代码片段显示在这里。谢谢

编辑:这是我当前的一些代码。这是非常屠宰作为目前我一直在尝试不同的方法。我没有它的设置加载任何东西,仍然试图让它更新时,文本文件的变化

public partial class Form1 : Form
{


    private FileSystemWatcher _watcher;
   List<string> myList =  new List<string>();

    public Form1()
    {
        InitializeComponent();

        myList = System.IO.File .ReadLines("C:\\inetpub\\wwwroot\\imomo\\Orders.txt").ToList();

        BindingSource binding = new BindingSource(myList.ToList(), null);
        GPSCOM.DataSource = binding;
        _watcher = new FileSystemWatcher();
        _watcher.Path = Path.GetDirectoryName("C:\\inetpub\\wwwroot\\imomo");
        _watcher.Filter=Path.GetFileName("C:\\inetpub\\wwwroot\\imomo\\Orders.txt");
        _watcher.SynchronizingObject = GPSCOM;
        _watcher.EnableRaisingEvents = true;
        _watcher.Changed += new FileSystemEventHandler(fileSystemWatcher1_Changed);
        _watcher.Error += new ErrorEventHandler(GPSCOMerror);
        GPSCOM.SelectionMode = SelectionMode.One;
        //myList.FirstOrDefault();
         if (serialPort.IsOpen)
        {
            return;
        }
        else
        {
            for (; ; )
            {
                switch ((string)GPSCOM.SelectedItem)
                {
                    case "task1":

                        var lines = System.IO.File.ReadAllLines("C:\\inetpub\\wwwroot\\imomo\\Orders.txt");
                        System.IO.File.WriteAllLines("C:\\inetpub\\wwwroot\\imomo\\Orders.txt", lines.Skip(1).ToArray());
                        //BindingSource binding = new BindingSource(myList.ToList(), null);

                        myList = System.IO.File.ReadLines("C:\\inetpub\\wwwroot\\imomo\\Orders.txt").ToList();
                        GPSCOM.DataSource = binding;
                        GPSCOM.SelectionMode = SelectionMode.One;
                        myList.FirstOrDefault();
                        GPSCOM.SetSelected(0, true);
                        GPSCOM.Refresh();

                        return;
                    case "task2":

                        var lines1 = System.IO.File.ReadAllLines("C:\\inetpub\\wwwroot\\imomo\\Orders.txt");
                        System.IO.File.WriteAllLines("C:\\inetpub\\wwwroot\\imomo\\Orders.txt", lines1.Skip(1).ToArray());
                        //BindingSource binding = new BindingSource(myList.ToList(), null);

                        myList = System.IO.File.ReadLines("C:\\inetpub\\wwwroot\\imomo\\Orders.txt").ToList();
                        GPSCOM.DataSource = binding;
                        GPSCOM.SelectionMode = SelectionMode.One;
                        myList.FirstOrDefault();
                        GPSCOM.SetSelected(0, true);
                        GPSCOM.Refresh();

                        return;
                    case "Task3":

                        var lines2 = System.IO.File.ReadAllLines("C:\\inetpub\\wwwroot\\imomo\\Orders.txt");
                        System.IO.File.WriteAllLines("C:\\inetpub\\wwwroot\\imomo\\Orders.txt", lines2.Skip(1).ToArray());
                        //BindingSource binding = new BindingSource(myList.ToList(), null);

                        myList = System.IO.File.ReadLines("C:\\inetpub\\wwwroot\\imomo\\Orders.txt").ToList();
                        GPSCOM.DataSource = binding;
                        GPSCOM.SelectionMode = SelectionMode.One;
                        myList.FirstOrDefault();
                        GPSCOM.SetSelected(0, true);
                        GPSCOM.Refresh();

                        return;
                    case "":
                        return;
                }
            }
            }

    }

你需要这样的东西吗

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            listBox1.DataSource = TaskQue.GetTasks();
        }

        private void Execute(object sender, EventArgs e)
        {
            string task = TaskQue.Pop();
            //execute task;
            listBox1.DataSource = TaskQue.GetTasks();
        }

        private void AddTask(object sender, EventArgs e)
        {
            TaskQue.Push(textBox1.Text);
            listBox1.DataSource = TaskQue.GetTasks();

        }
    }

    public class TaskQue
    {
        public static string txtPath = "C:/a.txt";

        public static string Pop()
        {
            StreamReader sr = new StreamReader(txtPath);
            string result = sr.ReadLine();
            string remaining = sr.ReadToEnd();
            sr.Close();
            StreamWriter sw = new StreamWriter(txtPath, false);
            sw.Write(remaining);
            sw.Close();
            return result;
        }

        public static void Push(string s)
        {

            StreamWriter sw = new StreamWriter(txtPath, true);
            sw.WriteLine(s);
            sw.Close();
        }

        public static IEnumerable<string> GetTasks()
        {
            return new List<string>(File.ReadLines(txtPath));
        }
    }
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
listBox1.DataSource=TaskQue.GetTasks();
}
私有void Execute(对象发送方、事件参数)
{
string task=TaskQue.Pop();
//执行任务;
listBox1.DataSource=TaskQue.GetTasks();
}
私有void AddTask(对象发送方,事件参数e)
{
TaskQue.Push(textBox1.Text);
listBox1.DataSource=TaskQue.GetTasks();
}
}
公开课
{
公共静态字符串txtPath=“C:/a.txt”;
公共静态字符串Pop()
{
StreamReader sr=新的StreamReader(txtPath);
字符串结果=sr.ReadLine();
剩余字符串=sr.ReadToEnd();
高级关闭();
StreamWriter sw=新StreamWriter(txtPath,false);
软件写入(剩余);
sw.Close();
返回结果;
}
公共静态无效推送(字符串s)
{
StreamWriter sw=新StreamWriter(txtPath,true);
西南书写线(s);
sw.Close();
}
公共静态IEnumerable GetTasks()
{
返回新列表(File.ReadLines(txtPath));
}
}

请提供一些代码,否则很难。如果您正在使用Process.Start执行程序,则可以侦听退出事件或等待执行完成,然后从列表框中删除第一个条目。无需查看文件或进行任何绑定。好的,我尝试了您的代码,当文件被其他程序更改时,列表框不会更新。尽管很高兴看到我的问题有了不同的解决方法。
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            listBox1.DataSource = TaskQue.GetTasks();
        }

        private void Execute(object sender, EventArgs e)
        {
            string task = TaskQue.Pop();
            //execute task;
            listBox1.DataSource = TaskQue.GetTasks();
        }

        private void AddTask(object sender, EventArgs e)
        {
            TaskQue.Push(textBox1.Text);
            listBox1.DataSource = TaskQue.GetTasks();

        }
    }

    public class TaskQue
    {
        public static string txtPath = "C:/a.txt";

        public static string Pop()
        {
            StreamReader sr = new StreamReader(txtPath);
            string result = sr.ReadLine();
            string remaining = sr.ReadToEnd();
            sr.Close();
            StreamWriter sw = new StreamWriter(txtPath, false);
            sw.Write(remaining);
            sw.Close();
            return result;
        }

        public static void Push(string s)
        {

            StreamWriter sw = new StreamWriter(txtPath, true);
            sw.WriteLine(s);
            sw.Close();
        }

        public static IEnumerable<string> GetTasks()
        {
            return new List<string>(File.ReadLines(txtPath));
        }
    }