|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Terms definitionStraight after the publishing of this article, I received criticism from number of readers including the reader dog_spawn: "How can you ever call a class 'single threaded' or 'multithreaded'? I have programmed using threads for years (C++, Java and recently C#) but your article makes absolutely no sense to me". In fact, he is right - there is no such a thing as single threaded 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. To avoid further misunderstanding, I would like to make clear that 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. IntroductionAll 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 cannot 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 synchronization, 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 GeneratorThe answer is AmThreader - the code generator. It converts any single threaded .NET class to multithreaded one. How it works? Let's look first into the stages of the development of simple multithreaded applications. What are the components of multithreading? Nothing in fact is really special:
For instance, we have a class using System;
namespace SomeNameSpace
{
public class ClassA
{
public ClassA()
{
}
public void DoSomething(int b)
{
// do something
}
}
}
Let's convert it to a 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 : IDisposable
{
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);
}
}
public void Dispose()
{
bStopWaiting = true;
ThreadStop = true;
EventStart.Set();
}
}
The snippet above is the simplest ,though complete multithreaded class. Yes, it is simple, but what can it do? Not much, the only thing it can do well is wait for the completion of the How can the new class For example: the original class try
{
ClassA MyClassA = new ClassA();
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 try
{
__ClassA MyClassA = new __ClassA();
MyClassA.DoSomething(5);
MyClassA.Dispose();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
So, what is so special in Using Reflection, AmThreder generates a new class from the Assembly with the same method 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 downloadTo download fresh version of AmThreader, visit http://www.amplefile.com/, "downloads" page. AmThreader constantly monitors if fresh version is available and will notify you. History
LicenseFor independent developers, AmThreader is free. However it has to be registered.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||