Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC子进程返回值_C#_Asp.net Mvc_Subprocess - Fatal编程技术网

C# ASP.NET MVC子进程返回值

C# ASP.NET MVC子进程返回值,c#,asp.net-mvc,subprocess,C#,Asp.net Mvc,Subprocess,我写了这样的代码 public void GitConnectAndFetch() { try { ProcessStartInfo startInfo = new ProcessStartInfo("git.exe"); startInfo.UseShellExecute = false; startInfo.WorkingDirectory = projectPath;

我写了这样的代码

public void GitConnectAndFetch()
{   
    try
    {
        ProcessStartInfo startInfo = new ProcessStartInfo("git.exe");
        startInfo.UseShellExecute = false;                
        startInfo.WorkingDirectory = projectPath;                
        startInfo.RedirectStandardInput = true;
        startInfo.RedirectStandardOutput = true;
        startInfo.Arguments = "branch -a";
        Process process = new Process();
        process.StartInfo = startInfo;
        process.Start();
        string branchname = "";
        List<SelectListItem> ddgit = new List<SelectListItem>();
        while (process.StandardOutput.Peek() >= 0)
        {
            branchname = process.StandardOutput.ReadLine().Trim();
            if (branchname.Trim().StartsWith("remotes/origin/"))
            {                       
                ddgit.Add(new SelectListItem { Text = branchname.ToString().Replace("remotes/origin/",""), Value = branchname.ToString() });
            }
        }
        if (ddgit.Count == 0) ddgit.Add(new SelectListItem() { Text = "", Value = "" });
        process.WaitForExit();
        ViewBag.Branchlist = ddgit;
    }
    catch (Exception ex)
    {
        localLog(ex.Message);
        localLog(ex.StackTrace);
    }
}

我在发帖后又打了一次电话

[HttpPost]
public ActionResult Otomasyon(FormCollection form)
{
    GitConnectAndFetch();                    
    return View();
}


我认为子进程运行缓慢,我正在调用它两次以填充主页上的下拉列表,如何使它运行一次以关闭。

如果您不期望不同的值,那么我只缓存ddgit的值,如果该缓存存在,则返回它而不是执行该进程。

首先,方法GitConnectAndFetch有副作用(将数据设置到ViewBag中),这是不好的。将其转换为纯函数,该函数返回SomeBranchItem[]


其次,缓存GitConnectAndFetch的结果。您可以通过将方法的结果设置为一个私有静态字段A,将当前时间戳设置为第二个静态字段B来实现它。然后,在每个请求中,您都应该比较当前时间戳和字段B中的时间戳,以确定是否可以使用结果中的缓存数据。

在关闭之前都是相同的数据,所以缓存是合理的,但如何使用呢如果数据量不大,我可以看看MemoryCache->。否则,可以看看Redis之类的缓存->。如果您计划与多个进程共享此缓存,那么使用Redis可能会更幸运。是的,MemoryCache可能是最容易实现的。
[HttpPost]
public ActionResult Otomasyon(FormCollection form)
{
    GitConnectAndFetch();                    
    return View();
}