Is there anything wrong or inherently unsafe about the way I’ve programmed this? I’m still learning threading logic in .NET.
Basically, I had APIManager.ExecuteRequest() being called twice, and it takes a while, so I wanted the two calls to happen concurrently. It looks like the dataResult variables are both suitably populated after the join, but I keep seeing things about AsyncResult and all these other .NET concurrency related APIs and I thought that I probably oversimplified this a little.
If there isn’t anything wrong, can someone maybe tell me a better way to get the same result?
MyDataResult dataResult1 = null, dataResult2 = null;
System.Threading.Thread t1 = new System.Threading.Thread(delegate()
{
dataResult1 = APIManager.ExecuteRequest(dataRequest1, TBIdentifiers.Text, TBCommands.Lines);
});
System.Threading.Thread t2 = new System.Threading.Thread(delegate()
{
dataResult2 = APIManager.ExecuteRequest(dataRequest2, TBIdentifiers.Text, TBCommands.Lines);
});
t1.Start();
t2.Start();
t1.Join();
t2.Join();
This seems OK.
Maybe you could optimize this a little by running second request in current thread, instead of having 3 threads where one is only waiting for the other two: