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.
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.
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.
I guess is something to do with Umbraco.Core.ApplicationContext.
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
Hi Alex.
In this case i only need to retrieve data. That's why i was thinking of IPublishedContent.
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:
I kick off a background thread that makes a request to the homepage with a special query string:
When that query string is present, I process the tasks that have been queued up:
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.
Hi Nicholas.
Thats actuelly not a bad idear.
I'll play around with it and post my result.
is working on a reply...