C# Create New T()

You can see what I’m trying (but failing) to do with the following code: protected T GetObject() { return new T(); } Any help would be greatly appreciated. EDIT: The context was as follows. I was playing around with a custom controller class for all controllers to derive from, with standardised methods. So in context, … Read more

How can I convert this foreach code to Parallel.ForEach?

I am a bit of confused about Parallel.ForEach. What is Parallel.ForEach and what does it exactly do? Please don’t reference any MSDN link. Here’s a simple example : string[] lines = File.ReadAllLines(txtProxyListPath.Text); List<string> list_lines = new List<string>(lines); foreach (string line in list_lines) { //My Stuff } How can I rewrite this example with Parallel.ForEach? 6 … Read more

What does ‘useLegacyV2RuntimeActivationPolicy’ do in the .NET 4 config?

While converting a project that used SlimDX, and therefore has unmanaged code, to .NET 4.0 I ran into the following error: Mixed mode assembly is built against version ‘v2.0.50727’ of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information. Googling around gave me the solution, which is to add this … Read more

Cancellation token in Task constructor: why?

Certain System.Threading.Tasks.Task constructors take a CancellationToken as a parameter: CancellationTokenSource source = new CancellationTokenSource(); Task t = new Task (/* method */, source.Token); What baffles me about this is that there is no way from inside the method body to actually get at the token passed in (e.g., nothing like Task.CurrentTask.CancellationToken). The token has to … Read more

When should TaskCompletionSource be used?

AFAIK, all it knows is that at some point, its SetResult or SetException method is being called to complete the Task<T> exposed through its Task property. In other words, it acts as the producer for a Task<TResult> and its completion. I saw here the example: If I need a way to execute a Func<T> asynchronously … Read more