Displaying a progress bar on the OnInspectorGUI function

Hi people, I'm trying to display a progress bar when I calculate some things in the Inspector but it appears this message:

ArgumentException: You can only call GUI functions from inside OnGUI.
UnityEngine.GUIUtility.CheckOnGUI () 
UnityEngine.GUILayoutUtility.DoGetRect (UnityEngine.GUIContent content, UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options) [0x00000]
UnityEngine.GUILayoutUtility.GetRect (UnityEngine.GUIContent content, UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options) [0x00000]
UnityEditor.InspectorWindow.OnGUI () 
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000]

The main code of the example is the follow, this is the editor of another class ViewArea:

using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(ViewArea))]
public class ViewAreaEditor : Editor{

    public override void OnInspectorGUI()
    {
        ViewArea va;
        va = target as ViewArea;

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.PrefixLabel("Cell Division");
        va.cellDivision = EditorGUILayout.Slider(va.cellDivision, 0.01f, 1f);
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.PrefixLabel("Ray Division");
        va.rayDivision = EditorGUILayout.Slider(va.rayDivision, 0.01f, 1f);
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Number Of Rays", va.numberOfRays.ToString());
        EditorGUILayout.EndHorizontal();

        if (GUILayout.Button("Calculate PVS"))
        {
            EditorUtility.DisplayProgressBar("Calculating the PVS", "Calculating - 0%", 0.0f);
            va.ThrowRays();
            EditorUtility.DisplayProgressBar("Calculating the PVS", "Calculating - 50%", 0.5f);
            va.CalculateTransformsPerCell();
            EditorUtility.DisplayProgressBar("Calculating the PVS", "Calculating - 100%", 1.0f);
            EditorUtility.ClearProgressBar();
        }
        if (GUI.changed)
        {
            EditorUtility.SetDirty(va);
        }

    }
}

There is now EditorGUI.ProgressBar you can use it together with EditorGUILayout.GetControlRect in order to embed it correctly into the Inspector without having to open Dialogues for them:

var rect = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight);
EditorGUI.ProgressBar(rect, progress, "");

I just saw this in the docs.

If you want to put 2D GUI objects (GUI, EditorGUI and friends), you need to wrap them in calls to Handles.BeginGUI() and Handles.EndGUI().

I'm guessing the problem is with the following line, since you're calling GUILayout instead of EditorGUILayout but don't have it surrounded by Handles.BeginGUI()/EndGUI().

if (GUILayout.Button("Calculate PVS"))