none
Muli-thread in server side, does anyone have a sample. RRS feed

  • Question

  • The code will perform some batch operation and most of it are in the loop. So what i'm planning is divide the work into four, so that let say a 100 records will be divided into 4 thread and so will perform 4times faster than before.

    My plan is, 

    task1 --> 1st to 25th records will do some operation, like updating the entity

    task2 --> 26th to 50th will do the same set of operations, just a different parameter based on the record 26th to 50th

    task3 --> 51st to 75th will do the same set of operations, just a different parameter based on the record 51st to 75th

    task4 --> 76th to 100th will do the same set of operations, just a different parameter based on the record 76th to 100th

    i'm not sure if this is good approach, but when i try that using C#

    Task.Factory.StartNew(() => {ComputeComponent(listOfTask1)});
    Task.Factory.StartNew(() => {ComputeComponent(listOfTask2)});
    Task.Factory.StartNew(() => {ComputeComponent(listOfTask3)});
    Task.Factory.StartNew(() => {ComputeComponent(listOfTask4)});

    It throws exception about the thread that im working on is not valid and the one the throws is the entity object. Does anyone give me some hint or info if this possible or there is another trick to do it.


    iamrommel



    • Edited by i am rommel Monday, August 26, 2013 5:39 AM
    Monday, August 26, 2013 5:38 AM

Answers

  • Hi,

    In order to preserve data integrity, all changes made to an Entity Object can only be performed on one thread - the logical thread associated with the current request. This restriction exists to avoid concurrency issues when multiple threads changing the same entity object. That would explain the invalid thread exception you're getting above.

    The only way to work-around this restriction (that I can think of) is to make the changes directly against the SQL database in multiple threads.

    Best regards,
    Huy

    • Marked as answer by i am rommel Tuesday, September 10, 2013 2:03 AM
    Monday, September 9, 2013 8:00 PM

All replies

  • Hi,

    Can you provide sample code for me to try?

    I create code based on this link in LS and not found any error: http://msdn.microsoft.com/en-us/library/dd537610.aspx


    Regards,
    Christian HL
    Microsoft Online Community Support


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Thursday, August 29, 2013 7:01 AM
  • Hi Christian,

    See http://sdrv.ms/192bZjL for the sample application that I did. There are two types how the long process is being executed and both are failing

    1. Using the Inserting_Entity hook for event

    2. Using the WebApi

    If you can tweak the code for it to work and can explain what's the problem i guess it's worth another blog entry for the Lightswitch.

    Thanks in advance. :)



    iamrommel


    • Edited by i am rommel Thursday, August 29, 2013 8:44 AM Updated the link
    Thursday, August 29, 2013 8:44 AM
  • Hi,

    Your code itself I think have some error.

    When I comment one line inside Task.Factory.StartNew(), it works perfectly. And the error itself not showing anything about invalid thread


    Regards,
    Christian HL
    Microsoft Online Community Support


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Wednesday, September 4, 2013 1:57 AM
  • Which line from the code that you commented

    iamrommel

    Wednesday, September 4, 2013 2:18 AM
  • InsertRandomName(1000) statement outside Task.Factory.StartNew(). Same as your left picture that you put comment

    Regards,
    Christian HL
    Microsoft Online Community Support


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Wednesday, September 4, 2013 2:26 AM
  • Ok here is the error message and what i did. And also i include the Dataworkspace in the constructor.


    iamrommel


    • Edited by i am rommel Wednesday, September 4, 2013 2:35 AM
    Wednesday, September 4, 2013 2:34 AM
  • Now i included the current workspace as the parameter in the executing task, but still got the same error

    iamrommel

    Wednesday, September 4, 2013 2:40 AM
  • Hi,

    In order to preserve data integrity, all changes made to an Entity Object can only be performed on one thread - the logical thread associated with the current request. This restriction exists to avoid concurrency issues when multiple threads changing the same entity object. That would explain the invalid thread exception you're getting above.

    The only way to work-around this restriction (that I can think of) is to make the changes directly against the SQL database in multiple threads.

    Best regards,
    Huy

    • Marked as answer by i am rommel Tuesday, September 10, 2013 2:03 AM
    Monday, September 9, 2013 8:00 PM
  • Oh, that make sense.

    Thanks for the hint and info


    iamrommel

    Tuesday, September 10, 2013 2:03 AM
  • Apart from the considerations of Huy about not using multi threading, also long running processes should be avoided on a web server. Remember, an application pool can be recycled any time and leaving the long running process in an unstable state

    A long running process should be ran an a dedicated machine and a persistent message queue should exist between the IIS and the server hosting the long running process. The message queue should be both recoverable (data on disk) and transactional. A message on the queue should not be removed until processed and there should be a mechanism in place to cope with duplicate messages. For more complex scenario a service bus should be used.


    paul van bladel

    Tuesday, September 10, 2013 6:25 AM
  • Hi Paul, 

    Actually it's not really long, let say 30 seconds operation on IIS is still acceptable on my current system design. I just thought of using the multi-threading hoping that i can somehow tweak the performance. 

    But i agree with you, service bus should be considered if it really requires me for a heavier and very very long running operations. Thanks for the tips anyway. :)


    iamrommel

    Tuesday, September 10, 2013 10:22 AM