PC Review


Reply
Thread Tools Rate Thread

C#, Dynamic Functions, Threads

 
 
Ahmad
Guest
Posts: n/a
 
      10th Jul 2008
Hi,

Is it possible to do something like the the following:

IBaseClass objClass = new LoadClassFromDLL( "Module.DLL");
ThreadStart objThreadStart= new
(objClass.GetFunctionFromName("GetString"))

Thread objThread = new Thread (objThreadStart)
objThread.Start();

Bearing in mind the function 'GetFunctionFromName' doesn't exist and I have
no idea how to implement it.

Thanks in Advance.

Regards,

Ahmad

 
Reply With Quote
 
 
 
 
Ciaran O''Donnell
Guest
Posts: n/a
 
      10th Jul 2008
Something along the lines of:

interface IBaseType
{
public void GetString();
}


void RunThread()
{

IBaseType obj = Activator.CreateInstance("c:\\myassembly\file.dll",
"MyAssembly.MyType");
Thread t = new Thread(new ThreadStart(obj.GetString()));
t.Start();
}


--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com


"Ahmad" wrote:

> Hi,
>
> Is it possible to do something like the the following:
>
> IBaseClass objClass = new LoadClassFromDLL( "Module.DLL");
> ThreadStart objThreadStart= new
> (objClass.GetFunctionFromName("GetString"))
>
> Thread objThread = new Thread (objThreadStart)
> objThread.Start();
>
> Bearing in mind the function 'GetFunctionFromName' doesn't exist and I have
> no idea how to implement it.
>
> Thanks in Advance.
>
> Regards,
>
> Ahmad
>

 
Reply With Quote
 
Ciaran O''Donnell
Guest
Posts: n/a
 
      10th Jul 2008
That is the closest thing to what you wrote that makes sense to me. It seems
like you either want to make an object from a class in the assembly and run a
method on it in a new thread or you want to extract a class and run a static
method on it in a new thread. The last post was the first.
This is the second incase thats what your after. I'm havent tested it but
its there of there abouts. You might need to have a little play to get the
GetMethod to find the right method etc.

Assembly ass = Assembly.LoadFrom("c:\\myassembly\file.dll");
Type mytype = ass.GetType("MyAssembly.MyType");
MethodInfo method = mytype.GetMethod("StaticMethodName", BindingFlags.Static
| BindingFlags.Public);
Thread t = new Thread(new ThreadStart(delegate() {
method.Invoke(null, new object[] { });
}));
t.Start();


--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com


"Ciaran O''Donnell" wrote:

> Something along the lines of:
>
> interface IBaseType
> {
> public void GetString();
> }
>
>
> void RunThread()
> {
>
> IBaseType obj = Activator.CreateInstance("c:\\myassembly\file.dll",
> "MyAssembly.MyType");
> Thread t = new Thread(new ThreadStart(obj.GetString()));
> t.Start();
> }
>
>
> --
> Ciaran O''Donnell
> http://wannabedeveloper.spaces.live.com
>
>
> "Ahmad" wrote:
>
> > Hi,
> >
> > Is it possible to do something like the the following:
> >
> > IBaseClass objClass = new LoadClassFromDLL( "Module.DLL");
> > ThreadStart objThreadStart= new
> > (objClass.GetFunctionFromName("GetString"))
> >
> > Thread objThread = new Thread (objThreadStart)
> > objThread.Start();
> >
> > Bearing in mind the function 'GetFunctionFromName' doesn't exist and I have
> > no idea how to implement it.
> >
> > Thanks in Advance.
> >
> > Regards,
> >
> > Ahmad
> >

 
Reply With Quote
 
Ahmad
Guest
Posts: n/a
 
      10th Jul 2008
I want the function to be dynamically loaded. GetString was an example I was
using but I placed it quoted deliberately

"Ciaran O''Donnell" wrote:

> Something along the lines of:
>
> interface IBaseType
> {
> public void GetString();
> }
>
>
> void RunThread()
> {
>
> IBaseType obj = Activator.CreateInstance("c:\\myassembly\file.dll",
> "MyAssembly.MyType");
> Thread t = new Thread(new ThreadStart(obj.GetString()));
> t.Start();
> }
>
>
> --
> Ciaran O''Donnell
> http://wannabedeveloper.spaces.live.com
>
>
> "Ahmad" wrote:
>
> > Hi,
> >
> > Is it possible to do something like the the following:
> >
> > IBaseClass objClass = new LoadClassFromDLL( "Module.DLL");
> > ThreadStart objThreadStart= new
> > (objClass.GetFunctionFromName("GetString"))
> >
> > Thread objThread = new Thread (objThreadStart)
> > objThread.Start();
> >
> > Bearing in mind the function 'GetFunctionFromName' doesn't exist and I have
> > no idea how to implement it.
> >
> > Thanks in Advance.
> >
> > Regards,
> >
> > Ahmad
> >

 
Reply With Quote
 
Ciaran O''Donnell
Guest
Posts: n/a
 
      10th Jul 2008
Is it static or Instance. You can change this to dynamic function name by
taking the Type.GetMethod bit from my second reply.
e.g

IBaseType obj = Activator.CreateInstance("c:\\myassembly\file.dll",
"MyAssembly.MyType");
Type mytype = obj.GetType();
MethodInfo method = mytype.GetMethod("MethodName", BindingFlags.Instance
| BindingFlags.Public);
Thread t = new Thread(new ThreadStart(delegate()
{
method.Invoke(obj, new object[] {/* put parameters here */ });
}));
t.Start();
--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com


"Ahmad" wrote:

> I want the function to be dynamically loaded. GetString was an example I was
> using but I placed it quoted deliberately
>
> "Ciaran O''Donnell" wrote:
>
> > Something along the lines of:
> >
> > interface IBaseType
> > {
> > public void GetString();
> > }
> >
> >
> > void RunThread()
> > {
> >
> > IBaseType obj = Activator.CreateInstance("c:\\myassembly\file.dll",
> > "MyAssembly.MyType");
> > Thread t = new Thread(new ThreadStart(obj.GetString()));
> > t.Start();
> > }
> >
> >
> > --
> > Ciaran O''Donnell
> > http://wannabedeveloper.spaces.live.com
> >
> >
> > "Ahmad" wrote:
> >
> > > Hi,
> > >
> > > Is it possible to do something like the the following:
> > >
> > > IBaseClass objClass = new LoadClassFromDLL( "Module.DLL");
> > > ThreadStart objThreadStart= new
> > > (objClass.GetFunctionFromName("GetString"))
> > >
> > > Thread objThread = new Thread (objThreadStart)
> > > objThread.Start();
> > >
> > > Bearing in mind the function 'GetFunctionFromName' doesn't exist and I have
> > > no idea how to implement it.
> > >
> > > Thanks in Advance.
> > >
> > > Regards,
> > >
> > > Ahmad
> > >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C#, Dynamic Functions, Threads Ahmad Microsoft C# .NET 3 10th Jul 2008 06:56 PM
Dynamic wait on threads Prasad Microsoft C# .NET 4 5th Dec 2005 06:09 AM
Dynamic creation of worker threads Alex Microsoft Dot NET 4 15th Nov 2005 12:26 PM
Dynamic creation of worker threads Alex Microsoft C# .NET 4 15th Nov 2005 12:26 PM
Multiple threads accessing Shared functions Bob Day Microsoft VB .NET 9 12th Dec 2003 08:49 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:34 AM.