Cross Thread Operation
-
Hi, I am facing a cross-thread communication problem in my application. Let me explain in detail: My application runs on a third party software. On the click of a button on the third party GUI, I need to open and play MediaPlayer and on the click of another button, I need to Stop the MediaPlayer. After lot of research, the only way I found to play and control a mediaplayer was by adding the wmplayer control on to a window form and control it using ctlcontrol method of wmplayer component. Now to instantiate this form class, I need to open a new thread from the main thread of my application. I am doing this in the 'Play' button click handler. Now on the click of 'Stop' button on the thrid party GUI, I need to close this particular form and close the thread. Here, I am getting the CROSS THREAD OPERATION EXCEPTION. Can someone tell me how can I close this form on click of Stop button on the third party GUI? Here's the sample Code: public void OnPlay(TrtReceiveData receiveData) { Thread startModulesThread = new Thread(new ThreadStart(this.videoAlbum.VideoPlay)); startModulesThread.SetApartmentState(ApartmentState.STA); startModulesThread.IsBackground = true; startModulesThread.Start(); } void VideoPlay() { String videoThumbnailPath = @"c:\ch1_video_08.mpg"; Application.Run(playBack = new PlayBack(videoThumbnailPath)); } void StopPlayback() { //WHAT NEEDS TO WRITTEN HERE TO CLOSE AND EXIT THE FORM?? } public partial class PlayBack : Form { public PlayBack(String path) { InitializeComponent(); axWindowsMediaPlayer1.URL = path; axWindowsMediaPlayer1.Ctlcontrols.play(); } } Thanks, Kraj
-
Hi, I am facing a cross-thread communication problem in my application. Let me explain in detail: My application runs on a third party software. On the click of a button on the third party GUI, I need to open and play MediaPlayer and on the click of another button, I need to Stop the MediaPlayer. After lot of research, the only way I found to play and control a mediaplayer was by adding the wmplayer control on to a window form and control it using ctlcontrol method of wmplayer component. Now to instantiate this form class, I need to open a new thread from the main thread of my application. I am doing this in the 'Play' button click handler. Now on the click of 'Stop' button on the thrid party GUI, I need to close this particular form and close the thread. Here, I am getting the CROSS THREAD OPERATION EXCEPTION. Can someone tell me how can I close this form on click of Stop button on the third party GUI? Here's the sample Code: public void OnPlay(TrtReceiveData receiveData) { Thread startModulesThread = new Thread(new ThreadStart(this.videoAlbum.VideoPlay)); startModulesThread.SetApartmentState(ApartmentState.STA); startModulesThread.IsBackground = true; startModulesThread.Start(); } void VideoPlay() { String videoThumbnailPath = @"c:\ch1_video_08.mpg"; Application.Run(playBack = new PlayBack(videoThumbnailPath)); } void StopPlayback() { //WHAT NEEDS TO WRITTEN HERE TO CLOSE AND EXIT THE FORM?? } public partial class PlayBack : Form { public PlayBack(String path) { InitializeComponent(); axWindowsMediaPlayer1.URL = path; axWindowsMediaPlayer1.Ctlcontrols.play(); } } Thanks, Kraj
You are getting the cross thread application basically because of the nature of windows controls. Windows form controls are not Thread safe meaning if you instantiate a contol in a thread (main thread) and try to access the data from other thread it won't allow you to do that way. Now in order to access the other control you have to use Invoke method for the control. Have a look at the following articles. http://www.codeproject.com/csharp/threadsafeforms.asp http://msdn2.microsoft.com/en-us/library/ms171728.aspx