MAS's picture

Updaing openTK Control

Hi everyone I hope someone can help me.

I have a command design pattern which updates my model this in turn updates my model objects.

My TKControl is on my main thread with a standard render loop approach.

To keep the ui responsive my picker class to select objects in the control is threaded. The command then has a callback which then calls the model to do something eg add a new object.

The threaded picker works great.

The added model object can render itself ie contains OpenGL commands but when I call the model to add the object it crash the app. (I believe because I am trying to update the view (Control)

from outside of the main thread. my question is there a way I can restructure my design to make this work?

I know I cannot call GL from off the main thread. do I need to employ some locks in my rendering loop or is that not going to help?

    [Serializable]
    public class AddObjectCommand : ViewUICommand, ICommand
    {
        DrawableShape _Shape1;
        List<Guid> _ShapeOffsetsGU;
        public AddObjectCommand ()
        {
        }
 
        void ICommand.Execute()
        {
            Environment.Instance.Picker.pickerfinished += new PickingCompletedCallBack(Picker_pickerfinished);
            Environment.Instance.Picker.StartSelecting(1);
        }
 
        void Picker_pickerfinished()
        {
            Environment.Instance.Picker.pickerfinished -= new PickingCompletedCallBack(Picker_pickerfinished);
            _Shape1 = Environment.Instance.Picker._SelectedShapes[0];
              Environment.Instance.Model.AddObject(new Polyline(new Vector3f(0f, 0f, 0f), Color.Cyan, true)); **** crashes when it hits the rendering code in the polyline object****
        }
 
        void ICommand.UnExecute()
        {
        }
    }

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

You can use Control.BeginInvoke to execute code on the UI thread. Try using that to call Model.AddObject.

MAS's picture

Thanks for your suggestion fiddler I shall try that out