0

I need to read a .txt that contains some variables that I need in other program. I explain it better: I have a program that makes a capture when it reads on a .txt some values, for example "1", "11", "111"... The problema that I have appears when I try to capture one or more images. It saves the correct number of captures, but all of them have the same information. I put my code below:

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;
    }
}

I restart the mesh value, but my program get stuck while it waits for the next txt values, I mean, it is only making the while loop and it doesn't make the "mesh = this.volume.CalculateMesh(1)" instruction. I need to wait for the values of the txt without stopping the rest of the program.

Is there any other way to do that?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Imrik
  • 674
  • 2
  • 14
  • 32
  • Which version of C# do you use? Will your text only be a series of ones? 1, 11, 111, ... 1...1? How often is the Textfile modified? – Timothée Bourguignon Dec 14 '15 at 10:59
  • I don't know version of C#, how can I know it? My .txt is 1 on the frist time, then 11, then 111... It is modified whenever I want, for example, each 10 seconds. – Imrik Dec 14 '15 at 11:03
  • I try to do it without the .txt file, only with a Thread.sleep(10000) but it stucks all the program during 10 seconds, with the same problem, the mesh doesn't restart with the new values. – Imrik Dec 14 '15 at 11:06

2 Answers2

1
  1. If you are watching a file that doesn't update continuously, I would recommend using the System.IO.FileSystemWatcher class.

  2. About your comment:

"my program get stuck while it waits for the next txt values, I mean, it is only making the while loop and it doesn't make the mesh = this.volume.CalculateMesh(1)"

Have you checked if you don't end up throwing an exception and landing in your catch block?

Community
  • 1
  • 1
Timothée Bourguignon
  • 2,190
  • 3
  • 23
  • 39
  • I checked now, I have this error, any idea of what could be? ((System.SystemException)(ex)) {"This API has returned an exception from an HRESULT: 0x80004005"} – Imrik Dec 14 '15 at 11:23
  • What is the exact line that throws the exception? Did you check in debug mode the information in the exception? – Timothée Bourguignon Dec 14 '15 at 12:16
  • Here: var vertices = mesh.GetVertices(); it has null vertices, but I don't know why. I use the same code that it is used all the times it wants to do a mesh capture, any idea? – Imrik Dec 14 '15 at 12:35
  • Sorry but from there I'm only guessing, I miss a lot of information about your code. I assume you are using the `Microsoft.Kinect.Toolkit.Fusion` toolkit. There the [GetVertices()](https://msdn.microsoft.com/en-us/library/microsoft.kinect.toolkit.fusion.mesh.getvertices.aspx?f=255&MSPPError=-2147217396) says it can throw an `InvalidOperationException` but nothing more. Is this the exception that is raised? What about your mesh, is it correctly filled up? – Timothée Bourguignon Dec 14 '15 at 12:42
  • the error happens here: if (null == vertices) { IntPtr ptr = IntPtr.Zero; ExceptionHelper.ThrowIfFailed(mesh.GetVertices(out ptr)); vertices = new ReadOnlyCollection(new NativeArray(ptr, (int)mesh.VertexCount())); } because "vertices" is null, but i don't know why, I use the same methods that the sdk use to obtain a Mesh, and also I have the first mesh correctly but then it cames with that error all the time, can you help me, please? thanks! :) – Imrik Dec 14 '15 at 15:04
  • This is the code that throws the error when `vertices == null`, yes. But that doesn't help us further. How/where is your `vertices` variable filled? Debug that process to see where it fails. – Timothée Bourguignon Dec 14 '15 at 15:43
  • it doesn't fail. I mean, I debug it and I see the same process for the first mesh as on the second one. Both meshes have all the properties to null, except mesh property. For example, vertices are null, points are null, color are null, but one mesh is saved and the second one no. Any idea? Thanks a lot for all your help! – Imrik Dec 14 '15 at 16:05
0

Maybe you can use Timer because while loop is very fast and uncontrollable. If you use Timer, you will control your code block in timer_tick event. You can start and stop code flow and make an easy code debug.