Write code to use threading and the lock keyword. 

1 Answer

Answer :

using System;

using System.Threading;

namespace ConsoleApplication4

{

class Class1

{

[STAThread]

static void Main(string[] args)

{

ThreadClass tc1 = new ThreadClass(1);

ThreadClass tc2 = new ThreadClass(2);

Thread oT1 = new Thread(new

ThreadStart(tc1.ThreadMethod));

Thread oT2 = new Thread(new

ThreadStart(tc2.ThreadMethod));

oT1.Start();

oT2.Start();

Console.ReadLine();

}

}

class ThreadClass

{

private static object lockValue = "dummy";

public int threadNumber;

public ThreadClass(int threadNumber)

{

this.threadNumber = threadNumber;

}

public void ThreadMethod()

{

for (;;)

{

lock(lockValue)

{

Console.WriteLine(threadNumber + " working");

Thread.Sleep(1000);

}

}

}

}

}

Related questions

Description : Why is the virtual keyword used in code?

Last Answer : The Virtual keyword is used in code to define methods and the properties that can be overridden in derived classes.

Description : What keyword would you use for scope name clashes?

Last Answer : this

Description : Define what is the use of the abstract keyword?

Last Answer : The modifier abstract is a keyword used with a class, to indicate that this class cannot itself have direct instances or objects, and it is intended to be only a ‘base’ class to other classes.

Description : Why use the keyword “const” in C#? Give an example.

Last Answer : “Const” keyword is used for making an entity constant. We can’t reassign the value to constant. Eg: const string _name = “Test”;

Description : What does the keyword “virtual” declare for a method or property? 

Last Answer : The method or property can be overridden. 

Description : What is the volatile keyword used for?

Last Answer : It indicates a field can be modified by an external entity (thread, OS, etc.). 

Description : Class methods to should be marked with what keyword?

Last Answer : static

Description : Methods must declare a return type, what is the keyword used when nothing is returned from the method?

Last Answer : void 

Description : What does the keyword “virtual” declare for a method or property? 

Last Answer : The method or property can be overridden.

Description : What does the keyword virtual mean in the method definition? 

Last Answer : The method can be over-ridden. 

Description : Define what is the difference between string keyword and System. String class?

Last Answer : String keyword is an alias for System. String class. Therefore, the System. String and string keyword are the same, and you can use whichever naming convention you prefer. The String class provides many methods for safely creating, manipulating, and comparing strings.

Description : Explain the “static” keyword in C#?

Last Answer : “Static” keyword can be used for declaring a static member. If the class is made static then all the members of the class are also made static. If the variable is made static then it will have a single instance and the value change is updated in this instance.

Description : Write code to define and use your own custom attribute.

Last Answer : (From MSDN) // cs_attributes_retr.cs using System; [AttributeUsage(AttributeTargets.Class|AttributeTargets.Struct, AllowMultiple=true)] public class Author : Attribute { public Author(string name) { this.name ... double version; string name; public string GetName() { return name; } }

Description : Write some code to declare and use properties.

Last Answer : // instance public string InstancePr { get { return a; } set { a = value; } } //read-only static public static int ClassPr { get { return b; } }

Description : Write some code that declares an array on ints, assigns the values: 0,1,2,5,7,8,11 to that array and use a foreach to do something with those values.

Last Answer : int x = 0, y = 0; int[] arr = new int [] {0,1,2}; foreach (int i in arr) { if (i%2 == 0) x++; else y++; }

Description : Write some code to use a delegate. 

Last Answer : Member function with a parameter using System; namespace Console1 { class Class1 { delegate void myDelegate(int parameter1); static void Main(string[] args) { MyClass myInstance = new ... void AMethod(int param1) { Console.WriteLine(param1); } } }

Description : Write some code to implement an indexer.

Last Answer : using System; namespace Console1 { class Class1 { static void Main(string[] args) { MyIndexableClass m = new MyIndexableClass(); Console.WriteLine(m[0]); Console.WriteLine(m[1]); Console.WriteLine(m[2]); Console. ... get { return myData[i]; } set { myData[i] = value; } } } }

Description : Write some code that uses an ArrayList.

Last Answer : ArrayList list = new ArrayList(); list.Add("Hello"); list.Add("World");

Description : Write some code to implement a jagged array. 

Last Answer : // Declare the array of two elements: int[][] myArray = new int[2][]; // Initialize the elements: myArray[0] = new int[5] {1,3,5,7,9}; myArray[1] = new int[4] {2,4,6,8};

Description : Write some code to implement a multidimensional array.

Last Answer : int[,] b = {{0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}}; 

Description : Write some code for a custom collection class.

Last Answer : using System; using System.Collections; public class Items : IEnumerable { private string[] contents; public Items(string[] contents) { this.contents = contents; } public IEnumerator GetEnumerator() { ... (string item in items) { Console.WriteLine(item); } Console.ReadLine(); } }

Description : Write some code for do… while.

Last Answer : int x; int y = 0; do { x = y++; Console.WriteLine(x); } while(y < 5);

Description : Write some code for a while loop. 

Last Answer : int n = 1; while (n < 6) { Console.WriteLine("Current value of n is {0}", n); n++; }

Description : Write some code for a for loop 

Last Answer : for (int i = 1; i <= 5; i++) Console.WriteLine(i);

Description : Write some if… else if… code.

Last Answer : int n=4; if (n==1)  Console.WriteLine("n=1"); else if (n==2) Console.WriteLine("n=2"); else if (n==3) Console.WriteLine("n=3"); else Console.WriteLine("n>3");

Description : Write some try…catch…finally code.

Last Answer : // try-catch-finally using System; public class TCFClass { public static void Main () { try { throw new NullReferenceException(); } catch(NullReferenceException e) { Console.WriteLine("{0} exception 1 ... ("exception 2."); } finally { Console.WriteLine("finally block."); } } }

Description : Write some code to overload an operator. 

Last Answer : class TempleCompare { public int templeCompareID; public int templeValue; public static bool operator == (TempleCompare x, TempleCompare y) { return (x.templeValue == y.templeValue); } ... templeValue); } public override int GetHashCode() { return templeCompareID; } }

Description : Write code for a case statement. 

Last Answer : switch (n) { case 1: x=1; break; case 2: x=2; break; default: goto case 1; }

Description : Write code for an enumeration. 

Last Answer : public enum animals {Dog=1,Cat,Bear}; 

Description : Write some code to box and unbox a value type. 

Last Answer : // Boxing int i = 4; object o = i; // Unboxing i = (int) o;

Description : Write code to show how a method can accept a varying number of parameters.

Last Answer : using System; namespace Console1 { class Class1 { static void Main(string[] args) { ParamsMethod(1,"example"); ParamsMethod(1,2,3,4); Console.ReadLine(); } static void ... o in list) { Console.WriteLine(o.ToString()); } } } }

Description : Write some code using interfaces, virtual methods, and an abstract class.

Last Answer : using System; public interface Iexample1 { int MyMethod1(); } public interface Iexample2 { int MyMethod2(); } public abstract class ABSExample : Iexample1, Iexample2 { public ABSExample( ... override void VIRTMethod2() { System.Console.WriteLine("VIRTMethod2 has been overridden"); } }

Description : Describe how to use code to retrieve resources at run time.

Last Answer : You must first create an instance of the ResourceManager class that is associated with the assembly that contains the desired resource. You can then use the GetString method to retrieve string resources or the GetObject method to retrieve object resources.

Description : Briefly highlight the differences between imperative and declarative security as they pertain to code access security.

Last Answer : Imperative security is implemented by calling methods of Permission objects in code at run time. Declarative security is configured by attaching attributes representing permissions to classes ... request assembly-wide permissions using the Assembly (assembly) directive with declarative security.

Description : Explain how to convert data in legacy code page formats to the Unicode format.

Last Answer : You can use the Encoding.Convert method to convert data between encoding types. This method requires instances of both encoding types and an array of bytes that represents the data to be converted. ... GetBytes method and can convert an array of bytes back to chars with the Encoding.GetChars method.

Description : Describe how to retrieve the ASCII key code from a keystroke. How would you retrieve key combinations for non-ASCII keys?

Last Answer : Keystrokes can be intercepted by handling the KeyPress and KeyDown events. Both of these events relay information to their handling methods in the form of their EventArgs. The KeyPressEventArgs, ... you would handle the KeyDown event and use the properties exposed by the KeyEventArgs instance.

Description : What is unsafe code?

Last Answer : Unsafe code bypasses type safety and memory management.  

Description : Define what is a code group?

Last Answer : A code group is a set of assemblies that share a security context.

Description : How can I remove 4 stripped screws from computer panel without damaging computer, including threading?

Last Answer : Go to the hardware store and get a reverse-tap drill bit. It’ll work like a charm.

Description : Does Threading Your Eyebrows Hurt?

Last Answer : Wtf is “threading your eyebrows?”

Description : Anyone here know anything about eyebrow threading?

Last Answer : There was an article on this in the NY Times a while back, and you might be able to find it in the archives. What I remember is that the process is very fast and good.

Description : Drilling, milling and threading are examples of which kind of processes? a.Additive manufacturing b.Subtractive manufacturing c.Formative manufacturing d.None of the above

Last Answer : b.Subtractive manufacturing

Description : In a CNC program block, N05 GO1 G91 G33 X20 Z-40 K5……, K refer to a.Depth of cut b.Pitch c.Multiple Threading Cycle d.Feed

Last Answer : b.Pitch

Description : Which of the following are made out of the carbon steel having carbon content of 0.9 to 1%? (A) Small punches, broaches reamers and springs (B) Cutlery, screws, rivets and files (C) ... lathe tools and razors (D) Forgings like can shaft, structural steel plate, threading dies and drawing dies

Last Answer : (A) Small punches, broaches reamers and springs

Description : Optimum penetration of a .024 or .031 inch self-threading pin in dentin is . . . a. 0.5 mm b. 1.0 mm c. 2.0 mm d. 2.5 mm e. 3.0 mm

Last Answer : c. 2.0 mm

Description : .................. refers to the ability of an operating system to support multiple threads of execution with a single process. A) Multithreading B) Multiprocessing C) Multiexecuting D) Bi-threading

Last Answer : A) Multithreading

Description : How would you read and write using the console?

Last Answer : Console.Write, Console.WriteLine, Console.Readline 

Description : Write a hello world console application. 

Last Answer : using System; namespace Console1 { class Class1 { [STAThread] // No longer needed static void Main(string[] args) { Console.WriteLine("Hello world"); } } }

Description : C# provides a default constructor for me. I write a constructor that takes a string as a parameter, but want to keep the no parameter one. How many constructors should I write? 

Last Answer : Two. Once you write at least one constructor, C# cancels the freebie constructor, and now you have to write one yourself, even if there’s no implementation in it.

Description : Explain how to use the PrintPreviewDialog control to display a preview of a printed document before it is printed.

Last Answer : You display a preview of a printed document with the PrintPreviewDialog control by first setting the PrintDocument property of the PrintPreviewDialog instance to the PrintDocument you want to preview, then by calling the PrintPreviewDialog.Show command to display the dialog box.