2

I'm developing a solution where I have a Windows Form, where I've placed a pictureBox. I want to move it through the screen depending on the value of pressure center I have already calculated.

I instantiated a class called pressureCenterManager that makes all the functionalities.

So, in my form code I have the following:

pressureCenterManager.displayPressureCenter(pressureMatrix, this.plot, this.pbox_CoG);

I get a correct value in the var pressureCenter.

This is my code in the displayPressureCenter function:

public void displayPressureCenter(double[,] pressureMatrix, Plot plot, PictureBox pictureBox)
        {
            //Get matrix size
            int xSize = pressureMatrix.GetLength(0);
            int ySize = pressureMatrix.GetLength(1);
            try
            {
                //Get CoP and move pictureBox
                System.Windows.Point centerOfPressure = getCenterOfPressure(pressureMatrix);
                pictureBox.Visible = true;
                pictureBox.Parent = plot.plotView;
                //Calculamos el punto dónde hay que printar utilizando una regla de 3 y descontando la mitad del tamaño de la señal (para que quede centrada)
                System.Drawing.Point displayPositionCart = new System.Drawing.Point((int)Math.Round((centerOfPressure.X * plot.plotView.Width / xSize) - (pictureBox.Width / 2)), (int)Math.Round((centerOfPressure.Y * plot.plotView.Height / ySize) - (pictureBox.Height / 2)));
                //Pasamos a coordenadas de pantalla y aplicamos un offset para quitar el eje
                System.Drawing.Point displayPositionScre = CartesianToScreenCoordinates(displayPositionCart, plot.plotView);
                displayPositionScre.Offset(0, -70);
                pictureBox.Location = displayPositionScre;
            }
            catch
            {

            }

I don't know why, when executing pictureBox.Visible = true; it jumps to the catch section.

Can you please help me?

Many thanks!

Victor P.
  • 97
  • 10
  • 2
    What error is being thrown by you going into the catch? add catch(Exception e) and read what e is – Fuzzybear Jul 11 '18 at 09:47
  • Cross-thread operation not valid: Control 'pnl_grafica' accessed from a thread other than the thread it was created on. – Victor P. Jul 11 '18 at 10:08
  • https://stackoverflow.com/questions/10775367/cross-thread-operation-not-valid-control-textbox1-accessed-from-a-thread-othe The data received in your displayPressureCenter method is coming from another thread context than the UI thread, and that's the reason you see this error. To remedy this, you will have to use a dispatcher as described in the MSDN article: http://msdn.microsoft.com/en-us/library/ms171728.aspx – Fuzzybear Jul 11 '18 at 10:17

1 Answers1

0

Some recommendations:

  1. Throw your exceptions back. New programmers think this is bad to see exceptions. Not doing so just causes confusion. Either rethrow it or use it to log and send back to the caller. You need to know an error occurred.
  2. Windows Forms calls have an InvokeRequired property and Invoke method to handle when you have to do GUI operations and aren't on the main thread. Use them. Here's some changes.

Code

 public void displayPressureCenter(double[,] pressureMatrix, Plot plot, PictureBox pictureBox)
 {

      if ( pictureBox.InvokeRequired )
      {
          this.Invoke(delegate { displayPressureCenter(pressureMatrix, plot, pictureBox)});
          exit;
      }

            //Get matrix size
            int xSize = pressureMatrix.GetLength(0);
            int ySize = pressureMatrix.GetLength(1);
            try
            {
                //Get CoP and move pictureBox
                System.Windows.Point centerOfPressure = getCenterOfPressure(pressureMatrix);
                pictureBox.Visible = true;
                pictureBox.Parent = plot.plotView;
                //Calculamos el punto dónde hay que printar utilizando una regla de 3 y descontando la mitad del tamaño de la señal (para que quede centrada)
                System.Drawing.Point displayPositionCart = new System.Drawing.Point((int)Math.Round((centerOfPressure.X * plot.plotView.Width / xSize) - (pictureBox.Width / 2)), (int)Math.Round((centerOfPressure.Y * plot.plotView.Height / ySize) - (pictureBox.Height / 2)));
                //Pasamos a coordenadas de pantalla y aplicamos un offset para quitar el eje
                System.Drawing.Point displayPositionScre = CartesianToScreenCoordinates(displayPositionCart, plot.plotView);
                displayPositionScre.Offset(0, -70);
                pictureBox.Location = displayPositionScre;
            }
            catch (Exception e)
            {
                 throw e;
            }

  // Rest of your code

References: https://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/zyzhdc6b(v=vs.110).aspx

Ctznkane525
  • 7,297
  • 3
  • 16
  • 40