提问者:小点点

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


我有两个独立的文件,格式1。cs和程序。反恐精英。我想要一个节目。cs在我选择表单中的文件路径后开始运行,方法是按下按钮1并选择文件目标,但它仅在我关闭表单后开始运行。我无法运行我的程序。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;

            }
        }
    }
}




这是我的主要课程。反恐精英:


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();
                }
            }
        }

任何帮助将不胜感激!


共1个答案

匿名用户

为了使窗体处于活动状态并同时运行其他代码,可以使用线程。使用系统添加到您的代码。threading将要在窗体后面运行的代码包装在一个新线程中,如sovar t=new thread()=

如果您想在表单中更改某些内容,而仍然在另一个线程中,您将需要使用方法调用器,如this.invoke((method Invoker)(()=

PS:您可能希望将应用程序更改为windows窗体应用程序,并从主winform运行所有内容。欲了解更多信息,请点击此处