Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Cross Thread Operation

Cross Thread Operation

Scheduled Pinned Locked Moved C#
questionhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kraj99
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • K kraj99

      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

      M Offline
      M Offline
      Mandaar Kulkarni
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups