Quick Notes for easy understanding. Chapters are on C#.Net,Linq,OOPS,Design Patterns,UML,Tools for development, Databases and many others. - Sid
Saturday, May 21, 2016
Database Answer
--
With Regards-
Siddu
Please consider the environment before printing this e-mail
Wednesday, May 18, 2016
Filter an ObservableCollection with a CollectionView in C#
http://jacobmsaylor.com/?p=1270
Monday, May 16, 2016
How do you pass arguments to a Thread that you create
- static void Main()
- {
- String m = "yeahh!";
- Thread thread = new Thread ( () => PrintToConsole (m) );
- thread.Start();
- }
- static void PrintToConsole (string message)
- {
- Console.WriteLine (message);
- }
In the above example we have effectively passed m into the thread function. Any number of parameters can be passed this way.
2. Another way is to use the overloaded Thread.Start function which accepts ParamterizedThreadStart.
- string parameter = "xyz";
- Thread thread = new Thread(new ParameterizedThreadStart(PrintToConsole));
- thread.Start(parameter);
- private void PrintToConsole(object o)
- {
- String m = (String) o;
- Console.WriteLine(m);
- }
3. This is the same as (1) but the example is for .Net 4.0 Tasks
- Task.Factory.StartNew(() => SomeMethod(p1,p2,p3));
Thread Pool
Wednesday, April 27, 2016
Dependency Injection
This pattern we use to break the strongly dependency across the class level.
Say, you have Class which does Database operation inside this class you want some logger to perferm like logging the debug message into the flat file.
var log = new Logger();
It is fine up to the day later sometime we have bunch of loggers available like logging Server,console or TCP/IP etc.
Of course we do not want to change all code and replace all lines with
var logger = new Logger();
by
var logger = new TcpLogger();
It is quite good idea to introduce an Interface ILog that is implemented by all the various loggers.
ILog logger = new Logger();
or
ILog logger = new TCPLogger();
or
ILog logger = new ConsoleLogger();
Now the type interface doesn't change type any more, we always keep single interface across.
interface ILog { void Log(string msg); } class Logger : ILog { public void Log(string msg) { //write to file. } } class TCPLogger : ILog { public void Log(string msg) { //write to Server - TCP/IP. } } class ConsoleLogger : ILog { public void Log(string msg) { //write to Console. } } class Database { ILog _ilog; public Database(ILog ilog) { // Dependency Injection. _ilog = ilog; } public void ExecuteNonQuery(string sql) { //do database operaion _ilog.Log("some Database operation happened"); } } Marshaling
Creating a bridge between the Managed code and unmanaged code . Which carries messages from the managed to the unmanaged environment and viseversa provided by CLR.
Why Marshaling?
You already know that there is no such compatibility between managed and unmanaged environments. In other words, .NET does not contain such the types HRESULT, DWORD, and HANDLE that exist in the realm of unmanaged code. Therefore, you need to find a .NET substitute or create your own if needed. That is what called marshaling.
An example is the unmanaged DWORD; it is an unsigned 32-bit integer, so we can marshal it in .NET as System.UInt32. Therefore, System.UInt32 is a substitute for the unmanaged DWORD. On the other hand, unmanaged compound types (structures, unions, etc.) do not have counterparts or substitutes in the managed environment. Thus, you'll need to create your own managed types (structures/classes) that will serve as the substitutes for the unmanaged types you use.
When I Need to Marshal?
Marshaling comes handy when you are working with unmanaged code, whether you are working with Windows API or COM components. It helps you interoperating (i.e. working) correctly with these environments by providing a way to share data between the two environments. Figure 1 shows the marshaling process, where it fall, and how it is required in the communication process between the two environments.
Monday, April 25, 2016
Invoke() and BeginInvoke()
What's the difference between Invoke() and BeginInvoke()
Delegate.Invoke/BeginInvoke and Control.Invoke/BeginInvoke
Delegate.Invoke: Executes synchronously, on the same thread.
Delegate.BeginInvoke: Executes asynchronously, on a threadpool thread.
Control.Invoke: Executes on the UI thread, but calling thread waits for completion before continuing.
Control.BeginInvoke: Executes on the UI thread, and calling thread doesn't wait for completion.
A logical conclusion is that a delegate you pass to Invoke() can have out-parameters or a return-value, while a delegate you pass to BeginInvoke() cannot (you have to use EndInvoke to retrieve the results).
new Thread(foo).Start(); private void foo() { this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate() { myTextBox.Text = "bing"; Thread.Sleep(TimeSpan.FromSeconds(3)); }); MessageBox.Show("done"); }
If use BeginInvoke, MessageBox pops simultaneous to the text update. If use Invoke, MessageBox pops after the 3 second sleep. Hence, showing the effect of an asynchronous (BeginInvoke) and a synchronous (Invoke) call.
Delegate.BeginInvoke() asynchronously queues the call of a delegate and returns control immediately. When using Delegate.BeginInvoke(), you should call Delegate.EndInvoke() in the callback method to get the results.
Delegate.Invoke() synchronously calls the delegate in the same thread.
why and when to use Invoke().
Both Invoke() and BeginInvoke() marshal the code you specify to the dispatcher thread.
But unlike BeginInvoke(), Invoke() stalls your thread until the dispatcher executes your code. You might want to use Invoke() if you need to pause an asynchronous operation until the user has supplied some sort of feedback.
For example, you could call Invoke() to run a snippet of code that shows an OK/Cancel dialog box. After the user clicks a button and your marshaled code completes, the invoke() method will return, and you can act upon the user's response.
Code Formater
| Paste Here Your Source Code | ||||||||||||||
| Source Code Formatting Options | ||||||||||||||
|
||||||||||||||
| Copy Formatted Source Code | ||||||||||||||
| Preview Of Formatted Code |