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#?
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.
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.
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.
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?
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
});
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)
);
You could use an anonymous method:
void Foo()
{
Thread myThread = new System.Threading.Thread(delegate(){
//Your code here
});
myThread.Start();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With