Tokens and Threads

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
gunter
Lieutenant, Jr. Grade (LT JG)
Lieutenant, Jr. Grade (LT JG)
Posts: 22
Joined: Thu Oct 02, 2008 9:43 pm

Tokens and Threads

Post by gunter » Mon Aug 17, 2009 10:23 pm

G'day guys,

I have a couple of worker functions that need to run in the background, so that the page isn't being held up (and I'm using a polled AJAX update panel).
I have considered using the BackgroundWorker class, but would like this to works as I have alot of code that is threaded in a classic manner.
The issue is that one of the worker functions, which deletes products, categories, manufacturers and vendors, when in another managed thread, doesn't work.

The function uses a simple:

Code: Select all

foreach (Product prod in Token.Instance.Store.Products)
            {
                ++prodCount;
                if (prod != null)
                    prod.Delete();
            }
And the other foreach loops for the categories, manufacturers and vendors.

This function works if it is not threaded, that is, if it's in the code behind the button.
The Token.Instance.Store.Products collection is seemingly empty.

I have tried the following code before the foreach statements:

Code: Select all

Token.Instance.Store = new Store(StoreID);
Where the StoreID is stored in a static int, which is equalling the correct value (this I have checked),
But it still doesn't delete the items.

Any help is greatly appreciated,
The wall I'm banging my head against is getting sick of me.

User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

Re: Tokens and Threads

Post by AbleMods » Tue Aug 18, 2009 8:54 pm

I'm not sure if I follow you completely, but I think I know the answer.

In 7.0.2(maybe 7.0.3 not sure which), Able stopped populating the Token instance when HTTP context was not established. In other words, a separate thread without an HTTP context won't automatically have the Token instance populated like you would expect.

The workaround (for me at least) was easy - just call:

Code: Select all

Token.Instance.InitStoreContext(StoreDataSource.Load(1))
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com

User avatar
Logan Rhodehamel
Developer
Developer
Posts: 4116
Joined: Wed Dec 10, 2003 5:26 pm

Re: Tokens and Threads

Post by Logan Rhodehamel » Thu Aug 20, 2009 4:14 pm

Joe is right... it was 7.0.2. We had to stop assuming that your store ID was 1. In a non HTTP thread, you need to tell the token which store to work with.
Cheers,
Logan
Image.com

If I do not respond to an unsolicited private message, it's not because I'm ignoring you. It's because the answer to your question is valuable to others. Try the new topic button.

gunter
Lieutenant, Jr. Grade (LT JG)
Lieutenant, Jr. Grade (LT JG)
Posts: 22
Joined: Thu Oct 02, 2008 9:43 pm

Re: Tokens and Threads

Post by gunter » Thu Aug 27, 2009 6:37 am

Thanks guys,

I managed to find that code in another of our sites' modules (which I had written and forgotten about).

Very useful peice of information, should be in the Wiki.

Post Reply