开发者

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

开发者 https://www.devze.com 2023-02-21 21:32 出处:网络
I wish to run a thread to update the Image in the picturebox . The image is streamed form the camera.

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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号