NextPVR Forums
  • Home
  • New Posts
  • Wiki
  • Members
  • Help
  • Search
  • Register
  • Login
  • Home
  • Wiki
  • Members
  • Help
  • Search
NextPVR Forums Public Developers v
« Previous 1 … 9 10 11 12 13 … 93 Next »
Threading

 
  • 0 Vote(s) - 0 Average
Threading
imilne
Offline

Posting Freak

Posts: 2,423
Threads: 135
Joined: Feb 2008
#1
2012-01-18, 06:45 PM
If I want to update the UI from a background thread, what's the best way of doing that?

I came across this code on the web, but it doesn't work because I think it's really designed for windows forms:

Code:
this.Invoke((MethodInvoker)delegate
{
    uiStatic.Args = some hashtable that's been updated in the thread;
});

I know this sort of stuff causes problems in Java if not done correctly, where the solution is a simple SwingUtilities.invoke... method similar to the above, and sub has mentioned in the past it would cause problems with NPVR too. I've certainly seen (rare) cases in the System plugin where the text suddenly renders nowhere near its location according to the skin (usually way out on the x-axis). I figured it might be down to the background threading I use, but I've also seen that problem once - and just the once - with the now/next popup too.

Regardless of that, I'd like to know I was doing it properly anyway.

Iain
sub
Online

Administrator

NextPVR HQ, New Zealand
Posts: 102,402
Threads: 742
Joined: Nov 2003
#2
2012-01-18, 06:52 PM
imilne Wrote:If I want to update the UI from a background thread, what's the best way of doing that?
Set a flag, and do the update the from the main thread on the next call to NeedsRendering().
imilne
Offline

Posting Freak

Posts: 2,423
Threads: 135
Joined: Feb 2008
#3
2012-01-18, 06:56 PM
Simple when you know how Big Grin

Thanks!

Iain
tmrt
Offline

Member

Posts: 188
Threads: 25
Joined: May 2007
#4
2012-05-28, 05:55 AM
sub Wrote:Set a flag, and do the update the from the main thread on the next call to NeedsRendering().

When coding the Files plugin it turned out that NeedsRendering() consistently fires only in the NMT mode. In the standard Windows-form mode I have to use a timer on the main thread. Any clue why there is a difference?
sub
Online

Administrator

NextPVR HQ, New Zealand
Posts: 102,402
Threads: 742
Joined: Nov 2003
#5
2012-05-28, 06:13 AM
tmrt Wrote:When coding the Files plugin it turned out that NeedsRendering() consistently fires only in the NMT mode.
NeedsRendering() is called pretty much continuously on the active plugin. On an NMT it's usually a few times a second. On a PC it'll probably be called a couple of dozen times a second.

Quote:In the standard Windows-form mode I have to use a timer on the main thread.
Doing stuff in NeedsRendering() is pretty much same as doing something in the timer tick event on a timer in a Windows form.

If you've got some long going operation, you'd kick it off on some background thread, and in NeedsRendering()/GetRenderList(), which run in the main thread, you'd update the UI to convey progress or whatever you'd like to tell the user
sub
Online

Administrator

NextPVR HQ, New Zealand
Posts: 102,402
Threads: 742
Joined: Nov 2003
#6
2012-05-28, 06:16 AM
I'm not 100% sure I answered your question. Ask again if you need more info.
tmrt
Offline

Member

Posts: 188
Threads: 25
Joined: May 2007
#7
2012-05-28, 07:13 AM (This post was last modified: 2012-05-28, 07:24 AM by tmrt.)
I realized that he problem is obviously not in NeedsRendering() misfiring, but UIStatic not updating after updating UIStatic.Args.

Let me show my problem:

I have:
Code:
public override bool NeedsRendering()
        {
            CheckProgress();
            bool needsRendering = base.NeedsRendering();
            needsRendering = needsRendering || uiList.NeedsRendering() || progIndicator.NeedsRendering();
            return needsRendering;
        }

bool CheckProgress()
        {
//            Logger.Error(this.GetType().ToString() + ":CheckProgress called:");
            bool disable = true;
            if (null != lastCommand)
            {
                if (lastCommand.IsRuning)
                {
                    UpdateProgressIndicator(lastCommand.GetLastStdOutLine());
                    disable = false;
                }

            }
            if (null != lastLocation)
            {
                if (lastLocation.IsMoving)
                {
                    UpdateProgressIndicator(lastLocation.GetProgressString());
                    disable = false;
                }

            }
            if (disable)
            {
                UpdateProgressIndicator("");
            }
            return disable;
        }

void UpdateProgressIndicator(string s)
        {
//            Logger.Error(this.GetType().ToString() + ":UpdateProgressIndicator called:" + s);

            Hashtable args = new Hashtable();
            args["@text"] = s;
            progIndicator.Args = args;
        }

lastLocation and lastCommand run on background threads. Using this code UIStatic progIndicator is updated on the screen regularely only in the NMT mode.

In the windows-form mode I get no updates unless I force rendering by NUtility.PluginHelperFactory.GetPluginHelper().ForceRender():

Code:
void progressTimer_Elapsed(object sender, EventArgs arg)
        {
//            Logger.Error(this.GetType().ToString() + ":progressTimer_Elapsed called" );
            if(CheckProgress()){
                progressTimer.Enabled = false;
            }
            NUtility.PluginHelperFactory.GetPluginHelper().ForceRender();
        }
sub
Online

Administrator

NextPVR HQ, New Zealand
Posts: 102,402
Threads: 742
Joined: Nov 2003
#8
2012-05-28, 07:25 AM
I'm sure there is a good reason for it. You'll probably need to through a pile of diagnostic logging messages in that CheckProgress method so you can see if it's going into the 'if' statements, and really updating the args etc.
tmrt
Offline

Member

Posts: 188
Threads: 25
Joined: May 2007
#9
2012-05-28, 07:40 PM
sub Wrote:I'm sure there is a good reason for it. You'll probably need to through a pile of diagnostic logging messages in that CheckProgress method so you can see if it's going into the 'if' statements, and really updating the args etc.

Logging showed that NeedsRendering was indeed called only occasionally on my development laptop, while everything worked as expected on another computer. It turned out that DirectX 9.0c was not installed on my laptop. Installing DirectX fixed the issue.
sub
Online

Administrator

NextPVR HQ, New Zealand
Posts: 102,402
Threads: 742
Joined: Nov 2003
#10
2012-05-28, 07:48 PM
tmrt Wrote:It turned out that DirectX 9.0c was not installed on my laptop. Installing DirectX fixed the issue.
Ok great.

I'll have a think about why it might not have been called regularly with directx runtimes missing.
« Next Oldest | Next Newest »

Users browsing this thread: 1 Guest(s)



  • View a Printable Version
  • Subscribe to this thread
Forum Jump:

© Designed by D&D, modified by NextPVR - Powered by MyBB

Linear Mode
Threaded Mode