简体   繁体   English

除非我关闭我的表单,否则我的代码不会运行,但是我需要打开我的表单,因为我想显示来自其他代码的信息

[英]My code won't run unless I close my form, however I need my form to be open as I want to display information from other code

I have two seperate files, Form1.cs and Program.cs.我有两个单独的文件,Form1.cs 和 Program.cs。 I want Program.cs to start running once I choose a filepath in my form by pressing button1 and selecting the file destination, however it only starts running once I CLOSE the form.一旦我通过按 button1 并选择文件目标在我的表单中选择一个文件路径,我希望 Program.cs 开始运行,但是它只有在我关闭表单后才开始运行。 I can't run my program.cs while the form is still open, however I want to display information on the form which I gain from program.cs.. This is how my form code looks:当表单仍然打开时,我无法运行我的 program.cs,但是我想在表单上显示从 program.cs 获得的信息。这就是我的表单代码的外观:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text.RegularExpressions;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;

namespace test
{
    public partial class Form1 : Form
    {
        public string Pathh;
        public Form1()
        {
            InitializeComponent();

        }

        public void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog
            {
                InitialDirectory = @"D:\",
                Title = "Browse Text Files",

                CheckFileExists = true,
                CheckPathExists = true,

                DefaultExt = "csv",
                Filter = "csv files (*.csv)|*.csv",
                FilterIndex = 2,
                RestoreDirectory = true,

                ReadOnlyChecked = true,
                ShowReadOnly = true
            };

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;

            }
        }
    }
}




And this is my main in Program.cs:这是我在 Program.cs 中的主要内容:


namespace test
{
    class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Form1 form = new Form1();
            form.ShowDialog();
            if (form.checkBox1.Checked)
            {
                Console.Write("LOL");
                Dictionary<string, string> LocatieProduct = new Dictionary<string, string>();
                Dictionary<string, int> PickCountSchap = new Dictionary<string, int>();
                /*for (int i = 0; i <= 17; i++)
                {


                    StringBuilder sb = new StringBuilder();
                    if (i < 10)
                        sb.Append("D0" + i);
                    else
                        sb.Append("D" + i);
                    string final = sb.ToString();
                    Console.WriteLine(final);
                    PickCountGang[final] = 1;
                }
                */
                Dictionary<string, int> PickCount = new Dictionary<string, int>();
                string path = form.textBox1.Text;


                var textBoxes = new List<Control>();
                foreach (Control c in form.Controls)
                {
                    if (c is TextBox)
                    {
                        textBoxes.Add(c);
                    }
                }
                using (var reader = new StreamReader(@path))
                {
                    int count = 0;
                    while (!reader.EndOfStream)
                    {
                        count++;


                        var line = reader.ReadLine().Split(';');

                        string TypeOmschrijving = line[11]; //Pick/Bulk
                        string Locatie = line[20]; //DXXXXXXXXX (locatie in mag)
                        string Omschrijving = line[15]; //welk product
                        string Soort = line[9]; //DROOG of VRIES of KOEL

                        if (Soort == "DROOG" && TypeOmschrijving != null && TypeOmschrijving != "Bulk" && Locatie != "retd" && Locatie != "" && Locatie != "RET.LEV.D")
                        {
                            //Console.Write(Soort + " " + TypeOmschrijving + " " + Omschrijving + " "  + Locatie + "\t");
                            string ParsedLocatie = LocatieParser(Locatie);
                            bool test1 = LocatieProduct.TryGetValue(ParsedLocatie, out string value2);
                            if (test1 == false)
                            { LocatieProduct.Add(ParsedLocatie, Omschrijving); }


                            bool test2 = PickCount.TryGetValue(Locatie, out int value);
                            if (test2 == false)
                            { PickCount.Add(Locatie, 1); }
                            else PickCount[Locatie]++;

                        }
                    }
                    foreach (var element in PickCount) //per schap
                    {

                        string trimmedResult = LocatieParser(element.Key);
                        //Console.Write('"' + trimmedResult + '"' + ", ");

                        bool test2 = PickCountSchap.TryGetValue(trimmedResult, out int value);
                        if (test2 == false)
                        { PickCountSchap.Add(trimmedResult, element.Value); }
                        else PickCountSchap[trimmedResult] += element.Value;
                    }
                    foreach (var element in PickCountSchap) //per schap
                    {
                        // Console.WriteLine(element.Key + " " + element.Value);
                        string result = "";
                        int i = 0;
                        char[] key = element.Key.ToCharArray();
                        foreach (char c in key)

                        {
                            if (i == 2)
                            { result = result + "."; }
                            result = result + c;
                            i++;
                        }
                        // Console.WriteLine(element.Value);
                    }
                    foreach (var element in LocatieProduct) //per schap
                    {
                        // Console.WriteLine(element.Key + " " + element.Value);
                    }
                    var Array = MagazijnCreator(PickCountSchap, LocatieProduct);


                    for (int y = 0; y < 70; y++)
                    {
                        Console.WriteLine();
                        for (int x = 0; x < 24; x++)
                        {
                            bool test2 = PickCountSchap.TryGetValue(Array[y, x].Locatie, out int value);
                            if (test2 == false)
                            { PickCountSchap.Add(Array[y, x].Locatie, 0); }
                            else if (Array[y, x].Locatie == "08.38")
                            {
                                bool Test3 = PickCountSchap.TryGetValue("17.1", out int test);
                                if (Test3)
                                    Array[y, x].PickCount = PickCountSchap["08.38"] + test;
                            }
                            else if (Array[y, x].Locatie == "08.40")
                            {
                                bool Test3 = PickCountSchap.TryGetValue("17.2", out int test);
                                if (Test3)
                                    Array[y, x].PickCount = PickCountSchap["08.40"] + test;
                            }
                            else if (Array[y, x].Locatie == "08.42")
                            {
                                bool Test3 = PickCountSchap.TryGetValue("17.3", out int test);
                                if (Test3)
                                    Array[y, x].PickCount = PickCountSchap["08.42"] + test;
                            }
                            else if (Array[y, x].Locatie == "08.44")
                            {
                                bool Test3 = PickCountSchap.TryGetValue("17.44", out int test);
                                if (Test3)
                                    Array[y, x].PickCount = PickCountSchap["08.44"] + test;
                            }
                            else if (Array[y, x].Locatie == "08.46")
                            {
                                bool Test3 = PickCountSchap.TryGetValue("17.5", out int test);
                                if (Test3)
                                    Array[y, x].PickCount = PickCountSchap["08.46"] + test;
                            }
                            else if (Array[y, x].Locatie == "08.43")
                            {
                                bool Test3 = PickCountSchap.TryGetValue("17.7", out int test);
                                if (Test3)
                                    Array[y, x].PickCount = PickCountSchap["08.43"] + test;
                            }
                            else if (Array[y, x].Locatie == "08.45")
                            {
                                bool Test3 = PickCountSchap.TryGetValue("17.6", out int test);
                                if (Test3)
                                    Array[y, x].PickCount = PickCountSchap["08.45"] + test;
                            }
                            else Array[y, x].PickCount = PickCountSchap[Array[y, x].Locatie];



                            string Count = (Array[y, x].PickCount + " ");
                            if (Count.Length < 5)
                            {
                                while (Count.Length < 4)
                                {
                                    Count += ' ';

                                }
                            }
                            Console.Write(Count);
                        }
                    }
                    int it = 1;
                    foreach (var obj in Array)
                    {
                        if (obj.Locatie != "PAD  ")
                        { textBoxes[it].Text = obj.Locatie; }
                    }
                    Console.ReadLine();
                }
            }
        }

Any help will be greatly appreciated!任何帮助将不胜感激!

In order to have the form alive and run your other code simultaneously you can use threads.为了使表单处于活动状态并同时运行其他代码,您可以使用线程。 add to your code using system.threading wrap the code you want to run behind your form in a new thread like so var t = new thread(()=>{ //CODE }) then start the new thread. using system.threading添加到您的代码中,将您想要在表单后面运行的代码包装在一个新线程中,例如var t = new thread(()=>{ //CODE })然后启动新线程。 t.start();

if you want to change something in the form while still in the other thread you will need to use the method invoker like so this.invoke((methodInvoker)(()=>{ this.background.color = colors.red; }))如果您想在另一个线程中更改表单中的某些内容,则需要使用方法调用程序,例如this.invoke((methodInvoker)(()=>{ this.background.color = colors.red; }))

PS: you may want to change your application to windows forms application and run every thing from a main winform. PS:您可能希望将您的应用程序更改为 windows forms 应用程序并从主 Winform 运行所有内容。 for more information check here有关更多信息,请在此处查看

暂无
暂无

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

相关问题 我正在使用WPF表单,但是当我添加新的WindowsForm表单时,我的程序将运行,但是Windows表单将不会显示 - I'm using WPF forms but when I add a new WindowsForm form, my program will run, but the windows form won't display 除非我断点代码,否则Entity Framework 6不会更新记录 - Entity Framework 6 won't update record unless I breakpoint my code 我的表单上有一个控件,我无法从代码中引用 - I have a control on my form I cant reference from code 当我尝试打开系统上安装的任何Web浏览器时打开我的应用程序登录表单的代码 - Code to open my application Login form when I try to open any Web Browser installed on my system 我想知道如何在后面的代码中访问模态表单元素。我也想知道如何在模态表单上应用jquery验证器 - I want to know that how can i access my modal form elements in my code behind .Also I want to know how can I apply jquery validator on modal form 为什么我无法关闭Windows窗体? - Why I can't close my windows form? 我的后台工作人员完成后,我想更改表单上的标签。 我应该在哪里添加此代码? - After my background worker is complete, I want to change labels on my form. Where should I add this code? 我需要一种在代码中使用 UI 输入的方法 - I need a way of using the input from my UI in my code 我希望我的代码能够显示 3 个选项,如果登录是由 adm1 进行的,并且如果登录是由任何其他用户进行的,我希望可以看到提款/存款 - i want my code to be able to display 3 options if the log in is by adm1 and if the log in is by any other user i want the withdraw/deposit to be seen 除非我使用断点,否则我的 selenium 代码中会出现 NoSuchElementException - I get a NoSuchElementException in my selenium code unless I use a breakpoint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM