1

I have created custom object in Python using pywin32. while calling that object takes some times. Following is my try at making that asynchronous with respect to application stat so that while macro is running application doesn't freez.

Sub demo()
    demofunction
End Sub

Function demofunction()
    Dim demoobj As Object
    Do While demoobj Is Nothing
        DoEvents
        Set demoobj = CreateObject("Python.DemoObj")
    Loop
    Debug.Print demoobj.Hello
End Function

Obviously the code is not working in terms of asynchronous run. Is this even possible any how?

Above code however working fine while freezing the application for a while.

Rahul
  • 10,830
  • 4
  • 53
  • 88
  • 1
    `CreateObject` is a blocking call. The loop doesn't actually do anything. – Comintern Feb 04 '17 at 05:36
  • Thanks for understanding it correctly that what I wanted. Thats why I put Obviously. I My code is just for explanation. – Rahul Feb 04 '17 at 06:11
  • VBA doesn't do asynchronous or multithreaded. If that's a requirement, you need a more recent/powerful language. Consider VB.NET or C#.... or hack up an async solution with VBScript+VBA. Did you research anything on the subject? – Mathieu Guindon Feb 04 '17 at 07:16
  • 1
    Very cool name FSO! – S Meaden Feb 04 '17 at 07:47

1 Answers1

1

Vba is single threaded but you can play some tricks to make it appear multi-threaded, after all rising star web server Node.js serves off of one thread but gives impression of multi-threaded.

1) So, you could put some Python component behind a web server and then use MSXML2.XMLHTTP60 asynchronously. Private WithEvents oXHR As MSXML2.XMLHTTP60 comes in handy here. (Note you need to declare this as a member of a class to use WithEvents)

2) You could shell out to a process (which gives you another thread) and call the Python component that way, then you use some Windows API calls to periodically check if the process has completed.

S Meaden
  • 8,050
  • 3
  • 34
  • 65
  • Thanks. That's exactly what I have done. See my other question : http://stackoverflow.com/questions/41935082/best-way-to-make-local-server-app-using-python?noredirect=1#comment71062831_41935082 – Rahul Feb 04 '17 at 08:18
  • Method 2 was first choice but in my environment it still lags. – Rahul Feb 04 '17 at 08:19
  • Goodness, is the Django code posted all that is required to get a web service up? – S Meaden Feb 04 '17 at 08:44
  • That is what you want to change after you have created django project using `django-admin startproject mysite` from command line. see: https://docs.djangoproject.com/en/1.10/intro/tutorial01/ – Rahul Feb 04 '17 at 08:58