xaria
xaria

Reputation: 842

Can a thread be accessed from two forms in a Windows forms application

I wish to run a thread to update the Image in the picturebox . The image is streamed form the camera. I require to stream images to two pictureboxes in two different forms, but one at a time. Is it possible to create a single thread which can be accessed by both forms.

I think a backgroundworker would be appropriate. But how do I update the images in the picturebox of the respective forms?

I am using VC++ CLI/CLR

Upvotes: 1

Views: 182

Answers (2)

Eli Waxman
Eli Waxman

Reputation: 2897

What I think you wanna do is this: when creating the new form, send to the constructer the first form as an object , then , and make a setter/ getter or just make the thread public, then you can "access" it from both forms as you requested.

Upvotes: 0

trickdev
trickdev

Reputation: 599

A Thread is an object which represent an independent path of execution (often run in parallel to another). I'm not really sure what you mean by "calling" a Thread but you can instantiate separate threads and run methods on them. Then between the Threads you have created you can use some kind of synchronisation such as Monitors, Mutexes and Events and a shared resource (being careful with cross-thread access).

For your problem I would be more tempted to use some kind of subscription pattern where the class which receives the images from the camera can update any observers of the camera. You may want an interface called ICameraObeserver with a method such as ReceiveImage, then any class could register with your camera class via some kind of method:

public void Register(ICameraObserver ico)

Then when the camera receives a new image, it can iterate through any subscribers of type ICameraObserver and call ReceiveImage passing the image it just received.

Just an idea. Be careful with updating the UI if you have multiple threads running - there is lots of information on this.

Upvotes: 1

Related Questions