简体   繁体   English

C#:Windows应用程序读取CSV文件并写入控制台问题

[英]C#: Windows App Reading CSV file and Writing to a Console Issue

I have a Windows Forms app that is able to launch a console for debuggin. 我有一个Windows Forms应用程序,该应用程序可以启动调试控制台。 In the app, through a menu click, I read a CSV file and write it to the console. 在应用程序中,通过单击菜单,我读取了一个CSV文件并将其写入控制台。 The function that does this is below. 执行此功能的功能如下。

    protected void menuRead_Click(object sender, EventArgs e)
    {
        // ... functionality to load CSV files
        System.IO.Stream inputDataFile = null;
        OpenFileDialog fd = new OpenFileDialog();
        fd.InitialDirectory = "c:\\";
        fd.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*";
        fd.FilterIndex = 1;
        fd.RestoreDirectory = true;
        if (fd.ShowDialog() == DialogResult.OK)
        {
            try
            {
                if ((inputDataFile = fd.OpenFile()) != null)
                {
                    inputData_exists = true;
                    // ... read input data from CSV file
                    using (CsvFileReader reader = new CsvFileReader(inputDataFile))
                    {
                        CsvRow row = new CsvRow();
                        while (reader.ReadRow(row))
                        {
                            foreach (string s in row)
                            {
                                Console.Write(s);
                                Console.Write(" ");
                            }
                            Console.WriteLine();
                        }

                        // ... close the input data stream
                        inputDataFile.Close();
                    }
                }
            }
            catch (Exception err)
            {
                //Inform the user if can't read the file
                MessageBox.Show(err.Message);
            }
        }
    }

Everything works fine except the following: The csv file has about 1200 lines of code. 除以下各项外,其他所有东西都可以正常工作:csv文件包含大约1200行代码。 When this code is executed, the OpenFileDialog() window only closes partially before the csv file contents begin to get written to the console window. 执行此代码后,在csv文件内容开始写入控制台窗口之前,OpenFileDialog()窗口仅部分关闭。 So, I can see the data writing to the console window, and I have a small rectangular portion of the dialog window showing on my form. 因此,我可以看到数据正在写入控制台窗口,并且在窗体上显示了对话框窗口的一小部分矩形。 Is there any way to ensure the dialog is closed before the data is written to the console? 有什么方法可以确保在将数据写入控制台之前关闭对话框? Should I open anew thread to communicate with the console? 我应该打开一个新线程来与控制台通信吗? Any advise or help woulf be greatly appreciated. 任何建议或帮助将不胜感激。 Thank you. 谢谢。

Your issue comes from the fact you display the console and your ShowDialog call. 您的问题来自显示控制台和ShowDialog调用的事实。 Get the result of the of Dialog then open your console application. 获取Dialog的结果,然后打开控制台应用程序。 You can also read the file on another thread I suppose. 您还可以在我想的另一个线程上读取文件。

You may use a lot of approaches here. 您可以在这里使用很多方法。 The simplest one: Use StringBuilder and than put all data at once. 最简单的一种:使用StringBuilder,然后一次放置所有数据。 Because output to console may be quite slowly. 因为输出到控制台的速度可能很慢。

StringBuilder consoleBuffer = new StringBuilder();
using (CsvFileReader reader = new CsvFileReader(inputDataFile))
{
    CsvRow row = new CsvRow();
    while (reader.ReadRow(row))
    {
        foreach (string s in row)
        {
            consoleBuffer.Append(s);
            consoleBuffer.Append(" ");
        }
        consoleBuffer.Append(Environment.NewLine);
    }

    Console.WriteLine(consoleBuffer.ToString());

    // ... close the input data stream
    inputDataFile.Close();
}

You need to give some time for your control "OpenFileDialog" to repaint itself during dispose of the control, the easiest way for WinForm is to use Application.DoEvents() 您需要花一些时间让控件“ OpenFileDialog”在处理该控件时重新绘制自身,WinForm的最简单方法是使用Application.DoEvents()

if (fd.ShowDialog() == DialogResult.OK)
{      
    Application.DoEvents();
    ...
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM