Thread in Plugin not Closing

Discussion in 'MediaPortal 1 Plugins' started by hbe02, April 5, 2008.

  1. hbe02

    hbe02 Portal Member

    Joined:
    April 1, 2008
    Messages:
    26
    Likes Received:
    1
    Ratings:
    +1 / 0
    Home Country:
    Hi all, Im running a thread in a plugin im making, i want this thread to keep running even when the user switches to another screen. But i also want the thread to be terminated when the user Exits Media Portal.
    Right now its working perfectly, but when i exit media portal i still have a thread under "Media Portal" in the running processes under CTRL+ALT+DEL.
    I declared the thread as follows:
    Code (Text):
    1. ListeningProgram = new Thread(commandsHandler);
    2.             ListeningProgram.Start();
    commands handler is a funciton that contains an infinite loop.

    So basicly my question is, where do i put the statement:
    Code (Text):
    1. ListeningProgram.Abort();  
    Since i want the thread to keep running even when the user switches to another screen, i assume that i cannot abort the thread in the:
    Code (Text):
    1. virtual void OnPageDestroy(int newWindowId)
    thanks :-D


     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. solis66
    • Premium Supporter

    solis66 MP Donator

    Joined:
    January 22, 2007
    Messages:
    141
    Likes Received:
    20
    Ratings:
    +20 / 0
    Home Country:
    Sweden Sweden
    Hi hbe02.

    Perhaps you can do your thread as a process-plugin that always runs when MP is running.
    Then you can interact with this plugin from your Window-plugin to get the information you request.

    If you dont need it to run all the time you can activate it when starting MP and start the executionthread from the windowplugin if you want.

    In this way, the processplugin starts and stops with MP, independent of the window plugin.
    Hopes this helps.
     
  4. and-81
    • Premium Supporter

    and-81 Retired Team Member

    Joined:
    March 7, 2005
    Messages:
    2,257
    Likes Received:
    183
    Gender:
    Male
    Occupation:
    IT Consultant/Developer
    Location:
    Melbourne
    Ratings:
    +183 / 0
    Home Country:
    Australia Australia
    Putting your thread in a process plugin is the best way to have it start and stop when you want.

    But, you should also consider setting:
    thread.IsBackground = true;

    This sounds like you're giving it a lower priority, but what this setting actually does is make the thread terminate if the application terminates. Without setting this the thread will keep running even after the application has stopped (unless you stop it properly yourself, which you should).

    Hope that helps,
     
  5. solis66
    • Premium Supporter

    solis66 MP Donator

    Joined:
    January 22, 2007
    Messages:
    141
    Likes Received:
    20
    Ratings:
    +20 / 0
    Home Country:
    Sweden Sweden
    Thanks, thats news to me too.
    Always good to learn some new tricks...
     
  6. hbe02

    hbe02 Portal Member

    Joined:
    April 1, 2008
    Messages:
    26
    Likes Received:
    1
    Ratings:
    +1 / 0
    Home Country:
    Thank guys!!! it worked!!
    solis66, thanks for the feedback, i think making my program run as a process plugin sounds like a great idea. and would keep everything standardised in MP.
    since i still didnt get into creating process plugins. i tried setting the thread as a background with:
    thread.IsBackground = true;
    worked perfectly fine, and i wish i would have known this property of threads like a year ago because ive had so much trouble terminating threads b4. thanks and-81
    btw, let me explain what im doing. my plugin basically opens a TCP sockets and listens to it in the background waiting for a file path like C:\asong.mp3 .. and then plays it on MP player. well, so far this is what im trying to accomplish but i have a somehow bigger vision for it like networking or voice activation where i could use different languages to interact with that TCP socket, even from a webserver!
    im still having trouble playing the music, but im putting that on a differnt thread :)
     
  7. and-81
    • Premium Supporter

    and-81 Retired Team Member

    Joined:
    March 7, 2005
    Messages:
    2,257
    Likes Received:
    183
    Gender:
    Male
    Occupation:
    IT Consultant/Developer
    Location:
    Melbourne
    Ratings:
    +183 / 0
    Home Country:
    Australia Australia
    I've actually started on a Telnet Interface to MediaPortal ... sounds like we're both doing the same thing ...

    It's still early days but check out the attachment if you're interested. It's accessible on port 2381.

    Cheers,

    PS. Try it out in Hyperterminal and type HELP to get a list of commands.

    Basically you can send actions, jump to a screen, or play a file with it. And it will send you notification of certain events as they occur in mediaportal.
     
  8. hbe02

    hbe02 Portal Member

    Joined:
    April 1, 2008
    Messages:
    26
    Likes Received:
    1
    Ratings:
    +1 / 0
    Home Country:
    very impressive code. i see youve made it pretty standardized and professional. my code is not really for normal users so all you do is send a string path file on the TCP socket, the plugin recieves it and simply plays the file.
    check out this post maybe you can help me:
    https://forum.team-mediaportal.com/plugins-47/play-file-37526/

    btw what is GUIWindowManager
    and how does this work:
    GUIWindowManager.SendThreadMessage(message);

    anyways, great job! I also attached a copy of what ive done :)

    [EDIT] okay my university firewall seems to have a problem with me uploading this file.

    until i firgure that out, here is some sample code :
    Code (Text):
    1.   private HDSockets comSocket;
    2.         private Thread ListeningProgram;
    3.         private bool newcommand = false;
    4.         private String FileToPlay;
    5.         private System.Windows.Forms.Timer timer1;
    6.        
    7.  
    8.         public override bool Init()
    9.         {
    10.             return Load(GUIGraphicsContext.Skin + @"\HDMPPlugin.xml");
    11.         }
    12.      
    13.         protected override void OnPageLoad()
    14.         {
    15.             //load listening socket
    16.             comSocket = new HDSockets(4500, HDSockets.ConnectionType.TCP);
    17.             comSocket.Bind();
    18.             comSocket.Listen(5);
    19.  
    20.             //start thread that recieves data
    21.             ListeningProgram = new Thread(commandsHandler);
    22.             ListeningProgram.IsBackground = true;
    23.             ListeningProgram.Start();
    24.  
    25.             //initialize parameters
    26.             newcommand = false;
    27.  
    28.             //initialize timer to play songs
    29.             timer1 = new System.Windows.Forms.Timer();
    30.             timer1.Interval = 1000;
    31.             timer1.Tick += new System.EventHandler(timer1_Tick);
    32.             timer1.Enabled = true;
    33.             //popUp("HD SmartHome Status", "HD SmartHome Has Been Loaded Successfuly");
    34.         }
    35.  
    36.         void commandsHandler(object obj)
    37.         {
    38.            
    39.             while (true) //loop forever listening for recieved commands
    40.             {
    41.                 HDSockets Client = new HDSockets(4500, comSocket.Accept());   //accept connection and create NetSocket to represetn client
    42.                 ThreadPool.QueueUserWorkItem((new WaitCallback(clientHandler)), Client); //handle client in separate thread
    43.             }
    44.         }
    45.  
    46.         void clientHandler(Object obj)
    47.         { //playing here created alot of problems b/c
    48.           //media player instance cannot be declared here
    49.             HDSockets Client = (HDSockets)(obj);
    50.             FileToPlay = Client.Recieve();
    51.             newcommand = true;
    52.             Client.Close();
    53.         }
    54.         private void timer1_Tick(object sender, EventArgs e)
    55.         {
    56.             if (newcommand == true)
    57.             {
    58.                 MediaPortal.Player.g_Player.Play(FileToPlay.Trim(), MediaPortal.Player.g_Player.MediaType.Music);
    59.                 newcommand = false;
    60.             }
    61.         }
    62.         private void SendtoVoice(String IP, String Playlist)
    63.         {
    64.             HDSockets initSocket = new HDSockets(4501, HDSockets.ConnectionType.TCP);
    65.             if (initSocket.Connect(IP) == true)
    66.             {
    67.                 initSocket.Send(Playlist);
    68.                 initSocket.Close();
    69.             }
    70.         }
    71.  
    72.  
    73.         protected override void OnClicked(int controlId, GUIControl control,Action.ActionType actionType)
    74.         {
    75.             if (control == buttonOne)
    76.                 OnButtonOne();
    77.          
    78.             base.OnClicked(controlId, control, actionType);
    79.         }
    80.  
    81.         private void OnButtonOne()
    82.         {
    83.             SendtoVoice("127.0.0.1", "Activate");
    84.             popUp("Status", "HD Smart Home Activated Successfuly");
    85.         }
    86.  
    87.         private void popUp(String title, String text)
    88.         {
    89.             GUIDialogOK dlg = (GUIDialogOK)GUIWindowManager.GetWindow(
    90.             (int)GUIWindow.Window.WINDOW_DIALOG_OK);
    91.             dlg.SetHeading(title);
    92.             dlg.SetLine(1, text);
    93.             dlg.SetLine(2, String.Empty);
    94.             dlg.SetLine(3, String.Empty);
    95.             dlg.DoModal(GUIWindowManager.ActiveWindow);
    96.         }
    97.  
    A few comments on my code, I tried Playing the file directly in my thread that handles socket requests. That didnt work! i cant remember why i debugged it a long time ago. After that i tried playing the file in the Process() function of my plugin. that also backfired and gave me some errors executing after a few times. sorry i dont remember much details. so what i did was make a timer that executes every 0.5 seconds. this timers simply checks if a flag has been change, this flag indicates if a request was recieved in the socket. it anything was recieved it simply plays the file located in a variable that is stored when the plugin recieves the data. yes i thought this approach was a bit eccentric, but.. thats the only was i could make it work that fast:-D
     
  9. and-81
    • Premium Supporter

    and-81 Retired Team Member

    Joined:
    March 7, 2005
    Messages:
    2,257
    Likes Received:
    183
    Gender:
    Male
    Occupation:
    IT Consultant/Developer
    Location:
    Melbourne
    Ratings:
    +183 / 0
    Home Country:
    Australia Australia
    GUIWindowManager is from the MediaPortal core, the SendThreadMessage function just fires a message into MediaPortal for the relevant component to pick up and act on it.

    If you trace the GUIMessage.MessageType.GUI_MSG_PLAY_FILE through the MediaPortal source you'll see where it gets acted on.

    You should definitely change your plugin from Window to Process, this will help a lot.

    I haven't done enough testing on the play file message yet, but I think to use it correctly you need to go to the relevant window first... It's still early days for my Telnet Interface.

    I'll see what I can do with it and then reply on your "Play file" thread.

    Good luck with it all,
     
  10. hbe02

    hbe02 Portal Member

    Joined:
    April 1, 2008
    Messages:
    26
    Likes Received:
    1
    Ratings:
    +1 / 0
    Home Country:
    at first i wasnt sure about creating a process plugin. after reviewing you code. I came to an understanding that in order to create a process plugin my class has to inherit from (IPlugin and ISetupForm) just like you have:
    public class TelnetInterface : IPlugin, ISetupForm
    all code under the Isetupform region is as before but the process code should be in the:
    #region IPlugin Members

    from then all i have to code is copy paste the .dll file into the \plugins folder and thats it? is my formulation correct?

    it seems safer to work with GUIWindowManager. i will be checking it out in the next couple of days.

    thanks again:)
     
  11. and-81
    • Premium Supporter

    and-81 Retired Team Member

    Joined:
    March 7, 2005
    Messages:
    2,257
    Likes Received:
    183
    Gender:
    Male
    Occupation:
    IT Consultant/Developer
    Location:
    Melbourne
    Ratings:
    +183 / 0
    Home Country:
    Australia Australia
    yep, it's not hard to turn it into a process plugin. And you will get the advantage that the process plugin's Start and Stop methods get called when MediaPortal starts and stops.

    Cheers,
     
Loading...

Users Viewing Thread (Users: 0, Guests: 0)

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice
  • About The Project

    The vision of the MediaPortal project is to create a free open source media centre application, which supports all advanced media centre functions, and is accessible to all Windows users.

    In reaching this goal we are working every day to make sure our software is one of the best.

             

  • Support MediaPortal!

    The team works very hard to make sure the community is running the best HTPC-software. We give away MediaPortal for free but hosting and software is not for us.

    Care to support our work with a few bucks? We'd really appreciate it!