Running process in background (threading)



  • Hi all!
    I have a command plugin that launches a process (rendering the scene in an external rendering engine). The process takes some time to complete, so I would like it to run in the background, so that the user is still able to interact with C4D while the process in running. The external renderer has options to specify if the rendering should be performed synchronously or asynchronously (launching the render process in a separate thread). I have confirmed that these options work as intended*. Regardless of these settings however, C4D freezes during the process and waits for the process to complete.

    I have not previously dealt with threading in C4D, so I am not quite sure how to approach this issue. Could there potentially be some interference between how C4D handles threads, and the threading in the renderer?

    Any suggestions on how to approach this? (I am still on R19/earlier)

    /Filip

    *I pass a variable to a "stopper callback" function in the renderer. When the render is completed, this flag changes its value. I print the state of this variable immediately after the rendering call. If I tell the renderer to operate synchronously, the printed value indicates that the process was completed, but for asynchronous rendering it correctly indicates that the render is not completed at the time when the variable is printed.



  • So, to clarify, here is what I think is happening (based on various tests):

    -The execute() function in my command plugin calls another function Foo()
    -Within Foo() there is a call to an external 3rd party library function that in turn creates a process in a separate thread.
    -The call to Foo() terminates, without waiting for the external process to finish.
    -However, the Execute() function does not terminate until the external process is finished.

    This surprises me: I would expect that Execute(), just like Foo() would continue its execution without waiting for the external process to finish. Is this the expected behaviour in Cinema 4D, and is there something I can do to change this behaviour so that Execute() does not wait for the external process?

    Thanks
    /Filip


  • Global Moderator

    Hi @Filip first of all welcome in the plugincafe community.

    Before to start I would like to point you to:
    -Read Before Posting
    -About Tags and Tagging
    -Q&A New Functionality

    I've setup your post correctly 😄 .

    Regarding your issue, there is an example in the C4DThread Manual about threading in Cinema 4D and how to render something and notify a MessageData plugin when the render is finished.

    If you have any question please provide some code example, in order to help you.
    Cheers,
    Maxime.



  • Hi Maxime,
    " welcome in the plugincafe community."
    Thanks! I've been a member of the old forum for a long time, so I am not really new to the community, just to the new forum. Thanks for setting up the post correctly, I will try to stick to the guidelines in future posts.

    I will check the link you provided and see if I can get it to work, otherwise I will get back with further questions.

    /Filip



  • Hi again,
    It turns out that the issue was not related to Cinema 4d itself, but was caused by how I handled the connection to the 3rd party library. You can mark this as solved.

    Best regards
    /Filip