6 important WPF and Silverlight Multi-threading interview questions with answers


WPF and Silverlight technologies are pretty wanted technologies when it comes to getting job as a c# or a .NET programmer / developers. One of the sections which very confusing is threading. So in this article we will discuss six important WPF and Silverlight questions which are asked in c# interviews from the aspect of threading.

Introduction

WPF and Silverlight technologies are pretty wanted technologies when it comes to getting job as a c# or a .NET programmer / developers. One of the sections which very confusing is threading. So in this article we will discuss six important WPF and Silverlight questions which are asked in c# interviews from the aspect of threading.

Question 1 :- How can we do multi-threading in WPF / Silverlight?

In order to implement proper multi-threading we need to use the dispatcher object.

Question 2 :- Can’t we just use threads, why do we need the dispatcher object?

In WPF / Silverlight objects have thread affinity. In other words the objects can be modified only by the thread that has created the object. Now when any WPF/Silverlight application runs they run on a main thread. So that main thread only has the permission to modify the same.


With out Dispatcher


Question 3 :- But what if try to modify it?


If other threads who have not created the UI object tries to modify the same it will throw an exception “invalid cross-thread access”.


For example in the below code we have a simple function called as “SetValue” , this function modifies a text box. This function is called directly by creating a “Thread” object in a button click event.




private void button1_Click(object sender, RoutedEventArgs e)
{
Thread obj = new Thread(new ThreadStart(SetValue));
obj.Start();
}

public void SetValue()
{
for (int i = 0; i < 10; i++)
{
textBox1.Text = "test";
}
}


Now the UI object “textBox1” is not created by thread “obj”. It’s created by the main thread which runs the WPF/Silverlight UI. So when you run the application you should get an exception as shown in the below “invalid cross-thread access”

WPF multithreaded error

Question 4 :- So how do we make the code working?

To make threading working in WPF and Silverlight we need to use the “Dispatcher” object. So rather than making calls directly to the UI object we need to route the calls via dispatcher object. The dispatcher object then prioritizes and executes the calls on the threads behalf to the UI control.

Disptacher

So to make the above code working we need to call the code from within the dispatcher object. We need to pass a delegate to the “BeginInvoke” function (which is done by using a lambda expression “()=>”) of the “Dispatcher” object.


public void SetValue()
{
for (int i = 0; i < 10; i++)
{
this.Dispatcher.BeginInvoke(() => { textBox1.Text = "Test"; });
}
}


Question 5:- What is a background worker thread?

Background worker is a simple thread which runs at the background and helps us execute process in the background. In order to use background worker class we need to provide two things:-


The first thing we need to provide is the heavy processing method which we want invoke in a multi-threading fashion. For instance the below method “_bWorker_DoWork” has the heavy processing logic. This method is provided to the “DoWork” property of the “BackGroundWorker” class.




readonly BackgroundWorker _bWorker = new BackgroundWorker();

_bWorker.DoWork += new DoWorkEventHandler(_bWorker_DoWork);
void _bWorker_DoWork(object sender, DoWorkEventArgs e)
{
//Heavy processing logic
}


The second thing is you need to invoke method “RunWorkerAsync” so that the “_bWorker_DoWork” method will be executed in the background.


_bWorker.RunWorkerAsync();

Question 6:- What is the difference between background worker and dispatcher?

Dispatcher only queues and executes the multi-threaded request. Dispatcher does not create threads. While background worker actually spawns and executes methods in a multi-threaded manner.



You can visit our site for more such .NET and C# Interview Questions. Here is the link to the site: http://www.questpond.com.


Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: