Results 1 to 6 of 6

Thread: How to do Asynchronous Programming with Delegates?

  1. #1
    Join Date
    Jan 2011
    Posts
    7

    How to do Asynchronous Programming with Delegates?

    I have recently started writing some codes that are related to the multiple-core processors. In that I am stuck at Asynchronous Delegates. I am not having enough ideas about terms that are related to asynchronous. That's why I thought to take some help from you people. I am having another doubt also. It is of ReaderWriterLockSlim and ReaderWriterLock. I am not able to understand much about this part. Since, it was related to this topic, I thought to ask on the same thread, instead of posting another. I am sure that you will not ignore this topic.

  2. #2
    Join Date
    Apr 2008
    Posts
    193

    Re: How to do Asynchronous Programming with Delegates?

    Parallel processing with multi-core processors can improve performance significantly. But the parallel access to data also poses dangers that could result in non-reproducible bugs. Once code is running in parallel on common data, this data should be closely observed. Parallel reading of data is usually not a problem, but in parallel writes appropriate precautions must be taken. This also applies to operations, they want a read and then perform an update access. This occurs, for example, already on when an object is stored in a counter that is incremented for each call of the object and its methods. In parallel executing threads can be conflicts:
    • The counter has the value 5
    • Thread A and B simultaneously read from the counter, so will work with the value 5
    • Both threads increment the value (ie work with the value 6)
    • Both threads write back the value in the counter, which now includes 6, although he should really have the value 7.
    To avoid such situations and to coordinate the parallel requests are exclusive Lock (access barriers) used. The .NET Framework provides to the class ReaderWriterLock ready. This regulates the access of threads to shared resources and allows multiple parallel reads, but only one writer. The operation and use of locks based on those of the semaphore.

  3. #3
    Join Date
    Oct 2008
    Posts
    167

    Re: How to do Asynchronous Programming with Delegates?

    Asynchronous delegates provide a pleasant way to values between a called Thread (worker) and its callers (Caller) exchange. It can be any number of parameters between the caller and worker thread passed. Either as arguments to the worker threads, or as return values at the termination of the worker. In addition, exceptions are forwarded to the origin of the thread asynchronous delegate. It should be noted, however, that the worker is asynchronous to the caller. The following example in which two strings are compared, for example, may originate from a database. First, the synchronous and traditional model. In the first example is the reading of the first string (dbr.GetResult (1)) and its allocation in string s1. Closing this process in dbr.GetResult (2) and the assignment is repeated to string s2. Note that the two reads are executed sequentially:
    Code:
    static void compareResult () 
    { 
    DBRetriever DBRetriever dbr = new (); 
    string s1 = dbr.GetResult (1); 
    string s2 = dbr.GetResult (2); 
    Console.WriteLine (? S1 == s2 "Same" "Different"); 
    }
    To speed up the entire run of this procedure, the following code example, the two database accesses are initiated in parallel. This is the asynchronous read operation. At this point, asynchronous delegates are used. The caller agrees with the worker thread while on the return values. The following example shows the use of asynchronous delegation pointed out. Two return values are returned.

  4. #4
    Join Date
    Mar 2008
    Posts
    258

    Re: How to do Asynchronous Programming with Delegates?

    I am providing code of asynchronous database query with explanation so that you can understand it properly.
    Code:
    delegate string getResult (int i); 
    static void compareResult () 
    { 
    // Declaration and instantiation of the two delegates 
    
    // With the getResult-Signature: 
    Result1 = new DBRetriever getResult (), getResult,. 
    Result2 = new DBRetriever getResult (), getResult,. 
    
    // Start the retrieval from the database: 
    IAsyncResult cookie1 result1.BeginInvoke = (1, null, null); 
    IAsyncResult cookie2 result2.BeginInvoke = (2, null, null); 
    
    // Perform some random calculation: 
    double seed = 1.23; 
    
    for (int i = 0; i <1000000; i + +) 
    seed = Math.sqrt (seed + 1000); 
    
    // Get the results, waiting for completion if necessary. 
    // Here's where any exceptions thrown will be: 
    string s1 = result1.EndInvoke (cookie1); 
    string s2 = result2.EndInvoke (cookie2); 
    Console.WriteLine (? S1 == s2 "Same" "Different"); 
    }
    The example starts with the declaration and instantiation of the two delegates, the referenced methods will be executed asynchronously. Each of these two delegate referring to a DBRetriever object. In the example we also assume that DBRetriever does not allow concurrent access. If that were not enough, a single delegate. Then be by BeginInvoke methods activated and then branches back to the caller immediately. As a function parameter, and in consultation with the delegate of an integer date is required. Furthermore, two parameters, an optional callback and a data object must be passed. In this example, however, are not used and therefore replaced by zero. BeginInvoke returns an object of type IASynchResult back. This serves as a cookie for calling EndInvoke below. IASynchResult also has the attribute IsCompleted to monitor the process progress and its Beendigung.Nach the call to BeginInvoke can continue to work with other routine. In the example here, made some mathematical calculations. Calling EndInvoke delivers the results back, in the example strings. Threads should not be terminated worker called the waiting EndInvoke at this point. If during the execution of the asynchronous method, an exception occurs, it will be further channeled to the caller.

  5. #5
    Join Date
    Mar 2008
    Posts
    227

    Re: How to do Asynchronous Programming with Delegates?

    For some object types .Net are the methods available as asynchronous versions. Their names start with "Begin" and "End". These asynchronous methods have similar signatures as those of the asynchronous delegate, solve problems and enable more complex but much more competing actions. A web or TCP socket server for example, can thus only a few threads in the pool several hundred competing requests and manage through. It uses the asynchronous methods and NetworkStream.BeginRead NetworkStream.BeginWrite. However, you should asynchronous methods to avoid, if possible, for the following reasons:
    • Unlike asynchronous delegate, it can happen that asynchronous methods do not run parallel to the caller.
    • The use of asynchronous methods is complex and there are some details to consider. The advantage of the asynchronous method disappears when you do not keep meticulous in using the guidelines.
    If the destination is only in the parallel execution of threads, you should better the synchronous variations of the methods used in conjunction with asynchronous delegates, such as NetworkStream.Read. Another possibility is the use of or ThreadPool.QueueUserWorkItem BackgroundWorker and of course the creation of a separate new thread.

  6. #6
    Join Date
    Mar 2008
    Posts
    192

    Re: How to do Asynchronous Programming with Delegates?

    By another pattern can be implemented asynchronous versions of methods. They are called "event-based asynchronous pattern" as described. The names of the methods end with the term "Async". The names of the corresponding events end with "Completed". This event-based methods provide events for the progress of the process (the "Progress Reporting") and the discontinuation of operations. They are thus likely Forms and Controls in Windows to update applications. If these features are to be used in conjunction with an object type that is not the event-based model supports asynchronous processing, it can be implemented by a BackgroundWorker helper class. Methods or to periodically execute code in general, timers are used. Since the version 1.1. of .Net Framework exists the System.Threading namespace with the class Timer. This class uses the thread pool, allowing the use of multiple timers. The Timer class is quite simple and consists only of a constructor and two methods (and dispose change). Change the method takes two parameters: The parameter 1st parameter defines the first and the subsequent timer tick interval in milliseconds.

Similar Threads

  1. Delegates : Function pointers in programming languages
    By Karumbu in forum Software Development
    Replies: 4
    Last Post: 28-12-2010, 05:34 AM
  2. What is Asynchronous Function?
    By Khan Baba in forum Software Development
    Replies: 5
    Last Post: 08-02-2010, 01:09 PM
  3. Asynchronous Socket Error 10053 issue
    By wirelessprom in forum Networking & Security
    Replies: 3
    Last Post: 31-12-2009, 02:44 PM
  4. What do you mean by Csharp Delegates?
    By Jarini in forum Software Development
    Replies: 3
    Last Post: 11-11-2009, 09:57 PM
  5. Asynchronous Transfer Mode (ATM) Networks - Guide
    By mindreader in forum Guides & Tutorials
    Replies: 11
    Last Post: 15-11-2006, 07:22 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,528,863.37135 seconds with 17 queries