Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bo Jacobsen 605 posts 2403 karma points
    Sep 11, 2018 @ 14:58
    Bo Jacobsen
    0

    How to start a new Thread that can access IPublishedContent property values?

    Hi all.

    I wanna start a new background Thread to run a long task. But i cannot figure out how to get it to access IPublishedContent property values.

    This is what i got.

    Thread thread = new Thread(() => Process(id, helper))
    {
        IsBackground = true
    };
    thread.Start();
    
    private static void Process(int id, UmbracoHelper helper)
    {
        // Works fine
        var content = helper.TypedContent(id);
    
        // Works fine
        if(content.HasValue("something"))
        {
            // This returns null
            var value = content.GetPropertyValue<string>("something");
        }
    }
    

    I guess is something to do with Umbraco.Core.ApplicationContext.

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Sep 11, 2018 @ 15:08
    Alex Skrypnyk
    0

    Hi Bo

    I would suggest you to work with Content service, it should work. Are you going only to retrieve data or write also?

    Alex

  • Bo Jacobsen 605 posts 2403 karma points
    Sep 11, 2018 @ 20:13
    Bo Jacobsen
    0

    Hi Alex.

    In this case i only need to retrieve data. That's why i was thinking of IPublishedContent.

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Sep 11, 2018 @ 23:56
    Nicholas Westby
    0

    I don't have a good answer for how to do this the "right" way, but here's what I have done in the past as a workaround.

    A request comes in to the homepage:

    /
    

    I then add my task to a collection:

    AddTask(() => Process(id, helper)));
    

    I kick off a background thread that makes a request to the homepage with a special query string:

    /?special-query-string=special-value
    

    When that query string is present, I process the tasks that have been queued up:

    ProcessTasks(TaskQueue);
    

    That way, a background thread is used to make a request to a special URL (which means the user of the site can view the page without a delay), but then when the site gets that special URL, it knows to process the tasks in the current thread (which it knows was requested by a background thread rather than a site visitor).

    And there is a bit of locking and such to make sure things don't go haywire when multiple requests come in around the same time.

    An alternative would be to use Umbraco's built-in scheduled tasks: https://our.umbraco.com/documentation/reference/config/umbracosettings/#scheduledtasks

    You could have that hit a special URL periodically so that you wouldn't have to start the background thread to hit the special URL.

  • Bo Jacobsen 605 posts 2403 karma points
    Sep 12, 2018 @ 06:39
    Bo Jacobsen
    0

    Hi Nicholas.

    Thats actuelly not a bad idear.

    I'll play around with it and post my result.

Please Sign in or register to post replies

Write your reply to:

Draft