All good suggestions.
I haven't toyed with threading all that much other than the Fenceposts extension but it's always a pain in rear to handle it properly. Granted the preview generation happens quite quickly and we haven't seen much in the way of slowdown. (Excepting huge piles of text.)
I'll likely run this by
@Neobeo as I'm not too well versed with threading, especially with async/await. I don't understand how that pattern works at all. Don't have a mental picture of it... (even though I passed the C# certification which includes async/await, I have no idea how to apply it)
Ah, it's pretty easy! I seem to remember asynchronous programming was a headache and a half before this came out. Here's an example:
Say you had a form that needed to download a file from somewhere, and then display the hex bytes for the user to be able to view them, along with some other things in the middle and afterwards. If you had this action happen on a button press, you might do something along the lines of (in pseudo-code):
Code:
byte[] GetNecessaryFile() {
var file = DownloadFile("http://myfilehosted.here/");
return file;
}
void Button_Click(object sender, EventArgs e) {
var bytes = GetNecessaryFile();
// maybe some other actions here
hexViewer.SetBytes(bytes);
// more actions once loaded
}
Depending on the size of the file and the internet speed, the form could hang for quite some time. So, you can implement async / await. You would make each of your functions async by using the async keyword, and make your GetNecessaryFile function return a Task. This will have added benefits, because if you have to do tasks in the meantime that won't hang the main form, you can do it while the file is downloading, and wait to proceed further, like so:
Code:
async Task<byte[]> GetNecessaryFileAsync() {
var file = await DownloadFileAsync("http://myfilehosted.here/"); // will assume this exists because it probably does
return file;
}
async void Button_Click(object sender, EventArgs e) {
var bytes = GetNecessaryFileAsync(); // this is a task, so it will start running but won't return anything yet
// maybe some other actions here that don't depend on those bytes
hexViewer.SetBytes(await bytes); // this causes execution to pause while it waits for the file to download if it isn't done yet
// more actions once loaded that continue after the file is done downloading
}
Does that make sense? I guess the main idea is to use this when you are doing something that will take a long time. I would define long time as causing a noticeable hang on the form (third of a second or so). It's generally used for I/O operations and network communication.