We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Tuesday, July 21, 2015

Threading and Assembly


1. ) What is the thread ?

A thread is basically a separate sequence of instructions designed to perform a “specific task" in the program.

2. ) What is Multithreading in c#?

Performing multiple tasks at the same time during the execution of a program is known as multithreading.

3. ) What is the Namespace used for multithreading in c#?

using System. Threading;

4. ) What is the advantage of multithreading in c#?

There is two main advantage to use of multithreading in c#.

-Optimize the use of computer resources such as memory.

-Save time

5. ) What are the classes used in the System. Threading Namespace?

Thread

Thread Pool

Monitor

Mutex..etc

6. ) What is the use of Thread class in c#?

The Thread class is used to perform tasks such as creating and setting the priority of a thread.

7. ) What are the main properties of the thread class?


Priority
Thread State
IsAlive
Current thread
Name etc.

8.) What are the methods used in thread class?

Join
Resume
sleep
Spin Wait
Suspended
Start
Interrupt

9.) What is the Thread Pool class in c#?

The Thread Pool class is used, to perform a task such as processing of asynchronous i/o and waiting on behalf of another thread.

10.) What is the method used in the Thread Pool class?

Gettype
Equals
SetMaxThreads
QueueUserWorkItem

11. ) What is monitor class in c#?


The Monitor class is used to access an object by granting a lock for the object to a single thread.

12. ) What are the methods used in the monitor class?

Enter
Exit
TryEnter
Wait
GetType

13. ) What is the Mutex class in c#?

A Mutex is used, to perform interprocess synchronization and a thread to have exclusive access to shared resources.

14. ) What are the methods used in the Mutex class?

Equals
close
OpenExisting
SetAccessControl
Release Mutex

15. ) What is the syntax for creating and starting a thread in c#?

First define a delegate:-
Public delegate void start_thread();
Create a new thread:-
Thread thread_name = new Thread(new start_thread(method_name));

16. ) Can we create a timer in threading?

Yes.

17. ) How can we schedule a thread in c#?

We can schedule the thread with the help of the priority property of the Thread class.

18. ) What is the priority value used for scheduling a thread in c#?

 Name:-It is used to specify a name for a thread.

Priority:-It obtain a value that indicates the scheduling priority of a thread.

Normal:- This value must be scheduled after the threads with Above Normal priority but before the Below Normal priority threads.

Highest:-The Highest value indicates that thread must be scheduled for running before any other threads.

Above Normal:-The Above Normal value indicates that the thread with this value must be scheduled after highest priority threads but before the Normal Priority thread.

Below Normal:-This value indicates that the thread with this value must be scheduled after a Normal Priority value thread but before the Lowest priority thread.

Lowest:- This value indicates that the thread with this value must be scheduled for running after the threads with other priorities such as Normal and Highest.

Thread State:-By default, the value of the thread state properties is unstarted.


19. ) What are the two types of thread in c#?

Foreground thread
Background thread


20. What is the thread life cycle?

 The life cycle of a thread starts when an object of the System.Threading.Thread class is created and ends when the thread is terminated or completes execution.

Following are the various stages in the life cycle of a thread:
· The Unstarted State: It is the situation when the instance of the thread is created but the Start method is not called.
· The Ready State: It is the situation when the thread is ready to run and waiting for the CPU cycle.
· The Not Runnable State: A thread is not executable, when:
· Sleep method has been called
· Wait method has been called
· Blocked by I/O operations
· The Dead State: It is the situation when the thread completes execution or is aborted.

21. What is the main thread?

System.Threading.Thread class is used for working with threads. It allows creating and accessing individual threads in a multithreaded application. The first thread to be executed in a process is called the main thread.

22. What is a join method in the thread?

 Make the current thread wait for another thread to finish.

23. What is Multi-tasking in c#.Net?

Its a feature of modern operating systems with which we can run multiple programs at the same time example Word, Excel, etc.

24. What's Thread.Sleep() in threading?

Thread's execution can be paused by calling the Thread.Sleep method. This method takes an integer value that determines how long the thread should sleep. Example Thread.CurrentThread.Sleep(2000).


25. How can we make a thread sleep for the infinite period?

You can also place a thread into the sleep state for an indeterminate amount of time by callingThread.Sleep(System.Threading.Timeout.Infinite). To interrupt this sleep you can call the Thread.Interrupt method.

26. What is Suspend and Resume in Threading?

It is Similar to Sleep and Interrupts. Suspend allows you to block a thread until another thread calls Thread.Resume. The difference between Sleep and Suspend is that the latter does not immediately place a thread in the wait state. The thread does not suspend until the .NET runtime determines that it is in a safe place to suspend it. Sleep will immediately place a thread in a wait state. 
Note:- In threading interviews, most people get confused with Sleep and Suspend. They look very similar.


27. What are Daemon thread's and how can a thread be created as Daemon?

Daemon thread's run in the background and stop automatically when nothing is running program. An example of a Daemon thread is "Garbage collector".The garbage collector runs until some .NET code is running or else it's idle. You can make a thread Daemon by Thread.Isbackground=true.


28. What is the use of Interlocked class?

The interlocked class provides methods by which you can achieve following functionalities:-
v increment Values.
v Decrement values.
v Exchange values between variables.
v Compare values from any thread. in a synchronization mode.
Example :- System.Threading.Interlocked.Increment(IntA)


29. How can we know a state of a thread in c#.Net?


"ThreadState" property can be used to get detail of a thread.Thread can have one or a combination of status.System.Threading.Threadstate enumeration has all the values to detect a state of thread.Some sample states are Is running, IsAlive, suspended, etc.

30.what is ManualResetEvent and AutoResetEvent ?

Threads that call one of the wait methods of a synchronization event must wait until another thread signals the event by calling the Set method. There are two synchronization event classes. Threads set the status of ManualResetEvent instances to signaled using the Set method. Threads set the status of ManualResetEvent instances to nonsignaled using the Reset method or when control returns to a waiting WaitOne call. Instances of the AutoResetEvent class can also be set to signal using Set, but they automatically return to unsignaled as soon as a waiting thread is notified that the event became signaled.


31. What is ReaderWriter Locks?

You may want to lock a resource only when data is being written and permit multiple clients to simultaneously read data when data is not being updated. The ReaderWriterLock class enforces exclusive access to a resource while a thread is modifying the resource, but it allows nonexclusive access when reading the resource. ReaderWriter locks are a useful alternative to exclusive locks that cause other threads to wait, even when those threads do not need to update data.


32. How can you avoid deadlock in threading in c#.Net?

Good and careful planning can avoid deadlocks. There so many ways Microsoft has provided by which you can reduce deadlocks example Monitor, Interlocked classes, Wait for handles, Event raising from one thread to other thread , ThreadState property which you can poll and act accordingly, etc.

33. What’s the difference between thread and process?


A thread is a path of execution that runs on CPU, a process is a collection of threads that share the same virtual memory. A process has at least one thread of execution, and a thread always runs in a process context.
 Note:- It's difficult to cover the threading interview question in this small chapter. These questions can take only to a basic level. If you are attending interviews where people are looking for threading specialist, try to get more deeply into synchronization issues as that's the important point they will stress.


34. What is the signature of the constructor of a thread class?

Thread(Runnable threadob,String threadName)

35. What are all the methods used for Inter Thread communication and what is the class in which these methods are defined?

1. wait(),notify() & notifyall()
2. Object class

36. What are the two types of multitasking?

1.process-based
2.Thread-based

37. What is the data type for the method isAlive() and this method is available in which class?

boolean, Thread

How to refer to the current thread of a method in c#.NET?

In order to refer to the current thread in .NET, the Thread.CurrentThread method can be used. It is public static property.

38. Component Object Model (COM)

Ans: Component Object Model (COM) is a method to facilitate communication between different applications and languages. COM is used by developers to create re-usable software components, link components together to build applications, and take advantage of Windows services. COM objects can be created with a variety of programming languages. Object-oriented languages, such as C++, provide programming mechanisms that simplify the implementation of COM objects. The family of COM technologies includes COM+, Distributed COM (DCOM) and ActiveXĂ‚® Controls.


39. What is DCOM(Distributed COM)?

Ans: The DCOM programming model enables you to display COM components over a network and easily distribute applications across platforms. DCOM components also help in two-tier client/server applications. These models also have some drawbacks that help the development of the COM+ approach

40. What is RCW (Runtime Callable Wrapper)?

Ans: RCW Means Runtime Callable Wrappers, The Common Language Runtime (CLR) exposes COM objects through a proxy called the Runtime Callable Wrapper (RCW). Although the RCW appears to be an ordinary object to .NET clients, it's primary function is to marshal calls between a .NET client and a COM object.

41. What is Full Assembly Reference?

A full assembly reference includes the assembly’s text name, version, culture, and public key token (if the
assembly has a strong name). A full assembly reference is required if you reference any assembly that is
part of the common language runtime or any assembly located in the global assembly cache

42. What is Partial Assembly Reference?

We can dynamically reference an assembly by providing only partial information, such as specifying only the assembly name. When you specify a partial assembly reference, the runtime looks for the assembly only in the application directory

. Types of friend assembly?

Unsigned friend assembly

signed friend assembly?

43. what are the two attributes used by friend assembly?

1. InternalsVisibleToAttribute

2. StrongNameIdentityPermission

44. Mehtods for dynamically loading an assembly?

Load() ,


45. What is a static assembly?

Static Assemblies are those Assemblies that are stored on the disk permanently. They may include .NET Framework classes, interfaces as well as a resource file. These assemblies are not loaded directly from the memory instead they are directly loaded from the disk when CLR (Common Language RunTime) requests for them. These Assemblies used to store on the disk as a file or set of file. Whenever one compiles the C# code, one gets STATIC assemblies.

46. What is dynamic assembly?

Dynamic assemblies are those assemblies that are not stored on the disk before execution in fact after execution they get stored on the disk. When .NET runtime calls them they are directly loaded from the memory not from the disk. Reflection emit provides many ways to create dynamic assemblies means These are created in the memory using System.Reflection.emit namespace.The System.Reflection.Emit namespace contains classes that allow a compiler or tool to emit metadata and Microsoft intermediate language (MSIL) and optionally generate a PE file on disk. When an application requires the types within these assemblies these dynamic assemblies are created dynamically at run time

47. What is an assembly?


Assemblies are the basic building blocks required for any application to function in the .NET realm. They have partially compiled code libraries that form the fundamental unit of deployment, versioning, activation scoping, reuse, and security. Typically, assemblies provide a collection of types and resources that work together to form a logical unit of functionality. They are the smallest deployable units of code in .NET. Compared to the executable files assemblies are far more reliable, more secure, and easy to manage. An assembly contains a lot more than the Microsoft Intermediate Language (MSIL) code that is compiled and run by the Common Language Runtime (CLR). In other words, you can say that an assembly is a set of one or more modules and classes compiled in MSIL and metadata that describes the assembly itself, as well as the functionalities of the assembly classes.

48. Name the different components of an assembly.

An assembly is a logical unit that is made up of the following four different types of components:
Assembly manifest
MSIL source code
Type metadata
Resources

49. What are the different types of assemblies? Explain them in detail.

The following are the two types of assemblies:
Private Assembly - Refers to the assembly that is used by a single application. Private assemblies are kept in a local folder in which the client application has been installed.
Public or Shared Assembly - Refers to the assembly that is allowed to be shared by multiple applications. A shared assembly must reside in the Global Assembly Cache (GAC) with a strong name assigned to it.

For example, imagine that you have created a DLL containing information about your business logic. This DLL can be used by your client application. In order to run the client application, the DLL must be included in the same folder in which the client application has been installed. This makes the assembly private to your application. Now suppose that the DLL needs to be reused in different applications. Therefore, instead of copying the DLL in every client application folder, it can be placed in the global assembly cache using the GAC tool. These assemblies are called shared assemblies.

50. Can one DLL file contain the compiled code of more than one .NET language?

No, a DLL file can contain the compiled code of only one programming language.

51. What is the maximum number of classes that can be contained in a DLL file?

There is no limit to the maximum number of classes that can be contained in a DLL file.

52. What is a satellite assembly?

Satellite assemblies are assemblies that are used to deploy language and culture specific resources for an application. In an application, a separate product ID is assigned to each language and a satellite assembly is installed in a language specific sub-directory.

53. Is versioning applicable to private assemblies?

No, versioning is not applicable to private assemblies as these assemblies reside in their individual folders. Versioning can be applied to GAC only.

54. What is metadata?

Assembly metadata describes every data type and member defined in the code. It stores the description of an assembly, such as name, version, culture, a public key of an assembly along with the types exported, other assemblies dependent on this assembly, and security permissions needed to run the application. In addition, it stores the description of types, such as the name, visibility, base class, interfaces implemented, and members, such as methods, fields, properties, events, and nested types.

It also stores attributes. Metadata is stored in binary format. Therefore, metadata of an assembly is sharable among applications that execute on various platforms. It can also be exported to other applications to give information about the services and various features of an application.

55. What is Delay signing?

To create a strongly named assembly and to make sure that this assembly can be used by someone else, we partially build this assembly by providing a Public Key. We write this Public Key in the AssemblyInfo.vb OR .cs file. We also add an attribute by the name <Assembly: AssemblyDelaySignAttribute(true)> to the assembly info file. This makes it sure that when we build the assembly, it would be containing the information only about the public key before we deliver it to our clients. This is a partial strong named assembly that we have created, and hence it is called Delayed Assembly.

56. How to prevent my .NET DLL to be decompiled?

We can prevent .NET DLL to be decompiled up to an extent by Obfuscate Source code, asymmetric encryption, and encrypted w32 wrapper application.

57. What is the Code Document Object Model (CodeDom)?

Code Document Object Model is code generators that are used to minimize repetitive coding tasks and to minimize the number of human-generated source code lines.

58. What is the Assembly Manifest?

Assemblies maintain all their information in a special unit called the manifest. Every assembly has a manifest.

The followings are the contents of an Assembly Manifest:
Assembly name - Represents a text string that specifies the assembly's name.
Version number - Represents a major and minor version number, as well as a revision and build number. The CL.R makes use of these numbers to enforce version policy.
Culture - Represents information of the culture or language, which the assembly supports. An assembly is a container of only resources containing culture- or language-specific information.
Strong name information - Represents the public key from the publisher, if a strong name is assigned to an assembly.
List of all files in the assembly - Represents a hash of each file contained in the assembly and a file name.
Type reference information - Represents the information used at the runtime to map a type reference to the file that contains its declaration and implementation.
Information on referenced assemblies - Represents a list of other assemblies that are statically referenced by the assembly. Each reference includes the names of dependent assemblies, assembly metadata (version, culture, operating system, and so on), and public key, if the assembly is strongly named.

59. What is the value of the Copy Local property when you add an assembly in the GAC?

False.

60. What is the Native Image Generator?

The Native Image Generator (Ngen.exe) is a tool that creates a native image from an assembly and stores that image to native image cache on the computer. Whenever an assembly is run, this native image is automatically used to compile the original assembly. In this way, this tool improves the performance of the managed application by loading and executing an assembly faster.

Note that native images are files that consist of compiled processor-specific machine code. The Ngen.exe tool installs these files on to the local computer.

61. Name the MSIL Disassembler utility that parses any .NET Framework assembly and shows the information in a human readable format


The Ildasm.exe utility.

62. What is the significance of the Strong Name tool?

The Strong Name utility (sn.exe) helps in creating unique public-private key pair files that are called strong name files and signing assemblies with them. It also allows key management, signature generation, and signature verification.

63. How can different versions of private assemblies be used in the same application without a re-build?

You can use different versions of private assemblies in the same application without a re-build by specifying the assembly version in the AssemblyInfo.cs or AssemblyInfo.vb file.

64. What is the Global Assembly Cache (GAC)?

GAC is a central repository (cache) in a system in which assemblies are registered to share among various applications that execute on local or remote machines. .NET Framework provides the GAC tool (gacutil.exe utility), which is used to view and change the content of the GAC of a system. Adding new assemblies to GAC and removing assemblies from GAC are some of the tasks that can be performed by using the gacutil.exe utility. GAC can contain multiple versions of the same .NET assembly. CLR checks GAC for a requested assembly before using the information on configuration files.

The gacutil.exe /i <assembly name> - is the command that is used to install an assembly in GAC. Users use the Command Prompt of Visual Studio to install an assembly in GAC by using this command.

You can see all the assemblies installed in the GAC using the GAC viewer, which is located at the <WinDrive>:<WinDir>\assembly directory, where <WinDir> is windows in Windows XP or windows in Windows Vista or WinNT in Windows 2000. Apart from the list of assemblies, the assembly viewer also shows relevant information, such as the global assembly name, version, culture, and the public key token.

65. Where is the information regarding the version of the assembly stored?

Information for the version of the assembly is stored inside the assembly manifest.

50. Discuss the concept of strong names.

Whenever an assembly is deployed in GAC to make it shared, a strong name needs to be assigned to it for its unique identification. A strong name contains an assembly's complete identity - the assembly name, version number, and culture information of an assembly. A public key and a digital signature, generated over the assembly, are also contained in a strong name. A strong name makes an assembly identical to GAC.

66. What is the difference between.EXE and.DLL files?

EXE
It is an executable file, which can be run independently.
EXE is an out-process component, which means that it runs in a separate process.
It cannot be reused in an application.
It has a main function.

DLL
It is a Dynamic Link Library that is used as a part of EXE or other DLLs. It cannot be run independently.
It runs in the application process memory, so it is called an in-process component.
It can be reused in an application.
It does not have a main function.

67. Which utility allows you to reference an assembly in an application?

An assembly can be referenced by using the gacutil.exe utility with the /r option. The /r option requires a reference type, a reference ID, and a description.

68. The AssemblyInfo.cs file stores the assembly configuration information and other information, such as the assembly name, version, company name, and trademark information. (True/False).

True.

69. Can 2 different applications use the same dll in GAC at the same time?

Yes. two applications can use the same dll. that is why we have shared assemblies and place it in the GAC.

70. How assemblies are implemented?
Assemblies can take the form of an executable (.exe) file or dynamic link library (.dll) file.

71. What are the different components of strong naming?

A strong name consists of
1. Name of the assembly
2. Public key token
3. Optionally any resources
4. Version Number

72. What is a strong naming in the case of assembly? 

There is always a possibility that they may use the same assembly name as your assembly name. If such types of assemblies are placed in the GAC then we cannot uniquely identify a particular assembly. By using strong naming we can give the assembly a unique name.

73.What the physical location of assembly?

c:\Windows\assembly

74.Different types of Assembly?

a)Private Assembly

An assembly used by a single application is called a private assembly. It's present in the bin folder.

b) SharedAssembly

An assembly used by more than one application. It's present in GAC.

c)Satellite Assembly

These are resource files which are compiled to assemblies.

75. What is a PE file?

Portable Executable File is either a DLL or an EXE. It is generated when a program is compiled. It consists of 3 part-

PE Header
MSIL
Metadata

76. What is ILDASM?

Its a tool used to view the contents of an assembly.

77. How do install and uninstall assembly in GAC?


install- gacutil -i <assembly name>

uninstall-gacutil -u <assembly name>




No comments: