Conversion of Singlethreaded C# Class to Multithreaded One

Terms definition
Multithreaded class – sounds very confusing.
In fact there is no such a thing as singlethreaded or multithreaded class,
on the other hand I do not have better name for the construction I have created.
Perhaps, I can name it a Thread Isolator.
AmThreader is a code generator that creates a class wrapper for any .NET class (not necessarily C#)
and any call of wrapper class method will be translated to original class call but in a separate thread.
Introduction
All of us use computers today. You start an application , click controls and at some
moment it stops to respond. Well, the most probable cause of it is that the main thread is
unable to exit (or continue) for some reason. What reason?
The reason in fact, can be any : Input-Output can not complete, some network problems,
internal application error and the thread went into endless loop etc.
Very annoying situation, indeed. How can this problem be resolved? The only way to create a
responsive and in a certain sense robust application is to use multithreading. However everything is
at cost because the development of multithreaded applications is more complicated. It involves
threads synchronisation, considerably more complicated debugging and so on. That is
why even reputable companies still use single threading. So, how to simplify the development
of multithreaded software?
Code Generator
The answer is AmThreader – the code generator.It converts any singlethreaded .NET class to multithreaded one.
How it works?
Let's look first into the stages of the development of simple multithreaded
application . What are the components of multithreading?
Nothing in fact is really special:
1. Temporary variables for holding and passing the parameters from one thread to another.
2. A new thread (worker thread).
3. Methods to invoke certain methods in the worker thread.
4. A loop that runs in the worker thread .
5. A bunch of enums ,switches and cases that control the work flow in the worker thread.
6. Wait procedure (or extra thread).
7. Threads synchronisation components (depend on OS and used language).
8. Exception handling.For instance we have a class ClassA that has the method DoSomething() which may not return.
public class
ClassA
{
public ClassA()
{
public void DoSomething(int b)
{
// do something
}
}
}Let's convert it to multithreaded one in order to make the application safe and friendly. Speaking in C#
terms this new class can be represented with a snippet below:
namespace SomeNameSpace
{
public class __ClassA
{
private enum Commands_enums
{
ClassA__enum_0,
DoSomething__enum_1
}
private Commands_enums CurrCommand;
private bool bError;
private bool ThreadStop;
private ManualResetEvent EventStart = new ManualResetEvent(false);
private object[] inParams = new object[100];
private object ValueParam;
private Thread fThread;
private int nTimeOut = 10000;
private int CycleTime = 15;
private ClassA ThreadObjectInstance;public __ClassA()
{
StartThread();
CurrCommand = Commands_enums.ClassA__enum_0;
WaitForComplete();
}public void DoSomething(int b)
{
inParams[0] = b;
CurrCommand = Commands_enums.doSomething__enum_1;
WaitForComplete();
}private void Worker_Thread_01()
{
while(!ThreadStop)
{
EventStart.WaitOne();
switch(CurrCommand)
{
case Commands_enums.ClassA__enum_0:
ThreadObjectInstance = new ClassA();
break;case Commands_enums.DoSomething__enum_1:
ThreadObjectInstance.DoSomething((int)inParams[0]);
break;
default:
break;
}
bStopWaiting = true;
CurrCommand = Commands_enums.wait_command__enum_;EventStart.Reset();
}
}private void StartThread()
{
ThreadStart thr_start_func = new ThreadStart (Worker_Thread_01);
fThread = new Thread (thr_start_func);
fThread.Name = "Worker_Thread_01";
fThread.Start (); //starting the thread
}private void WaitForComplete()
{
int ElapsedTime = 0;
EventStart.Set();
while(!bStopWaiting)
{
//** Place for event processing
ElapsedTime += CycleTime;
if (ElapsedTime > nTimeOut)
{
bStopWaiting = true;
throw new Exception("Wait TimeOut");
}
Thread.Sleep(CycleTime);
}
}
}}
The snippet above is the simplest ,though complete multithreaded class. Yes, it is simple , but what can it do?
> Not mu
ch, the only thing it can do well is waiting for a completion of DoSomething() method and announce
of it's failure if the worker thread sticks in it. Even with this limited functionality it is useful. The application can
always be terminated gracefully. Although the main thread is still frozen during the execution of DoSomething().
However for the event driven applications (all GUI or winforms) there is a trick to overcome that unpleasant
effect. We only need to place Application.DoEvents() inside Wait loop. The interface will still be
responding during doSomething(). The code line Thread.Sleep(CycleTime) controls the responsiveness of the
interface. The smaller CycleTime the more responsive the interface is, on the other hand it uses more CPU resources.How can the new class __ClassA be used? The same way the original class is.
For example :
The original class ClassA can be used in the way something like that :
ClassA MyClassA = new ClassA();
try
{
MyClassA.DoSomething(5);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
How to use the new class? Well , almost the same. The only difference is that the class must be
destroyed explicitly calling Dispose() , otherwise the application will never end with worker thread running.
__ClassA MyClassA = new __ClassA(); try
{
MyClassA.DoSomething(5);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
MyClassA.Dispose();So, what is so special in __ClassA and how can it be derived (not in the sense of inheritance) from ClassA ? Absolutely
nothing and this operation can be automated completely.
Using Reflection AmThreder generates new class from the Assembly with the same methods names , indexers and properties
as the original class has. Only public methods are processed, even the source code of the original class is no longer needed.
Where to download
To download fresh version of AmThreader visit http://www.amplefile.com/ , "downloads" page.
AmThreader constantly monitors if fresh version is avalable and will notify you.
History
The current version is 1.0.2.0
License
For independent developers AmThreader is free. However it has to be registered












No comments yet... Be the first to leave a reply!