简体   繁体   English

C#:尝试将控制台程序转移到GUI时出现问题

[英]C#: Problems while trying to transfer a Console Program into a GUI

Im new to C#! 我是C#的新手! So my Problem: I got a C# Demo Program from an extern company, the program sets the settings of an extern device, than initialize it and starts recording the Data. 所以我的问题是:我从extern公司获得了一个C#演示程序,该程序设置extern设备的设置,而不是对其进行初始化并开始记录数据。 The Data is written into the Console and streamed into a CSV at a Location i can choose. 数据被写入控制台,并在我可以选择的位置流式传输为CSV。 After pressing a Key in the Console the Recording stops. 在控制台中按一个键后,录制停止。 I am trying now to write a short GUI that starts and stops this Recording. 我现在正在尝试编写一个简短的GUI,以启动和停止此记录。 What I did: I converted the main-method, which started the recoding, into a normal static method and deleted all writelines so the console doesnt start, than I created a Windows Form with 2 Buttons, one Start and One Stop. 我做了什么:我将开始重新编码的主方法转换为常规的静态方法,并删除了所有写行,因此控制台无法启动,这比我创建了一个带有2个按钮,一个“开始”和“一站式”的Windows窗体。 If I press Start, the Recording starts, but I cant do anything with the Gui it is like freeezed, a Console still appeares and i can only stop the process by pressing a key in the console. 如果我按开始,则录制开始,但是我无法像使用FreeGUI一样进行任何操作,控制台仍然出现,并且我只能通过按控制台中的键来停止该过程。

Here is a snippit of the Console Code: 这是控制台代码的一个片段:

class BHJKL
{
    system
    private static A;


    private static B;


    // This method will be executed every time new data is received
    static void OnNewDataReceived(object sender, NewDataEventArgs eventArgs)
    {
         //some code in here


         using (StreamWriter file = new StreamWriter(@"C:\Users\XYZ\File_Trial" + DateTime.Now.DayOfYear + ".csv", true))
         {
             //Writeline data..
         }

     }

}

// This was the Main Method before i changed it //这是我更改它之前的主要方法

    public static void Record()
    {



        // Here is some Code that configure the Settings of the device




        // initialize device
        // MORE CODE USING THE COMPANYS EXTERN LIBRARYS TO START THE DEVICE

        //start device
        deviceManager.DeviceStart();


        while (!Console.KeyAvailable) // as long as no key is pressed do...
        {
            Thread.Sleep(100); // do nothing, let the event processing thread that is managed by the library handle incomming data
        }

        //stop device
        deviceManager.DeviceStop();

        //unsubscribe event
        deviceManager.NewDataReceived -= OnNewDataReceived;

        //deinitialize device
        deviceManager.DeinitializeDevice();

        //dispose device
        deviceManager.Dispose();




    }
}

My Attempt: Changing the main method into static Recording. 我的尝试:将主要方法更改为静态录制。 Than by Pressing the Start Button, call the method. 比按开始按钮,调用方法。

Write this at the end of the method: 在方法末尾写下:

while (Rec==true) 
{
    Thread.Sleep(100); 
}

By Pressing the Stop Button: set.Rec(false) But after Pressing the start Button, i cant press the stop button anyome. 通过按下停止按钮:set.Rec(false)但是在按下开始按钮之后,我无法再按下停止按钮。 I hope someone can understand my Problem and can give me some advices. 我希望有人能理解我的问题并能给我一些建议。

Thread.Sleep blocks your UI from doing anything. Thread.Sleep阻止您的UI执行任何操作。 Consider using a System.Windows.Forms.Timer 考虑使用System.Windows.Forms.Timer

Rather than sleeping in a loop, you should just let your Start method return. 不要让自己陷入循环,而应该让Start方法返回。 Move all of the code that comes after the sleep loop into your Stop button's click event handler. 将休眠循环之后的所有代码移到“ Stop按钮的click事件处理程序中。

(Depending on how the code is structured, you may also have to switch deviceManager from being a local variable to being a field in the class) (根据代码的结构,您可能还必须将deviceManager从本地变量切换为类中的字段)

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

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