Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to run a simple function on a new Thread?

I have two functions that I want to run on different threads (because they're database stuff, and they're not needed immediately).

The functions are:

            getTenantReciept_UnitTableAdapter1.Fill(rentalEaseDataSet1.GetTenantReciept_Unit);
            getTenantReciept_TenantNameTableAdapter1.Fill(rentalEaseDataSet1.GetTenantReciept_TenantName);

In javascript, I know I can create create an anonymous function and call it on a new thread quite easily with something like this:

setTimeout(new function(){doSomethingImportantInBackground();}, 500);

Is there something like this in C#?

like image 547
Malfist Avatar asked Oct 21 '09 20:10

Malfist


People also ask

How do I run a Python code from another thread?

Use the Python threading module to create a multi-threaded application. Use the Thread(function, args) to create a new thread. Call the start() method of the Thread class to start the thread. Call the join() method of the Thread class to wait for the thread to complete in the main thread.

How do you run a function in a separate thread C++?

To start a thread we simply need to create a new thread object and pass the executing code to be called (i.e, a callable object) into the constructor of the object. Once the object is created a new thread is launched which will execute the code specified in callable. After defining callable, pass it to the constructor.

How do you run the same function on multiple threads in Python?

To implement threading in Python, you have to perform three steps: Inherit the class that contains the function you want to run in a separate thread by using the Thread class. Name the function you want to execute in a thread run() . Call the start() function from the object of the class containing the run() method.


4 Answers

Your question isn't very clear, I'm afraid. You can easily start a new thread with some code, using anonymous methods in C# 2, and lambda expressions in C# 3:

Anonymous method:

new Thread(delegate() {
    getTenantReciept_UnitTableAdapter1.Fill(
        rentalEaseDataSet1.GetTenantReciept_Unit);
}).Start();
new Thread(delegate() {
    getTenantReciept_TenantNameTableAdapter1.Fill(
        rentalEaseDataSet1.GetTenantReciept_TenantName);
}).Start();

Lambda expression:

new Thread(() =>
    getTenantReciept_UnitTableAdapter1.Fill(
        rentalEaseDataSet1.GetTenantReciept_Unit)
).Start();
new Thread(() =>
    getTenantReciept_TenantNameTableAdapter1.Fill(
        rentalEaseDataSet1.GetTenantReciept_TenantName)
).Start();

You can use the same sort of syntax for Control.Invoke, but it's slightly trickier as that can take any delegate - so you need to tell the compiler which type you're using rather than rely on an implicit conversion. It's probably easiest to write:

EventHandler eh = delegate
{
    // Code
};
control.Invoke(eh);

or

EventHandler eh = (sender, args) =>
{
    // Code
};
control.Invoke(eh);

As a side note, are your names really that long? Can you shorten them to get more readable code?

like image 55
Jon Skeet Avatar answered Oct 15 '22 08:10

Jon Skeet


Similar to what's been said - I find tasks to be a bit simpler (supported as of .net 4 and can be used as follows as of .net 4.5):

Task mytask = Task.Run(() => 
{
    //Lines of code
});
like image 40
JSideris Avatar answered Oct 15 '22 09:10

JSideris


Starting threads is relatively expensive.

You might be better of using a thread from the thread pool:

ThreadPool.QueueUserWorkItem(unused =>
    getTenantReciept_UnitTableAdapter1.Fill(
        rentalEaseDataSet1.GetTenantReciept_Unit)
);
ThreadPool.QueueUserWorkItem(unused =>
    getTenantReciept_TenantNameTableAdapter1.Fill(
        rentalEaseDataSet1.GetTenantReciept_TenantName)
);
like image 28
oefe Avatar answered Oct 15 '22 09:10

oefe


You could use an anonymous method:


void Foo()
{
    Thread myThread = new System.Threading.Thread(delegate(){
              //Your code here
     });
    myThread.Start();
}
like image 38
Daniel Rodriguez Avatar answered Oct 15 '22 09:10

Daniel Rodriguez