c# as a replacement for a script language

  • Thread starter Ingo Schasiepen
  • Start date
I

Ingo Schasiepen

Hi there,
i'm evaluating if c# is suited to replace a script language. Most of
the elements of this language can be replaced with a c#-library, but
it also has some realtime-like elements. E.g., in this language, the
following code is possible:

{
Int32 i=0;
OnEvent ( ... )
{
Print (i);
}
}

Whenever a certain event occurs, the specified code is executed in a
seperate thread (something similar exists in pearl (not perl) or other
realtime languages).
Of course this can be achieved with c#, but the user (which is used to
the old script language) should be able to write c#-code which looks
nearly the same as the example above.
Does anybody have an idea how this can be done?

Bye,
Ingo
 
E

Empire City

Can someone tell me what replacing only the method name "Invoke" with "BeginInvoke" in the example below means? They both produce the same results. However, when I threw some volume at it the Invoke seemed to perform more stably, although the BeginInvoke seemed faster. Is it because the BeginInvoke has a bit more overhead associated with it?



-----------------------------------------------------------



When you receive an asynchronous callback, it is recommended
that you always update the control from the same thread in which
the control was created because windows forms controls are _not_ threadsafe.

Typically, if you are updating controls from asynchronous callback methods
that are not executing in the same thread in which the control was created,
use Control.Invoke() passing the delegate that updates the control.

You can aso use Control.InvokeRequired to check if the thread is
the same thread in which your control was created, and then accordingly
decide whether you need to call Control.Invoke().

Some C# code shown below (same semantics apply to VB.NET):

if( ! MyControl.InvokeRequired )
{
this.AddItemToControl( item );
else
{
MyControl.Invoke( new AddItemDelegate

( this.AddItemToControl, new Object[] {item} ));
}
 
1

100

Can someone tell me what replacing only the method name "Invoke" with "BeginInvoke" in the example below means? They both produce the same results. However, when I threw some volume at it the Invoke seemed to perform more stably, although the BeginInvoke seemed faster. Is it because the BeginInvoke has a bit more overhead associated with it?



When you call Invoke the caller will be blocked until the callee returns (maybe that is the reason you find it more stably) If you call BeginInvoke, though, the caller (worker thread )won't be blocked and will continue working and maybe that's why you find it faster.

The Invoke make sense only when it is called from outside the UI thread, owning the control (when InvokeRequired is true) and doesn't make sense otherwise. BeginIvoke is very useful when is called from inside the UI thread as well. In this case the delegate you provided will be the next thing to be executed when the current method ends. You can think of it for something like PostMessage in Windows.


HTH
B\rgds
100
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top