在后台运行进程

本文关键字:进程 运行 后台 | 更新日期: 2023-09-27 18:26:45

我需要读取一个.txt文件,其中包含我在其他程序中需要的一些变量。我解释得更好:我有一个程序,当它在.txt上读取一些值时,会进行捕获,例如"1"、"11"、"111"。。。当我试图拍摄一张或多张图像时,我遇到的问题就出现了。它保存了正确数量的捕获,但所有捕获都具有相同的信息。我把我的代码放在下面:

bool lecturaOK = false;
int time = 0;
while (!lecturaOK)
{
    try
    {
        string text = System.IO.File.ReadAllText(@"prueba.txt");
        if (text == "1" && time == 0)
        {
            time++;
            using (BinaryWriter writer = new BinaryWriter(File.Open(@"kinect" + counter + @".stl", FileMode.Create)))
            {
                Helper.SaveBinaryStlMesh(mesh, writer, true, counter, posicionamiento);
            }
            counter++;
            mesh = this.volume.CalculateMesh(1);
        }
        else if (text == "11" && time == 1)
        {
            time++;
            using (BinaryWriter writer = new BinaryWriter(File.Open(@"kinect" + counter + @".stl", FileMode.Create)))
            {
                Helper.SaveBinaryStlMesh(mesh, writer, true, counter, posicionamiento);
            }
            counter++;
            mesh = this.volume.CalculateMesh(1);
        }
        else if (text == "111" && time == 2)
        {
            time++;
            using (BinaryWriter writer = new BinaryWriter(File.Open(@"kinect" + counter + @".stl", FileMode.Create)))
            {
                Helper.SaveBinaryStlMesh(mesh, writer, true, counter, posicionamiento);
            }
            counter++;
            mesh = this.volume.CalculateMesh(1);
        }
        else if (text == "1111" && time == 3)
        {
            time++;
            using (BinaryWriter writer = new BinaryWriter(File.Open(@"kinect" + counter + @".stl", FileMode.Create)))
            {
                Helper.SaveBinaryStlMesh(mesh, writer, true, counter, posicionamiento);
            }
            counter++;
            mesh = this.volume.CalculateMesh(1);
        }
        else if (text == "11111" && time == 4)
        {
            time++;
            using (BinaryWriter writer = new BinaryWriter(File.Open(@"kinect" + counter + @".stl", FileMode.Create)))
            {
                Helper.SaveBinaryStlMesh(mesh, writer, true, counter, posicionamiento);
            }
            counter++;
            lecturaOK = true;
        }
    }
    catch (Exception ex)
    {
        continue;
    }
}

我重新启动了网格值,但我的程序在等待下一个txt值时陷入了困境,我的意思是,它只进行while循环,而没有执行"mesh=this.volume.CalculateMesh(1)"指令。我需要在不停止程序其余部分的情况下等待txt的值。

还有别的办法吗?

在后台运行进程

  1. 如果您正在观察一个不连续更新的文件,我建议您使用System.IO.FileSystemWatcher类。

  2. 关于您的评论:

"我的程序在等待下一个txt值时被卡住了,我的意思是,它只进行while循环,而没有使网格=this.volume.CalculateMesh(1)"

你有没有检查过你是否最终没有抛出异常并降落在你的catch块中?

也许您可以使用Timer,因为while循环非常快且不可控。如果您使用Timer,您将在Timer_tick事件中控制您的代码块。您可以启动和停止代码流,并进行简单的代码调试。