views:

204

answers:

4

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#?

+2  A: 

Yes- in C# it is called an anonymous method. Funnily enough in the example they create a thread!

Edit- Read Jon's answer- his is more complete. Another option to consider is the Backgroundworker component.

RichardOD
What about checking for Control.InvokeRequired?
Malfist
This doesn't answer the OP's question, he is asking for a simple way to run that anonymous method in an external thread.
snicker
You'll still have to do that- there are patterns that make this easy though. Another good option for you is to use a Backgroundworker component- have you considered that? http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
RichardOD
@Snicker- it doesn't answer all of the question- I was answering the bit specifically phrased "Is there something like this in C#?"
RichardOD
Can you provide an example of using the Backgroundworker Component?
Malfist
Have a look at this- http://www.dotneat.net/2009/02/10/BackgroundworkerExample.aspx Basically you would put your fill code in the DoWork event handler. You can safely update any GUI code in the RunWorkerCompleted event handler. Does that explain it? Can you paste an example of what you wish to do to the GUI- perhaps tomorrow I can then expand my answer.
RichardOD
No, that explains it. Thanks!
Malfist
+8  A: 

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?

Jon Skeet
I think the OP wanted each call to Fill() to run on its own thread. But other than that, spot on.
LBushkin
@LBushkin: Will adjust. It's not a very clear question, to be honest.
Jon Skeet
I was going to expand my answer to include lambda's but yours is more complete (and better) anyway. A backgroundworker component is still an option, as you don't have to worry about Control.Invoke.
RichardOD
Okay, nevermaind, I don't have to execute it with Control.invoke. I thought it was earlier throwing an CrossThreadExecution thing-a-majigger
Malfist
+1 for the comment on the long variable names with little to no actual meaning
snicker
Okay, I'll update the answer to keep the EventHandler stuff in, but in a slightly different way.
Jon Skeet
The names are correspond to the Stored Procedure, or the Table that they come from. I don't know why the '1' is appended to them.
Malfist
A: 

You could use an anonymous method:


void Foo()
{
    Thread myThread = new System.Threading.Thread(delegate(){
              //Your code here
     });
    myThread.Start();
}
Seth Illgard
A: 

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)
);
oefe