Click here to Skip to main content
15,892,059 members
Articles / Programming Languages / C#

LiveCode.NET

Rate me:
Please Sign up or sign in to vote.
4.86/5 (55 votes)
6 May 2002Public Domain3 min read 231.7K   2.5K   170  
Compile C# code on-the-fly. Usage in a plug-in / plug-out component model.
//
//						PlugWithEvent
//
// sample splugin containing a class implementing 'IPlugInterface'
//				and generating event callbacks
//
using System;
using System.Threading;

using MultiPlug.PlugInterface;

namespace MultiPlug.PlugWithEvent
{

public class WithEventClass : MarshalByRefObject, IPlugInterface
{
	public WithEventClass( string initString )
	{
		baseText = "WithEventClass: " + initString;
	}

	public void Dispose()
	{
		StopThread();
	}

	public string ModifyString( string inpString )
	{
		return baseText + " @ " + inpString;
	}

	public string CurrentText
	{
		get { return baseText;  }
		set { baseText = value; }
	}

	public bool StartThread( IPlugEvent eventCallback )
	{
		this.eventCallback = eventCallback;
		if( workthread != null )
			return false;

		ThreadStart ts = new ThreadStart( this.ThreadMethod );
		workthread = new Thread( ts );
		workthread.Name = "WORKER " + this.GetHashCode().ToString();
		workthread.Start();
		return true;
	}
		
	public void StopThread()
	{
		if( workthread != null )
		{
			workthread.Abort();		// brute force...
			workthread.Join();
			workthread = null;
		}
		eventCallback = null;
	}

	/// <summary> Thread start entry point </summary>
	private void ThreadMethod()
	{
		Thread.CurrentThread.ApartmentState = ApartmentState.MTA;
		Thread.CurrentThread.IsBackground = true;
		int hint = 0;
		do
		{
			Thread.Sleep( 1000 );
			eventCallback.PlugNotification( hint++ );
		}
		while( true );
	}

	private string		baseText;
	private IPlugEvent	eventCallback;
	private Thread		workthread;
}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Web Developer
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions