Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about CsScript/threading
#1
This is probably a more general QM programming question.

I have a CsScript which has multiple C# methods to invoke different times over the life of the program (which may be running for many hours). I believe that the C# calls themselves are thread safe. What is the best way to keep this persistent and how to make this available for other threads to invoke?

I could make a wrapper class that wrapped the CsScript - I'd make a singleton instance of this QM class and make a it a global to be accessed by other threads. This would be very simple - but is it safe? I read somewhere in the documentation that some COM pointers shouldn't be passed between threads - I don't know what the rules are for CsScript within QM.
#2
Macro CsScript example - global variable
Code:
Copy      Help
;CsScript variables can be global, and can be used by multiple threads.
;C#/VB script functions can be called from QM by multiple threads simultaneously. Thread-safety depends only on the C#/VB script.
;CsScript initialization functions (AddCode etc) should not be called by multiple threads simultaneously. Use lock if need to prevent it.

;EXAMPLE
CsScript+ g_cs
#opt nowarnings 1 ;;disable warning "Most COM objects cannot be global" for g_csObj
IDispatch+ g_csObj
#opt nowarnings 0
int+ g_cs_inited
;g_cs_inited=0 ;;enable this when want to change C# code or options
if !g_cs_inited
,lock ;;prevent executing initialization code by multiple threads simultaneously
,if !g_cs_inited
,,;g_cs.SetOptions("...")
,,g_cs.AddCode("")
,,g_csObj=g_cs.CreateObject("TestGlobal")
,,g_cs_inited=1
,lock-

;now script functions can be called by multiple threads simultaneously
out g_cs.Call("TestGlobal.StaticCounter")
out g_csObj.Counter()

#ret
using System;

public class TestGlobal
{
static int ms_counter;
int m_counter;

static public int StaticCounter() { return ms_counter++; } //this code is not thread-safe
public int Counter() { return m_counter++; } //this code is not thread-safe
}
#3
Thanks! The first example you sent me worked great but I was (slightly) worried by the warning messages; now that you've updated it it's perfect. Big Grin


Forum Jump:


Users browsing this thread: 1 Guest(s)