Objects, threads and construction order

  • Thread starter Thread starter Jon Harrop
  • Start date Start date
J

Jon Harrop

I'm trying to define a Show3D class that takes a scene graph and spawns a
visualization rendering that scene graph in a new thread but keeps it
editable via properties in the Show3D class. For example, I'd like to be
able to alter the camera in the spawned visualization via a property in the
Show3D class.

I believe the Show3D constructor needs to create a new thread, set it to
STA, start the thread, create a new form from that thread and then create a
new message loop in that thread (Application.Run(form)).

Can the form's properties be adjusted from another thread or must I
communicate everything via events?

Is it possible to derive Show3D from Form or must Show3D wrap Form?

Does C# provide any support for having constructors spawn threads
implicitly?

Are there any scene graph libraries already out there with idiomatic C#
APIs?
 
I'm trying to define a Show3D class that takes a scene graph and spawns a
visualization rendering that scene graph in a new thread but keeps it
editable via properties in the Show3D class. For example, I'd like to be
able to alter the camera in the spawned visualization via a property in the
Show3D class.

I believe the Show3D constructor needs to create a new thread, set it to
STA, start the thread, create a new form from that thread and then create a
new message loop in that thread (Application.Run(form)).

I'd stop at "start the thread"; let the other thread do the rest.
Can the form's properties be adjusted from another thread or must I
communicate everything via events?

Only the thread that created the form should mess with the form. If
you know
the details in advance, then let it know at startup... Perhaps look at
ParamaterisedThreadStart, else you can use "captured" variables, or
properties
on a spawning class.

Otherwise yes you'll need some
kind of comms. This might just be exchanging the Form reference - from
which
the first thread can call BeginInvoke or Invoke - this could be
wrapped on the Show3D
wrapper.
Is it possible to derive Show3D from Form or must Show3D wrap Form?

In the scanario I'd wrap just to hide away a lot of unnecessary
details...
Does C# provide any support for having constructors spawn threads
implicitly?

No; but the explicit support is very good.
Are there any scene graph libraries already out there with idiomatic C#
APIs?

Managed DirectX? (I assumed at the top this was what you were talking
about)

Marc
 
Marc said:
I'd stop at "start the thread"; let the other thread do the rest.
Yes.


Only the thread that created the form should mess with the form. If
you know
the details in advance, then let it know at startup... Perhaps look at
ParamaterisedThreadStart, else you can use "captured" variables, or
properties
on a spawning class.
Ok.

Otherwise yes you'll need some
kind of comms. This might just be exchanging the Form reference - from
which
the first thread can call BeginInvoke or Invoke - this could be
wrapped on the Show3D
wrapper.

Brilliant. What would this look like in C#? Say I want to call Show() and
Hide() from another thread. How do I invoke them? Is it easier to use
invoke Visible=true from another thread somehow?
In the scanario I'd wrap just to hide away a lot of unnecessary
details...

That's what I thought. But does this require me to reimplement lots of form
functionality in the Show3D object (e.g. a Visible property) unnecessarily?
No; but the explicit support is very good.

Ok, I've got that pinned down already.
Managed DirectX? (I assumed at the top this was what you were talking
about)

MDX, yes.
 
Brilliant. What would this look like in C#? Say I want to call Show() and
Hide() from another thread. How do I invoke them? Is it easier to use
invoke Visible=true from another thread somehow?

It all hinges on having a reference to the Form (that is owned by the
other thread).
For simple methods (void, no params), you can use e.g.

someForm.Invoke(new MethodInvoker(someForm.Show));

For more complex examples (properties, non-trivial methods etc) the
easiest route is an anonymous method in 2.0:

// here we are on the non-UI thread
// note it is a good idea to get all data
// into primatives first to avoid threading nuances
string text = GetSomeText();

someForm.Invoke((MethodInvoker) delegate {
// *this* bit of code runs on the Form's thread
someForm.Text = text; // the "text" variable is "captured" into this
method
});

If you don't want the outer thread to wait, then use BeginInvoke in
the above.

Marc
 
Marc said:
For more complex examples (properties, non-trivial methods etc) the
easiest route is an anonymous method in 2.0...

Awesome. Works like a treat. Thanks very much!
 
Back
Top