InternetException
About coding and whatnot
Notes on .NET, C#, self-hosted LLMs, Android and whatever else I break on the way. Written since 2012, migrated from WordPress to a few static files. Who is writing this?
async/await in C#
async/await syntax should be used for I/O bound asynchronous operations only.
public async Task<int> GetUrlContentLengthAsync()
{
var client = new HttpClient();
Task<string> getStringTask =
client.GetStringAsync("https://docs.microsoft.com/dotnet");
DoIndependentWork();
string contents = await getStringTask;
return contents.Length;
}
Compiler transforms code into a state machine that to track yielding execution for await and resuming execution when a background job is finished.
Things to keep in mind:
asyncmethod needs await- add
Asyncsuffix to asynchronous methods async voidfor ‘Fire and Forget’ work i.e. event Handlers- be aware of mixing LINQ with
asynclambdas - wait for tasks in a non-blocking manner (do not use
Task.Resulti.e.) - for tight loops use
ValueTasks - Consider using
ConfigureAwait(false)for performance and for avoiding deadlocks
Further read:
CPU bound vs I/O bound
There are two types of asynchronous operations in .NET.
- I/O bound
- CPU bound
For I/O bound async/await syntax should be used. TPL should not be used for this. async methods does not spawns new thread to hand le I/O work asynchronously.
For CPU bound operations TPL should be used (Task.Run). This spawns new thread. Performance testing should be done to check if overhead of new thread and context switching is not greater than actual work to be done.
SupressFinalize in IDisposable interface
- informs GC that object does not need to be added to finalizer queue
- finalizer execution is not deterministic and objects can live for long time in queue - this is optimization technique
- to make sure that object disposes of any unamanaged resources even if
Disposewill be not called we need to implement finalizer
Basically finalizer execution time is not deterministic. We cannot predict when this will be called and till that time - object is existing and still taking memory space. Since we are controlling creation and disposal of those resources we can say to GC: ‘Do not worry about that! It already had been taken care of!’. So this way object will be destroyed sooner, leaving this space to be used by something else possibly.
IDisposable interface pattern
Example implementation of IDisposable
public class MyResource : IDisposable
{
public MyResource(IntPtr handle)
{
this.handle = handle;
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if(!this.disposed)
{
if(disposing)
{
component.Dispose();
}
CloseHandle(handle);
handle = IntPtr.Zero;
disposed = true;
}
}
~MyResource()
{
Dispose(disposing: false);
}
}
MyResource object have pointer which is unmanaged. This is why this class should implement IDisposable interface. Implementation should be correct according to recommended pattern (see below for documentation).
There a re few points to highlight here:
General Interview Questions
What is DDD
What is BDD
What is TDD
What is asynchronous programming?
- software often waits for some I/O operation to complete (file operation, network call, database query etc.)
- we can use this time for something productive instead to wait for data to be available or I/O operation to be completed
- with asynchronous programming user can work in the meantime
- not waiting for I/O operations synchronously enhances user experience
Of course this problem is much more complex. Modern software operates on much more different PC architecture than 20 years ago. Nowadays software must be asynchronous to appear more user friendly and to made better use of modern multi CPU architecture. Right now complex, professional software uses multiple processes, threads on multiple CPUs (CPU you buy is not really *single* CPU) or even utilize GPU for more complex computations (like i.e. video editing software). Before that we mostly had single threaded desktop applications that had ‘please wait’ or ’loading’ pop ups. Right now this is song of the past really and to achieve better User Interfaces that allow to edit tag of your post without changing whole post. With mobile devices on the rise and sometimes lousy cellular network those kind of designs would not be sensible or sometimes impossible to implement. Did you ever happened to reenter some chunk of data into some software because you lost it? Maybe network was down? Or you hit back button by mistake? Right now it belongs to past mostly. All of those would be hard to do with synchronous operations. Right now people are bit spoiled and they tend to leave the page/application if it appears to be slow and not every company can sell ‘one-in-the-market-necessary-for-your-work-software’ that you will be forced to use. And this is why we have asynchronous operations and this is why we have asynchronous programming model that allows us to program such operations.
Dynamic runtime delegates conversion
Introduction
Delegates can be converted easily to another type if you know types of those delegates. But if you do not know it is impossible without really complicated code.
Automatic scrolling to edited text box in Xamarin Android.
Important note: this should work well with newer version of Xamarin (2.3.3). If you cannot update for some reason this article will explain workaround.
Introduction
In previous article I explained how to detect show and hide events of software keyboard in Xamarin Android. I did it in my project because of the fact that some of text boxes was hidden by software keyboard that suppose to edit contents of those text boxes. In this article I will explain how to work around this issue in some older versions of Xamarin.
Detecting software keyboard events in Xamarin Android
#delegates #dependency injection #design patterns
Introduction
It is quite strange that you can’t easily detect if software keyboard is visible or not on Android. Because of that, it is also complicated thing to do in Xamarin.
Xamarin Android Services Pattern on GPS example
#design patterns #XAML #dependency injection
Introduction
In Xamarin if you want to use many Android native service/mechanism you have to implement it in Activity class. Or at least that is how Xamarin documentation wants you to do it.
Xamarin with TinyIoC
#XAML #dependency injection #Xamarin
Introduction
This article is builds on topics and solutions introduces in ViewFactory for Xamarin application and Xamarin Master-Detail Page articles. It shows method for easier navigation and use of services via IoC in Xamarin application. Knowledge from previous examples is not necessary, but makes this article bit easier to understand.