简体   繁体   English

如何暂停代码直到我想要? [类似于Thread.Sleep(int);]

[英]How to pause the code until I want? [something like Thread.Sleep(int);]

I think the best way to make my point is to give you these examples: 我认为,表达我观点的最好方法是举几个例子:

Console.ReadLine(); 
Process.WaitForExit();

These functions will pause the application (without freezing the UI). 这些功能将暂停应用程序(不冻结UI)。

I need something like this. 我需要这样的东西。

In fact, I am writing something like ReadLine() but in windows forms applications . 实际上,我正在windows forms applications编写类似ReadLine()
When you call this method, it should be waited until the user press Enter . 调用此方法时,应等待它,直到用户按Enter为止。

您可以创建一个等待AutoResetEvent的线程,UI线程应继续并接受表示该事件的输入。

You can use backGroundWorker which simply checks for cancellation and waits some time using Thread.Sleep(100) for example. 您可以使用backGroundWorker ,它仅检查取消情况并使用Thread.Sleep(100)等一些时间。

The other way is to create own thread and control all checks and callbacks yourself. 另一种方法是创建自己的线程并自己控制所有检查和回调。

Console apps don't have a UI so there's nothing for WaitForExit() to freeze. 控制台应用程序没有UI,因此WaitForExit()不会冻结。 In a console app the chrome (title bar and window buttons) are handled by the system. 在控制台应用程序中,镶边(标题栏和窗口按钮)由系统处理。 In a WinForms app those things aren't painted until you pass the NC_PAINT events to the default handler. 在WinForms应用程序中,直到将NC_PAINT事件传递给默认处理程序后,这些内容才会被绘制。 If your main message loop is sleeping then your chrome doesn't get painted. 如果您的主消息循环正在休眠,则您的镶边不会被涂漆。

A WinForms app doesn't close automatically so there should be no reason to call something like WaitForExit(). WinForms应用程序不会自动关闭,因此没有理由调用WaitForExit()之类的东西。 Instead you explicitly tell the app when to close - say when the user presses escape or something. 取而代之的是,您明​​确告知应用程序何时关闭-例如用户何时按Escape键。

Try this 尝试这个

Task logManager = Task.Factory.StartNew(() => {   /* Your code */ }, TaskCreationOptions.LongRunning);

or even 甚至

Task screenshotManager; 

     private void StartScreenshotManager()
            {
                screenshotManager = Task.Factory.StartNew(() => { ScreenshotManagerJob(); }, TaskCreationOptions.LongRunning);
                screenshotManager.ContinueWith((t) => { ScreenshotManagerUpdateUI(); }, TaskScheduler.FromCurrentSynchronizationContext());
            }

            private void ScreenshotManagerUpdateUI()
            {
                // ... UI update work here ... 
            }

            private void ScreenshotManagerJob()
            {
             // your code
             }

Most likely, you don't need to halt the execution at all in Windows Forms application. 最有可能的是,您根本不需要在Windows Forms应用程序中停止执行。

Windows Forms applications usually require a much more consistent architecture. Windows Forms应用程序通常需要更加一致的体系结构。 If you need some code to proceed after the user input, you should not be halting a function execution. 如果在用户输入后需要继续执行一些代码,则不应停止函数执行。 In contrast, open a dialog (or provide any other mean for the user to do the required input) and just wait for him to "interact with it". 相反,打开一个对话框(或为用户提供所需的输入内容提供任何其他方式),然后等待他“与之交互”。 Interaction should raise C# event s that will be processed by an logical class instance that knows how to do it and is subscribed to them. 交互应该引发C# event ,该event将由知道如何进行操作并已订阅它们的逻辑类实例处理。

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

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