在后台运行流程

问题描述 投票:0回答:2

我需要读一个.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的值而不停止程序的其余部分。

还有其他办法吗?

c# multithreading process background
2个回答
1
投票
  1. 如果您正在观看不连续更新的文件,我建议您使用System.IO.FileSystemWatcher类。
  2. 关于你的评论:

“我的程序在等待下一个txt值时卡住了,我的意思是,它只是制作了while循环并且它没有使得mesh = this.volume.CalculateMesh(1)”

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


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.