C# 当我的主程序以C结尾时,如何运行程序或函数#

C# 当我的主程序以C结尾时,如何运行程序或函数#,c#,.net,C#,.net,我是windows开发新手,我需要它 在php中,我可以这样做: <?php exec("second.sh > /dev/null 2>&1") ?> 当我这样做时,php程序调用second.sh程序运行并退出,而不等待second.sh退出 我需要c#程序中的这种行为 我的第二个程序将运行5分钟并退出。 我需要发表一篇文章,但我不想等待请求完成以继续主程序,因为此请求可能需要5分钟才能完成 运行另一个程序是我在php上看到的一个“变通方法”。 理想

我是windows开发新手,我需要它

在php中,我可以这样做:

<?php

exec("second.sh > /dev/null 2>&1")

?>

当我这样做时,php程序调用second.sh程序运行并退出,而不等待second.sh退出

我需要c#程序中的这种行为

我的第二个程序将运行5分钟并退出。 我需要发表一篇文章,但我不想等待请求完成以继续主程序,因为此请求可能需要5分钟才能完成

运行另一个程序是我在php上看到的一个“变通方法”。 理想情况是调用HttpWebRequest,不要等待它完成。

“理想情况是调用HttpWebRequest,不要等待它完成。”

您可以执行TaskFactory.StartNew(()=>Something.HttpWebRequest(“url”)

“理想的做法是调用HttpWebRequest,不要等待它完成。”

您可以执行TaskFactory.StartNew(()=>Something.HttpWebRequest(“url”)

简短的回答 您可以启动另一个流程,如下所示:

using System.Diagnostics; // This goes in your usings at the top
...
Process.Start("process.exe");
摘自。程序需要在您的路径上,这样您就可以按名称运行它。否则,您需要指定其完整文件路径

然而 如果需要,您可以在一个程序中完成所有这些:

public void Main()
{
    //Your main program

    // [Send the POST]

    // Now start another thread to wait for the response while we do other stuff
    ThreadPool.QueueUserWorkItem(new WaitCallback(GetResponse));

    //Do other stuff
    //...
}

private void GetResponse(object state)
{
    // Check for evidence of POST response in here
}
我不知道您的第二个程序是如何检查POST响应的,但不管它是什么,您都可以在GetResponse中复制该逻辑。

简短答案 您可以启动另一个流程,如下所示:

using System.Diagnostics; // This goes in your usings at the top
...
Process.Start("process.exe");
摘自。程序需要在您的路径上,这样您就可以按名称运行它。否则,您需要指定其完整文件路径

然而 如果需要,您可以在一个程序中完成所有这些:

public void Main()
{
    //Your main program

    // [Send the POST]

    // Now start another thread to wait for the response while we do other stuff
    ThreadPool.QueueUserWorkItem(new WaitCallback(GetResponse));

    //Do other stuff
    //...
}

private void GetResponse(object state)
{
    // Check for evidence of POST response in here
}
我不知道您的第二个程序是如何检查POST响应的,但不管它是什么,您都可以在GetResponse中复制该逻辑。

谢谢大家 我最后说:

System.Threading.ThreadStart th_start = () =>
{
    slowFunction(arg);

};

System.Threading.Thread th = new System.Threading.Thread(th_start)
{
    IsBackground = true
};

th.Start();
出于某种原因,TaskFactory.StartNew没有运行我的slowFunction:

Task.Factory.StartNew(() => 
   new MyApp().slowFunction(arg), 
   System.Threading.CancellationToken.None,
   TaskCreationOptions.None,
   TaskScheduler.Default
);
谢谢大家 我最后说:

System.Threading.ThreadStart th_start = () =>
{
    slowFunction(arg);

};

System.Threading.Thread th = new System.Threading.Thread(th_start)
{
    IsBackground = true
};

th.Start();
出于某种原因,TaskFactory.StartNew没有运行我的slowFunction:

Task.Factory.StartNew(() => 
   new MyApp().slowFunction(arg), 
   System.Threading.CancellationToken.None,
   TaskCreationOptions.None,
   TaskScheduler.Default
);

调查你确定你需要一个新的计划吗?因为你所描述的问题也可以用简单的线程来解决。仔细看看你确定你需要一个新的程序吗?因为你所描述的问题也可以通过简单的线程来解决。我不想查看帖子的回复。我想发一个帖子,不要等待,我不想查看帖子的回复。我想发一个帖子,不要等待