New Posts New Posts RSS Feed: Cross thread exception... where 's my problem?
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Cross thread exception... where 's my problem?

 Post Reply Post Reply
Author
pponzano View Drop Down
Senior Member
Senior Member
Avatar

Joined: 28-Apr-2011
Location: Italy
Posts: 165
Post Options Post Options   Quote pponzano Quote  Post ReplyReply Direct Link To This Post Topic: Cross thread exception... where 's my problem?
    Posted: 22-Nov-2011 at 8:50am
Hello,
I'm relatively new to DevForce IdeaBlade / Application Framework...
as far I've done some testing everything was ok... I load a single view, done my stuff, close and reopened project and everything was ok... now that I've started opening a new page things get damaged...

I've got a screen defined as

    [Export]
    public class SecuritiesNoteManagerViewModel : Screen
    {
 private BindableCollection<SecuritiesNoteResult> _SecuritiesNoteResult = new BindableCollection<SecuritiesNoteResult>();
        private readonly IApplicationRepository _repository;
        private readonly IWindowManager _windowManager;

        private DateTime _date;
        private int _ancheEstinti;

        [ImportingConstructor]
        public SecuritiesNoteManagerViewModel(IApplicationRepository repository, IWindowManager windowManager)
        {
            _repository = repository;
            _windowManager = windowManager;
        }
...

   public void OpenSecuritiesNote()
        {
            _windowManager.ShowWindow(new ReportViewModel(_repository)); //here troubles start
        }

}

When I click the Button and the OpenSecuritiesNote starts I got an execption inside my Telerik Reporting saying :

An EntityManager can only execute on a single thread. This EntityManager is authorized to execute on the thread with id=’8’; the requested operation came from the thread with Id=‘12’.

Consider calling the EntityManager’s asynchronous methods; they work safely on background threads managed by DevForce.

You may have to disable this cross-thread checking for specific reasons such as automated testing. Please review our documentation on multi-threading issues and the EntityManager.AuthorizedThreadId property.


The method that raises the exception is :

  public DomainObject GetSecuritiesNoteForReport(int xxx, DateTime data)
        {
            var query = Manager.DomainObject .Where(nota => nota.xxx == xxx&& nota.DATA == data);

            var op = query.Execute(); //here

            if (op.ToQuery().Count() == 0) return null;

            var item = op.ToQuery().FirstOrNullEntity();

            return item;

        }

I use IoC pattern just not to create a new manager... I think that the reportviewer of telerik creates the report on another thread (probably it's so since I use the domain model on a server, not in the WPF client)

Thanks



Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 22-Nov-2011 at 6:28pm
Hi pponzano;

I'm not too familiar with Telerik control but by looking at the exception message, it seems that your synchronous query inside GetSecuritiesNoteforReport is causing the problem.

As suggested by the message, you might need to restructure that method to use asynchronous query.
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 22-Nov-2011 at 7:14pm
Unless, of course, you're actually working on the server, in which case you are perfectly correct in doing synchronous.

If this is the case, then you need to make sure that your EntityManager is not being used by multiple concurrent threads. If somehow the EntityManager was created on another thread, you can set the AuthorizedThreadId to 0 to turn off the check. However, only do this if you're absolutely sure you're not doing concurrent access.

Another suggestion is to make sure that you create an EntityManager for each server side request.
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 22-Nov-2011 at 9:36pm
@pponzano,
If you are not explicilty starting a new thread on the client yourself, then the Telerick ReportViewer is indeed doing it's work on a different thread. If that's the case, then you should obtain a new instance of the repository before calling GetSecuritiesNoteForReport. Then this new repository instance will create the EntityManager on the correct thread.
 
Decorate your repository with [PartCreationPolicy(CreationPolicy.NonShared)], then IoC.GetInstance(typeof(IApplicationRepository), null) will return a new instance every time.
 
Marcel
Back to Top
pponzano View Drop Down
Senior Member
Senior Member
Avatar

Joined: 28-Apr-2011
Location: Italy
Posts: 165
Post Options Post Options   Quote pponzano Quote  Post ReplyReply Direct Link To This Post Posted: 23-Nov-2011 at 12:02am
Hello,
for @Denisk : I'm not sure that Telerik reporting handle correcly an async result..I've to try... the problem for me is due to the fact I generate the Repository on the client (WPF) and when I ask for the report the rendering report starts itself a new thread...so passing the IRepository in the report costructor is not a good thing... maybe I can create a new manager each time the report is created (since the reports are in read-only there's no risk that two manager reading at the same time will damage something)...
for @mgood (I'm advapi of http://devforcecaliburn.codeplex.com/ so you've just helped me a lot!,thanks) if I decorate with [PartCreationPolicy(CreationPolicy.NonShared)] is ok or it's just a workaround? what's the cons of this?
 
Thanks
Paolo
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down