ObjectScope Handlingh in Multi-Threaded apps

5 posts, 0 answers
  1. Norbert
    Norbert avatar
    35 posts
    Member since:
    Dec 2009

    Posted 16 Feb 2011 Link to this post

    Hi,

    I want to use OpenAccess in desktop apps. Therefore I would like to load data asynchronosly.
    What is the best design for this purpose? Is it supportet?
    E.g. I want to load a DataGridView in the background.

    Best regards
    Norbert
  2. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 21 Feb 2011 Link to this post

    Hi Norbert,

    In general for multithread'ed apps, you should consider using the one thread - one object scope approach.
    To explain it shortly, you will provide a kind of ScopeFactory/ScopeManager that will provide a new object scope when requested and bind this scope to the thread... when the thread is finished using the scope it will release/dispose it again.

    There a several ways of doing this..Most of the examples discussed in the Code Library are based on web sites since they are by nature multi-threaded (more web reuqest coming in)... but you can easily "transfer" this to a desktop app having multiple threads for handling background data loading stuff...

    Regards

    Henrik
  3. DevCraft banner
  4. Norbert
    Norbert avatar
    35 posts
    Member since:
    Dec 2009

    Posted 21 Feb 2011 Link to this post

    Hi Henrik,

    thank you for answering.

    But what is if I want to load data in backround and bind it to controls. These are two different threads?
    Maybe I misunderstand but that's the problem?!

    I've already seen the code snippet about best usage of ObjectScope in WinForms apps - that's the way I should use it?

    Thank you in advance,
    Norbert
  5. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 21 Feb 2011 Link to this post

    Hi Norbert,

    Thanks for elaborating.... In your case I think it might be better to pass an instance of the object scope (obtained by the main/UI thread) along to the background worker thread...
    Alternatively you can have your background thread obtain (create) the object scope and dispose it again. However, your persistent instances (loaded by the background thread) will be "object scope less" since it is disposed when going into the main/UI thread...

    So my solution is basically to:

     - UI/main thread, obtain (create) scope on form level
     - Create background worker thread and pass a reference to the obtained object scope to it
     - Background worker thread will use the object scope passed to query the persistent model
     - UI/main thread will bind the returned persistent objects to the UI controls.
     - Upon closing/finishing the form, the object scope obtained in step 1 is disposed.

    Regards

    Henrik
  6. Petko_I
    Admin
    Petko_I avatar
    184 posts

    Posted 21 Feb 2011 Link to this post

    Hi Norbert and Henrik,

    It is usually recommended to have one context per thread. The examples you have seen focus on this approach. The context is intended to be a light-weight object so if you have multiple threads each thread is recommended to interact with its own context. As Henrik pointed out in order to load data asynchronously you will need a different thread to load the data. The thread can be simply a background worker for example. There are some considerations to be made before deciding on whether you will have one context for both the main thread and the data-loading thread or you will have separate ones. If you will not modify the data you will be displaying then we would suggest that you use the context-per-thread approach. If, however, you need to perform updates on the data you will be showing in the grid, then you should opt for the single context initialized in the main thread and passed to the data-loading thread.  The second solution is basically what Henrik suggested. You should be careful, though, because you will have to set the multithreading option for the context you use. In order to do so, you may open the Model Settings Dialog and in the Backend Settings -> Runtime Settings tab check the Multithreaded Context option. Do not forget the handle the disposal of the context when the main thread's life ends.

    Do not hesitate to contact us, should you have further questions.

    Kind regards,
    Petko_I
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Back to Top
DevCraft banner