C# The calling thread cannot access this object because a different thread owns it.

One of the common problems encountered during multi-threading is when you try to set a Windows Control value from a different thread. I also faced this issue while implementing the following code:

private async void TestAsyncMethodVoid()
{
    System.Net.Http.HttpClient hClient = new System.Net.Http.HttpClient();
    string content = String.Empty;
    try
    {
        content = await hClient.GetStringAsync("http://www.microsoft.com").ConfigureAwait(false);
    }
    catch (Exception ex)
    {
        content = ex.Message;
    }
    btn00.Content = content;
}

btn00 is a button control whose value I was trying to set here using async/await as the value was coming over a network call. You may have some other case scenarios also where you’d want to set a control’s property directly from the thread. So to do that we’re gonna use, Dispatcher.Invoke method.

It executes the specified delegate synchronously on the thread the Dispatcher is associated with. Look at the following code with correction:

private async void TestAsyncMethodVoid()
{
	System.Net.Http.HttpClient hClient = new System.Net.Http.HttpClient();
	string content = String.Empty;
	try    
	{
		content = await hClient.GetStringAsync("http://www.microsoft.com").ConfigureAwait(false);
	}
	catch (Exception ex)
	{
		content = ex.Message;
	}
	this.Dispatcher.Invoke(() =>
	{
		btn00.Content = content;
	});
}

This code now works without any glitches.

Stop/Prevent Browser from Caching the current Page

protected void StopPageCaching()
{
    HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
    HttpContext.Current.Response.Cache.SetValidUntilExpires(false);
    HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    HttpContext.Current.Response.Cache.SetNoStore();
}

Difference between Covariance and Contravariance delegates in .NET

When you assign a method to a delegate, the method signature does not have to match the delegate exactly. This is called covariance and contravariance.

Covariance

Covariance makes it possible that a method has a return type that is more derived than that defined in the delegate.

public delegate TextWriter CovarianceDel();
public StreamWriter MethodStream() { return null; }
public StringWriter MethodString() { return null; }
CovarianceDel del;
del = MethodStream;
del = MethodString;

Because both StreamWriter and StringWriter inherit from TextWriter, you can use the CovarianceDel with both methods. However, the other way round is not possible.

Contravariance

Contravariance permits a method that has parameter types that are less derived than those in the delegate type.

void DoSomething(TextWriter tw) { }
public delegate void ContravarianceDel(StreamWriter tw);
ContravarianceDel del = DoSomething;

The method DoSomething takes TextWriter as its parameter. Since, StreamWriter is derived from TextWriter, we can also pass the object of StreamWriter to the method DoSomething through this delegate instead of TextWriter.

More Info:- http://blogs.msdn.com/b/ericlippert/archive/tags/covariance+and+contravariance/