Tuesday, February 1, 2022

C# Questions and answers

 1) What's the difference between Invoke() and BeginInvoke() on delegate?

The answer are as below 

  • Delegate.Invoke: Executes synchronously, on the same thread.
  • Delegate.BeginInvoke: Executes asynchronously, on a threadpool thread.
  • Invoke blocks the caller until the scheduled action finishes.
  • When you use BeginInvoke your loop is going to run super fast since BeginInvoke returns right away. This means that you're adding lot and lots of actions to the message queue. You're adding them much faster than they can actually be processed. This means that there's a long time between when you schedule a message and when it actually gets a chance to be run.
2) How to check a thread status.
Ans:- A Thread class is responsible for creating and managing a thread in multi-thread programming. It provides a property known as IsAlive to check if the thread is alive or not. Or in other words, the value of this property indicates the current execution of the thread.

Syntax:
public bool IsAlive { get; }

// C# program to illustrate the
// use of IsAlive property
using System;
using System.Threading;

public class GFG {

// Main Method
static public void Main()
{
Thread thr;

// Get the reference of main Thread
// Using CurrentThread property
thr = Thread.CurrentThread;

// Display the current state of
// the main thread Using IsAlive
// property
Console.WriteLine("Is main thread is alive"+
" ? : {0}", thr.IsAlive);
}
}

Output:Is main thread is alive ? : True

3) How to subscribe to and unsubscribe from events In C#.

Ans:- 

No comments:

Post a Comment