Browsing 7239 questions and answers with Jon Skeet

How to preserve await behavior with TaskCompletionSource.SetException?

(This is a new attempt at this question which now demonstrates the issue better.) Let's say we have a faulted task (var faultedTask = Task.Run(() => { throw new...
Jon Skeet
people
quotationmark

It turns out that await can throw AggregateException, though, which is not documented behavior. No, that's the behaviour when the first nested exception is an AggregateException. Basically, when you call... more 2/15/2016 4:03:55 PM

people

Load an image from URL as base64 string

I am trying to load an image as a base 64 string so that i can show it in a html like this: <html><body><img...
Jon Skeet
people
quotationmark

It seems to me that you need to separate the base64 part, which is only needed in your HTML, from fetching the data from the response. Just fetch the data from the URL as binary data and convert that to base64. Using HttpClient makes this... more 2/15/2016 10:17:23 AM

people

What is the use of the return value of the StringBuilder Append(string...) function?

The complete syntax of StringBuilder's Append(string s) function (and similar functions) is StringBuilder myStringBuilder.Append(string myString) since...
Jon Skeet
people
quotationmark

It means you can easily chain calls together: sb.Append("Foo=").Append(foo).Append("&Bar=").Append(bar); ... instead of using several separate... more 2/15/2016 10:03:50 AM

people

What will happen if two clients send request simultaneously to a ServerSocket that is using a while loop to accept requests

I've two clients on two different machines and I don't know when they're gonna send requests. is this the scenario where I HAVE TO use Selector and...
Jon Skeet
people
quotationmark

is this the scenario where I HAVE TO use Selector and ServerSocketChannel? Nope. A more common solution is to have one thread per client - when you accept a call, create a new thread (or use an existing one from a thread-pool) and use... more 2/14/2016 2:12:25 PM

people

Trying to get a list of properties dynamically where count > 0

I have a model like so public class UserModel { List<UserModel> users } public class UserModel { public List<UserSomeObj> userSomeObj { get; set; } public...
Jon Skeet
people
quotationmark

As List<T> implements the non-generic ICollection interface, you can cast to that: var results = users.Select(x => x.GetPropertyValue(prop)) .Cast<ICollection>() .Where(list =>... more 2/13/2016 4:14:27 PM

people

Consuming rest api timeout

How to deal with timeout problem when calling REST service. My GetAsync function: private async Task<string> GetAsync(string endPoint, string id) { using...
Jon Skeet
people
quotationmark

I would suggest making your method accept a CancellationToken that you can then pass onto the GetAsync and ReadAsStringAsync methods. You can easily create a cancellation token which will expire after a certain time: var cts = new... more 2/12/2016 2:53:00 PM

people

Closures behaving differently in for and foreach loops

While experimenting with closures in C# I found out that they work rather unexpectedly if they capture an iterator variable in a loop. var actions = new...
Jon Skeet
people
quotationmark

In C# 5 and beyond, the foreach loop declares a separate i variable for each iteration of the loop. So each closure captures a separate variable, and you see the expected results. In the for loop, you only have a single i variable, which... more 2/12/2016 2:46:13 PM

people

Unable to cast object of type 'System.Object[,]' to type 'System.Object[]'

I have a dll which gives me as an output an Object[,] {Name = "Object[,]" FullName = "System.Object[,]"} I am trying to convert it to Object[] to be able to read it properly,...
Jon Skeet
people
quotationmark

Rather than using it as an Object[] (which it isn't), use the System.Array API: var dictionary = new Dictionary<string, string>(); // For example Array array = (Array) data; for (int i = 0; i < array.GetLength(0); i++) { ... more 2/12/2016 11:34:13 AM

people

Cancellation token for observable

How can I cancel the following type of Rx Observable, if the following observable is being created on a StartButton click, i.e from a stop button. var instance =...
Jon Skeet
people
quotationmark

You retain the IDisposable that was returned by Subscribe, and call Dispose on it. There may well be a way of integrating the Rx IDisposable-based unsubscription with CancellationToken out of the box, but just calling Dispose would be a... more 2/12/2016 11:24:07 AM

people

Weird java.lang.ExceptionInInitializerError JSoup

In my newest Project I use JSoup to retrieve a Webpage, get all Elements with the class ".heads" and then save specific parts of it to a file. When I run it, I get this...
Jon Skeet
people
quotationmark

The problem is what you've done with the library, basically. You don't need to make it a library - you just download it as a jar file and put that in your classpath. It looks like you've included the source, but you haven't included the... more 2/11/2016 9:51:01 PM

people