如何自动启动和停止流入过程?

发布于 2025-01-24 01:13:21 字数 345 浏览 4 评论 0 原文

我最近使用

本教程。我得到了工作的应用程序,并更改了程序以满足我的需求。我知道您可以通过在CMD中键入“ dotnet run”来启动该过程,并通过在CMD中按CTRL + C来阻止它。

但是,如何通过C#代码自动启动和终止该过程呢?

我希望在另一个程序中调用时运行的过程,然后在24小时后终止。

提前致谢。

I've recently set up my first influxdb using

https://www.influxdata.com/blog/getting-started-with-c-and-influxdb/

This tutorial. I got the application to work and changed the program to fit my needs. I'm aware that you can start the process by typing "dotnet run" in CMD and stop it by pressing ctrl + c in CMD.

But how could you automatically start and terminate the process via c# code?

I'd like the process to run when called by another Programm and then be terminated after 24 hours.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沙沙粒小 2025-01-31 01:13:21

您的问题应减少到如何开始和安排关闭过程。

“ nofollow noreferrer”>计时器类
可以使用。

在我的演示中, open(button)将在 path 中打开 .txt 文件。

关闭(按钮)将开始5S倒计时,以关闭 Notepad 程序。

您可以根据需要更改它。

.NET 4.8框架Winform在此处使用。

当然,您还可以使用控制台应用程序来完成此操作。

以下是我的代码:

using System;
using System.Diagnostics;
using System.IO;
using System.Timers;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private static System.Timers.Timer aTimer;

        private void button1_Click(object sender, EventArgs e)
        {
            StartApp(@"C:\Users\Administrator\Desktop");
        }
        /// <summary>
        /// Start the process
        /// </summary>
        /// <param name="ArrayFolderPath">Need to open the path of the process folder, multiple paths are separated by ','; eg:d:\test,e:\temp</param>
        private void StartApp(string ArrayFolderPath)
        {
            string[] foldersNamePath = ArrayFolderPath.Split(',');
            foreach (string folderNamePath in foldersNamePath)
            {
                GetFolderApp(folderNamePath);
            }
        }

        /// <summary>
        /// Recursively traverse all exe files in the folder, this method can be further extended to other suffix files
        /// </summary>
        /// <param name="folderNamePath">Folder path</param>
        private void GetFolderApp(string folderNamePath)
        {
            string[] foldersPath = Directory.GetDirectories(folderNamePath);
            foreach (string folderPath in foldersPath)
            {
                GetFolderApp(folderPath);
            }

            string[] filesPath = Directory.GetFiles(folderNamePath);
            foreach (string filePath in filesPath)
            {
                FileInfo fileInfo = new FileInfo(filePath);

                //Open the file with the suffix .txt
                if (fileInfo.Extension.Equals(".txt"))
                {
                    Process.Start(filePath);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SetTimer();
        }
        private static void SetTimer()
        {
            // Create a timer with a two second interval.
            aTimer = new System.Timers.Timer(5000);
            // Hook up the Elapsed event for the timer. 
            aTimer.Elapsed += OnTimedEvent;
            aTimer.AutoReset = true;
            aTimer.Enabled = true;
        }

        private static void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            CloseApp("Notepad");
        }

        /// <summary>
        /// Close the application
        /// </summary>
        /// <param name="ArrayProcessName">The application names are separated by ','</param>
        private static void CloseApp(string ArrayProcessName)
        {
            string[] processName = ArrayProcessName.Split(',');
            foreach (string appName in processName)
            {
                Process[] localByNameApp = Process.GetProcessesByName(appName);//Get all processes with program name
                if (localByNameApp.Length > 0)
                {
                    foreach (var app in localByNameApp)
                    {
                        if (!app.HasExited)
                        {
                            app.Kill();//close the process
                        }
                    }
                }
            }
        }
    }
}

输出:

”在此处输入图像描述”

Your question should be reduced to how to start and schedule a process to shut down.

Timer class and
ProcessStartInfo.CreateNoWindow class can be used.

In my demo, open(button) will open the .txt file in the path.

close(button) will start a 5s countdown to close the notepad program.

You can change it according to your needs.

The .net 4.8 framework winform is used here.

Of course, you can also use the console application to complete this.

Below is my code:

using System;
using System.Diagnostics;
using System.IO;
using System.Timers;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private static System.Timers.Timer aTimer;

        private void button1_Click(object sender, EventArgs e)
        {
            StartApp(@"C:\Users\Administrator\Desktop");
        }
        /// <summary>
        /// Start the process
        /// </summary>
        /// <param name="ArrayFolderPath">Need to open the path of the process folder, multiple paths are separated by ','; eg:d:\test,e:\temp</param>
        private void StartApp(string ArrayFolderPath)
        {
            string[] foldersNamePath = ArrayFolderPath.Split(',');
            foreach (string folderNamePath in foldersNamePath)
            {
                GetFolderApp(folderNamePath);
            }
        }

        /// <summary>
        /// Recursively traverse all exe files in the folder, this method can be further extended to other suffix files
        /// </summary>
        /// <param name="folderNamePath">Folder path</param>
        private void GetFolderApp(string folderNamePath)
        {
            string[] foldersPath = Directory.GetDirectories(folderNamePath);
            foreach (string folderPath in foldersPath)
            {
                GetFolderApp(folderPath);
            }

            string[] filesPath = Directory.GetFiles(folderNamePath);
            foreach (string filePath in filesPath)
            {
                FileInfo fileInfo = new FileInfo(filePath);

                //Open the file with the suffix .txt
                if (fileInfo.Extension.Equals(".txt"))
                {
                    Process.Start(filePath);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SetTimer();
        }
        private static void SetTimer()
        {
            // Create a timer with a two second interval.
            aTimer = new System.Timers.Timer(5000);
            // Hook up the Elapsed event for the timer. 
            aTimer.Elapsed += OnTimedEvent;
            aTimer.AutoReset = true;
            aTimer.Enabled = true;
        }

        private static void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            CloseApp("Notepad");
        }

        /// <summary>
        /// Close the application
        /// </summary>
        /// <param name="ArrayProcessName">The application names are separated by ','</param>
        private static void CloseApp(string ArrayProcessName)
        {
            string[] processName = ArrayProcessName.Split(',');
            foreach (string appName in processName)
            {
                Process[] localByNameApp = Process.GetProcessesByName(appName);//Get all processes with program name
                if (localByNameApp.Length > 0)
                {
                    foreach (var app in localByNameApp)
                    {
                        if (!app.HasExited)
                        {
                            app.Kill();//close the process
                        }
                    }
                }
            }
        }
    }
}

Output:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文