Background Thread understanding?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 2569 Views - Last Post: 20 February 2017 - 10:53 AM Rate Topic: -----

#1 JapanDave  Icon User is online

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 363
  • Joined: 01-February 16

Background Thread understanding?

Posted 18 February 2017 - 11:12 PM

For the last moth or so I have been playing around with background threads as I have an application that runs over a VPN and running everything on the UI thread causes it to be slooowwww.

So my question(s) are:
  • If I put a method onto a background thread, does all the methods that method use also stay on that thread? Of coarse , the logical answer is yes they do, but I just want to be sure that there are no instances where this is not true.
  • Is there any sense in putting each method onto its own thread? Even if the mother thread is already a background thread?


Cheers.

This post has been edited by JapanDave: 18 February 2017 - 11:12 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Background Thread understanding?

#2 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6504
  • View blog
  • Posts: 14,355
  • Joined: 02-June 10

Re: Background Thread understanding?

Posted 19 February 2017 - 02:24 AM

Quote

Is there any sense in putting each method onto its own thread? Even if the mother thread is already a background thread?

Sure. Happens a lot when you have some kind of processing that can benefit from it... maybe has a bunch of waiting time etc.

Case-use:
  • Network study. If you need to ping 0-255 on your network to see if there is a device. Since ping has its own delay doing that 255 times on one thread means a long delay. If you have some sort of "Network Status" app on your phone you've seen this in action.
  • File processing. Maybe you load 25 text files then process them to count the number of times "cat" and "cheeseburger" are in the same sentence. Again, parallel is better than serial. The Program "Subtitle Edit" does this with its 'batch process' mode. You've probably seen other programs do some sort of parallel batch processing... Maybe an image converter that takes a folder of .png and outputs a folder of .jpg
  • Video processing. Video is just a bunch of still frames. If you were going to say... crop the video... you can crop each frame on its own thread then reassemble the results.
  • Internet service calls. If you have an app that goes through your video collection and gets all the metadata online... Or gets the weather for the top 25 cities... News feed fetcher...

Was This Post Helpful? 1
  • +
  • -

#3 JapanDave  Icon User is online

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 363
  • Joined: 01-February 16

Re: Background Thread understanding?

Posted 19 February 2017 - 02:56 AM

@tlhIn`toq, ahh, you have made things crystal clear again for me. One more question, the max amount of parallel threads is supposed to around 1000 which I can't remember where I read that, but did you happen to know how many threads a C# app can have running at once? I am guessing the hardware can play a part as well.

This post has been edited by JapanDave: 19 February 2017 - 02:57 AM

Was This Post Helpful? 0
  • +
  • -

#4 shoddy  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 75
  • Joined: 07-March 15

Re: Background Thread understanding?

Posted 19 February 2017 - 02:59 AM

Quote

So my question(s) are:
If I put a method onto a background thread, does all the methods that method use also stay on that thread? Of coarse , the logical answer is yes they do, but I just want to be sure that there are no instances where this is not true.

Answer yes. If you put method A in Thread and method A invokes method B and C for example then methods A,B,C will be running in the same thread until they finish their tasks.

Quote

Is there any sense in putting each method onto its own thread? Even if the mother thread is already a background thread?

Answer it depends. If each method takes a long time complete (for example method A calculates average of a lot of numbers, method B selects and transforms a lot of data) then yes you can and maybe you shoud put each method (A and B) on different threads. But if your method A invokes method B like i mentioned above you can only put method A in thread.
If you start a lot of threads in the background then you can get a lower performance.
Was This Post Helpful? 1
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6504
  • View blog
  • Posts: 14,355
  • Joined: 02-June 10

Re: Background Thread understanding?

Posted 19 February 2017 - 03:09 AM

View PostJapanDave, on 19 February 2017 - 03:56 AM, said:

@tlhIn`toq, ahh, you have made things crystal clear again for me. One more question, the max amount of parallel threads is supposed to around 1000 which I can't remember where I read that, but did you happen to know how many threads a C# app can have running at once? I am guessing the hardware can play a part as well.


I don't know the max numbers off the top of my head. Most OSes have their own thread-pool systems built in to handle that. For example if you look at one of those Network Status apps you'll see 5-15 requests go out, then as the results come in new requests go out. The coder didn't do those in batches. They just started 255 threaded operations. The thread management infrastructure held things up until threads were available.

If you have an 8xdual core you get more threads than a single Pentium 4. But its not the job of the developer to micro manage that stuff these days which is nice.

Then there are differences for other reasons. If your C# is running on a Xamarin app, on an Android device, and you are specifying multiple thread calls to all be marshaled to the Main/UI thread... Android only lets 5 threads at a time run like that - presumably to keep the UI responsive. I think iOS does a similar thing but the number is probably different. And probably different between an iPhone 3 and the latest generation iPad.
Was This Post Helpful? 1
  • +
  • -

#6 JapanDave  Icon User is online

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 363
  • Joined: 01-February 16

Re: Background Thread understanding?

Posted 19 February 2017 - 03:23 AM

Thanks again guys. That is what I figured. Up votes for the help.

Dave
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • lashings of ginger beer
  • member icon

Reputation: 6338
  • View blog
  • Posts: 25,559
  • Joined: 12-December 12

Re: Background Thread understanding?

Posted 19 February 2017 - 03:37 AM

From your other topic I guess that you are still working with Threads. I'll mention again the preference for Tasks and the TPL.

Threads vs Tasks

Quote

Conclusion
The bottom line is that Task is almost always the best option; it provides a much more powerful API and avoids wasting OS threads.

The only reasons to explicitly create your own Threads in modern code are setting per-thread options, or maintaining a persistent thread that needs to maintain its own identity.

(I also think you should be discovering bottlenecks in your application, I get the impression that you might be viewing the use of threads as a panacea.)
Was This Post Helpful? 2
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6504
  • View blog
  • Posts: 14,355
  • Joined: 02-June 10

Re: Background Thread understanding?

Posted 19 February 2017 - 03:45 AM

Quote

I get the impression that you might be viewing the use of threads as a panacea.


Mmm.... 5:30am... A stack of panacea with butter and syrup sounds good right now. Side of bacon and hash browns.


I'd also point out that since you said you were going to look at Xamarin, tasks over threads are the suggested/preferred mechanism.
Was This Post Helpful? 2
  • +
  • -

#9 JapanDave  Icon User is online

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 363
  • Joined: 01-February 16

Re: Background Thread understanding?

Posted 19 February 2017 - 04:00 AM

Ahhh, @andrewsw & @tlhIn`toq, looks like I have been barking up the wrong tree, I need to look at Task. May take a while, I but I will see what mischief I can get up to with them.
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5824
  • View blog
  • Posts: 19,831
  • Joined: 05-May 12

Re: Background Thread understanding?

Posted 19 February 2017 - 08:14 AM

Also the fact that things running slow over VPN suggests that you are doing network intensive things. It was just masked when things ran on your LAN because your LAN was presumably slow. A basic approach and/or algorithmic change maybe needed so that you are not hitting the network as often and/or moving around as much data if you can avoid it.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5824
  • View blog
  • Posts: 19,831
  • Joined: 05-May 12

Re: Background Thread understanding?

Posted 19 February 2017 - 08:24 AM

I've noticed in your past threads that you seem to like using ToList(). That will force the LINQ query to get all the data all at once. If all you need is to see if the list is non empty, perhaps using Any() would satisfy your needs.

Or if you really need all the data, but you are only dealing with items one at time, omit the ToList() and just return an IEnumerable<T> instead. That way data is sent as needed. (This may seem counter intuitive since it will seem like you'll be hitting the network more often, but the user experience is actually better because the UI is responsive between the requests.)

Any which way, the comments about panacea is quite accurate. You need to run your code through a profiler, analyze the bottlenecks, and address them. Often just throwing more CPU at the problem is not the best solution.
Was This Post Helpful? 2
  • +
  • -

#12 JapanDave  Icon User is online

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 363
  • Joined: 01-February 16

Re: Background Thread understanding?

Posted 19 February 2017 - 05:25 PM

@Skydiver, yes that is very true. Of late I have been trying to get rid of those kind of calls.

I do have a question I have always wondered. In the code below, is putting the method in the loop bad practice? Is the method querying the database each time when going through the loop. I tried seeing if there was a perfomance difference compared to the second example, but I can't seem to get much of a performance difference.

Which is better,

This is the method

public static IEnumerable<T> GetData(DatabaseDataContext dataContext)
   {
      return dataContext.T.Where(p => p.Data  == someData);
   }


And the code to loop through,


foreach (var item in GetData(dataContext))
   {
       //do something
   }


And this code:


var sourceData = GetData(dataContext);
      
      foreach (var item in sourceData)
      {
           //do something
      }


Ignoring possible NullReference, which is the better practice?

This post has been edited by JapanDave: 19 February 2017 - 05:26 PM

Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5824
  • View blog
  • Posts: 19,831
  • Joined: 05-May 12

Re: Background Thread understanding?

Posted 19 February 2017 - 05:37 PM

The two are the same.

Depending on how good my unit tests are for GetData() I will choose one over the other. I've been diligent and have great tests, I would do the former. I've been lazy and have very little tests, then the latter so that I can inspect a variable with the debugger if I really need to so that I can figure out what is happening.
Was This Post Helpful? 1
  • +
  • -

#14 JapanDave  Icon User is online

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 363
  • Joined: 01-February 16

Re: Background Thread understanding?

Posted 19 February 2017 - 05:45 PM

Just curious, I would have thought GetData(dataContext) would a fraction slower due to having to check each time if the variable has data in it. Or am I mistaken in how it treats the data?
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5824
  • View blog
  • Posts: 19,831
  • Joined: 05-May 12

Re: Background Thread understanding?

Posted 19 February 2017 - 06:28 PM

When in doubt compile a release version of your code and examine the IL. :)
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2