Background worker (to prevent freezing) (1 Viewer)

Dragy

Portal Pro
April 27, 2009
778
333
32
Home Country
Netherlands Netherlands
In a windows forms app I can just use the BackgroundWorker from the Toolbox.. but how to do that with a MediaPortal plugin? I don't want that MP freezes when I retrieve some stuff from the web.
I also like to show a progressbar when it is doing stuff in the background.

How to achieve this?
 

DJBlu

Portal Pro
August 14, 2007
1,670
813
Llanelli
Home Country
United Kingdom United Kingdom
In a windows forms app I can just use the BackgroundWorker from the Toolbox.. but how to do that with a MediaPortal plugin? I don't want that MP freezes when I retrieve some stuff from the web.
I also like to show a progressbar when it is doing stuff in the background.

How to achieve this?

VB

Dim MyThread as new threading.thread(Addressof MySub)

Thread.start

Sub MySub

'Insert code here.

End Sub

C#

Thread MyThread = new Thread(addressof MyVoid);
MyThread.Start;

void MyRoutine
{
// Insert Code here
}
 

Dragy

Portal Pro
April 27, 2009
778
333
32
Home Country
Netherlands Netherlands
Thanks! I will search for mor information about Thread :)
And sorry I forgot to mention I use C#
 

SilentException

Retired Team Member
  • Premium Supporter
  • October 27, 2008
    2,617
    1,130
    Rijeka, Croatia
    Home Country
    Croatia Croatia
    I wouldn't use the bare Thread. BackgroundWorker wrapper is easier to use for this purpose.

    Decide whether you need progress bar for your purpose, for 99% web operations, using GUIWaitCursor is enough. If you need progress bar, you can use GUIDialogProgress.
     

    tourettes

    Retired Team Member
  • Premium Supporter
  • January 7, 2005
    17,301
    4,800
    If using a tread remember to add try/catch for errorn handling around the thread method. Otherwise if you manage to create an unhandled exception inside the code whole application will be closed down without a warning.
     

    Dragy

    Portal Pro
    April 27, 2009
    778
    333
    32
    Home Country
    Netherlands Netherlands
    I have made some progress with it and this is working, but is this the right way to do?
    I have some kind of feeling this is not the way to use it.

    Code:
    protected override void OnClicked(int controlId, GUIControl control, MediaPortal.GUI.Library.Action.ActionType actionType)
            {
                if (control == buttonDevices)
                {
                    bw_get = new BackgroundWorker();
    
                    GUIWaitCursor.Show();
                    bw_get.RunWorkerAsync();
                    bw_get.DoWork += bw_getDevices;
                    bw_get.RunWorkerCompleted += bw_getDevices_Completed;
                    //bw_get.ProgressChanged += bw_getDevices_ProgressChanged;
    
                    GuiList.Clear();
    
                    activeType = HCB.type.device;
                    GUIPropertyManager.SetProperty("#hcb.activeType", "Devices");
                }
    ..........

    Code:
            void bw_getDevices(object sender, DoWorkEventArgs e)
            {
                try
                {
                    e.Result = h.getDevices();    // This gets passed to RunWorkerCompleted
                }
                catch (Exception ex)
                {
                    Log.Error("bw_getDevices: " + ex.Message);
                }
            }
    
            void bw_getDevices_Completed(object sender, RunWorkerCompletedEventArgs e)
            {
                
                try
                {
                    devicesList = (SortedList)e.Result;
    
                    foreach (device d in devicesList.Values)
                    {
                        GUIListItem item = new GUIListItem();
                        item.OnItemSelected += new MediaPortal.GUI.Library.GUIListItem.ItemSelectedHandler(listSelectDevice);
                        item.Label = d.name;
                        item.Label2 = d.type;
                        item.ItemId = d.id;
                        item.IsPlayed = d.status;
    
                        GuiList.Add(item);
                    }
                    GUIPropertyManager.SetProperty("#itemcount", GuiList.Count.ToString());
                    Log.Info("HCB: Devices listed");
    
                    GUIWaitCursor.Hide();
                }
                catch (Exception ex)
                {
                    Log.Error("bw_getDevices_Completed: " + ex.Message);
                }
            }

    Download whole project here: http://dl.dropbox.com/u/21558454/MP_HomeControlBox.zip

    I hope it's all alright now :) But there is a big chance that my coding is all wrong..
    Give me some feedback please.
     

    Dragy

    Portal Pro
    April 27, 2009
    778
    333
    32
    Home Country
    Netherlands Netherlands
    It works on my computer (i7 8 virtual cores), but on the HTPC (2 cores) it only works at random times.. I get this error very often:
    bw_getDevices_Completed: object reference not set to an instance of an object.

    I cannot understand why it only works randomly..
    Also the bw_getMacros works mostly only after the first time. So the first time it just doesn't get all the macros..

    //edit
    OMG, I think I've fixed it now!
    Instead of this:
    Code:
    bw_get_devices = new BackgroundWorker();
    [B]bw_get_devices.RunWorkerAsync();[/B]
    bw_get_devices.DoWork += bw_getDevices;
    bw_get_devices.RunWorkerCompleted += bw_getDevices_Completed;
    I use now this (duh):
    Code:
    bw_get_devices = new BackgroundWorker();
    bw_get_devices.DoWork += bw_getDevices;
    bw_get_devices.RunWorkerCompleted += bw_getDevices_Completed;
    [B]bw_get_devices.RunWorkerAsync();[/B]
     

    Users who are viewing this thread

    Top Bottom