Question Running a dll file in a new thread?

therehere3

New member
Joined
Sep 23, 2014
Messages
4
Programming Experience
5-10
Hello there everyone!

I have created a simple dll file that will sleep for 5 seconds then show a message and repeat.

I want to call in my .exe program my dll in a new thread and run it constantly.
So something around:

Thread t = new Thread();
t.run("Test.dll");

Or something around there?
Please help me, thanks:)
 
You don't call a DLL. You call a method. You need to reference your library in your application project, create an instance of the type containing the method you want to execute and pass that method to your Thread object, then start the thread. Your code will end up looking something like this:
using MyLibraryNamespace;

// ...

            var source = new SomeTypeFromLibrary();
            var t = new Thread(source.SomeMethod);

            t.Start();
 
You don't call a DLL. You call a method. You need to reference your library in your application project, create an instance of the type containing the method you want to execute and pass that method to your Thread object, then start the thread. Your code will end up looking something like this:
using MyLibraryNamespace;

// ...

            var source = new SomeTypeFromLibrary();
            var t = new Thread(source.SomeMethod);

            t.Start();

Shouldn't I not use "var" as my variable type and actual variable types?
SomeTypeFromLibrary source = new SomeTypeFromLibrary();
Thread t = new Thread(source.SomeMethod);


Or no?
 
Update: Also, I did this:
private void button1_Click(object sender, EventArgs e)
        {
            var source = new PHAC();
            Thread t = new Thread(source.SomeMethod);
        }


And on the PHAC I'm getting an error saying:
'PHAC' is a 'namespace' but is used like a 'type'

And my dll library file is called PHAC.dll and under my projects References I added the dll file.
 
Shouldn't I not use "var" as my variable type and actual variable types?
SomeTypeFromLibrary source = new SomeTypeFromLibrary();
Thread t = new Thread(source.SomeMethod);


Or no?

You can use `var` whenever the type can be inferred from the initialising expression. Some people use it all the time, some people never use it and some people only use it when the type is explicit in the initialising expression. In this case, it's explicit in both cases so, if you're ever going to use `var`, this would be the time. Otherwise, you have the type of the object specified explicitly on both the left and the right of the assignment. That certainly won't hurt but, in my opinion, makes the code look more cluttered than is necessary. It's a case of personal taste though. Just be consistent in whatever you choose to do.
 
Update: Also, I did this:
private void button1_Click(object sender, EventArgs e)
        {
            var source = new PHAC();
            Thread t = new Thread(source.SomeMethod);
        }


And on the PHAC I'm getting an error saying:


And my dll library file is called PHAC.dll and under my projects References I added the dll file.

I spelled it out in my example. I used "SomeTypeFromLibrary" in my example because you need to create an instance of some type from the library. You can't create an instance of a namespace. What method do you want to execute and what type is that method a member of? That's the type you instantiate and that's the method you run.
 
Back
Top Bottom