.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

How Do I maintain the chronological order in which Asynchronous delegate calls are made?

Posted By:      Posted Date: September 28, 2010    Points: 0   Category :.NET Framework

I have created a basic logging framework for my application that writes log messages to a local text file using an Asynchronous delegate.

My application has many threads running however, and as a result, the order of the log entries in the text file is not chronological.

So I have two competing requirements: on one hand, my WriteToLog() method has to return almost immediately so that it doesn't block in-flight code, while at the same time the log file has to be in chronological order.

Any ideas?  Some of the problems I've encountered are:

1a.  Trying to use a Queue (with a lock{ } around it) to maintain the order seems to result in deadlocks

1b.  Even if I use a Queue, at some point the Queue must be written to disk, so it seems like I'd have to frequently have WriteToLog spin off a "WriteToDisk" like method.

2a.  I've considered instantiating a new thread which polls the Queue from (1a) and writes to disk in a loop, say, every 5 seconds.  I'm leaning more towards this method.

3.  I've looked into log4net and some other stuff but it just seems like overkill, and I'd really like to have my own framework for this seemingly simple task.



View Complete Post

More Related Resource Links

.NET Delegates: Making Asynchronous Method Calls in the .NET Environment


One of the many great features of the .NET Framework is that it has asynchronous infrastructure built in. In .NET you can call any method asynchronously by defining a delegate for the method and calling the delegate's asynchronous methods. This is beneficial to your application because when a synchronous call is made, the calling thread is blocked until the method completes whereas an asynchronous call is made on a different thread, and this allows the original thread to continue its work while the asynchronous call is in progress.This article explains delegates in .NET and how to use them to perform asynchronous calls, eliminating age-old threading problems.

Richard Grimes

MSDN Magazine August 2001

Windows 2000: Asynchronous Method Calls Eliminate the Wait for COM Clients and Servers


Windows 2000 is the first version of COM to support asynchronous method calls, which permit clients to make nonblocking calls to COM objects and objects to process incoming calls without blocking the calling threads. COM clients benefit from asynchronous method calls because they can continue working while waiting for outbound calls to return. Objects benefit because they can queue incoming calls and service them from a thread pool. Our SieveClient and SieveServer sample apps demonstrate how to create and use asynchronous clients and servers in COM-based distributed applications.

Jeff Prosise

MSDN Magazine April 2000

How can I maintain "state" across WCF method calls?

I need to retrieve very large portions of NTFS files (as byte arrays) from a WCF Service.  The needed portions of the files are far too large to efficiently return in a single request, so I need a "chunky" implementation that will return just a portion of the file on each method call. But the entire process needs to be very fast... specifically: I need to avoid the overhead of re-opening the file each time the client requests the next "chunk". So how can I keep the filestream open on the service-side in anticipation of the next request from the client for the next chunk? And if no subsequent request arrives, then close the filestream? That is, implement "lazy close". Or stated another way: Does the code instantiated on the service-side to service an incoming method call remain "alive" after the method returns to the caller?  Or can it be configured that way, like a Singleton SAO behaves in Remoting?? Thanks, DT 

Making asynchronous calls to web services during ASP.NET page processing

Some ASP.NET web applications use web services to get some data that they display to users. Some pages may lay hardly on web services and these pages need some optimization to work better. In this posting I will show you how to use web services behind your ASP.NET page asynchronously and perform a lot of queries to web services. Sample code included! Eneta.Examples.WebAppThreading.zip VS2008 solution | 60KB Asynchronous page processing As an introduction let's see how ASP.NET supports asynchronous calls behind pages. Although we can use our own threading mechanism and avoid what is offered by ASP.NET it is more safe and time saving to use ASP.NET own infrastructure. The image on right that I stole from MSDN article Asynchronous Pages in ASP.NET 2.0 shows you how synchronous and asynchronous pages are processed by ASP.NET. I recommend to read this MSDN article because you can find also other methods for asynchronous calls. I will introduce you how to use PageAsyncTask based calls. If you look at diagram on right you can see that asynchronous processing of registered tasks happens between PreRender and PreRenderComplete events in separate threads. If we have data bound controls on our page we have to move data binding to PreRenderComplete event because this is the closest phase of page processing where asynchronous calls are finished.

Asynchronous Web Services, jQuery or .NET (BeginInvoke) Calls?



I have been moving my application to be more AJAX-based.  Currently I have a web service call where I use jQuery to gather some textbox (string) and checkbox (boolean) results and pass it to a web service.  Everything works really well, the response time is super quick.  The only downside that I can see to this is

1) you need to use jQuery, which will add to the user's download time - this doesn't really affect my application too much because I am using jQuery throughout the site

2) users can see the code and potentially try to hack the web service because they know where it is

I am most concerned with #2.  I've been reading a lot about .NET web service calls, using IAsyncResult, http://msdn.microsoft.com/en-us/library/2e08f6yc.aspx, and I am beginning to understand the 4 methods available in this article.  However, is there extra bloat using this way compared to the jQuery web service call?

If there isn't that much bloat or lag time using this method, I'd like to convert my application.  Which one of these 4 methods is best in a scenario where I am just returning a small string value?  The 4 methods in the article are

  • to wait for EndInvoke itself, which from what I read is not recom

Delegate subroutine calls


Hi, I have read the replies here about delegate calls across threads, but still cannot get my 'simple' code to work.

I have one class that assigns a subroutine as a delegate to be passed to another class. This second class then calls the delegate subroutine from it's timer routine, but I cannot resolve this one so any pointer would be much appreciated.

Class 1:
    public delegate void preClampDisplay();
    private void InitialiseGui()

     preClampDisplay pcDelegate = new preClampDisplay(this.preClampUpdate);
     preClampAdjust = new buttonAdjust(btnPreClampIncrease, btnPreClampDecrease, pcDelegate);


Class 2:

        private Delegate DelegateUpdate, DelegateCommit;

        public buttonAdjust(Button increase, Button decrease, Delegate update)
            DelegateUpdate = update;


        private void OnUpdateTimerTimedEvent(object source, ElapsedEventArgs e)

 // this version does no

How to change the chronological order of blog commets



We have a requirement where we have to show blog comments in reverse chronological order than what SP2010 OOTB provides. Is there a way to do so.This is urgent as we have a demo to our customer this friday.




Server.MapPath dont have access to web all calls made in business layer


Hi Guys,

I'm trying to find a way to map a path to a pdf object in my business layer.. im using nop commerce and their messagemanager class is in the business layer and all calls to email queuing and the like are done in the business layer..

i would love to know if there's a way to do it without having the ability to pass the path in from the web layer..

my backup plan is an appsetting in web.config.. but hoped it wouldn't come to that?!

jQuery $(document).ready() and ASP.NET Ajax asynchronous postback

Many call it the ASP.NET jQuery postback problem, but using the technique below should make it no problem anymore. $(document).ready() isn't called after an asynchronous postback. What this means? You lose the functionality that should be executed within $(document).ready() after an UpdatePanel rendered its contents after an asynchronous postback.

Enabling Users to Maintain SharePoint Content

When SharePoint is installed as a corporate intranet, there is a considerable amount of effort involved in maintaining the content to keep the site relevant. As we discussed in the first few parts of this series, SharePoint has many built-in features to reduce the burden on the site administrator.

Some areas of the site usually contain less-dynamic information. Internal memos and corporate policies are common examples of this information. One option for this information is to post these documents in a document library. Documents in a library are often stored in a few different formats: Word, Acrobat (PDF), or HTML. Each of these formats has its drawbacks.

jQuery live() and ASP.NET Ajax asynchronous postback

The issue is that normally $(document).ready() is called when the DOM is ready to be manipulated. But this doens't happen after an ASP.NET Ajax asynch postback occurs.

This means that the initial jQuery bindings won't be automatically available after the asynchronous postback is over. I underline "automatically" here because my last post describes more possibilities, how this issue can be worked around.

Maintain HTML Radio Button Selection Inside GridView After Postback

Long time ago I wrote an article about getting the value of the selected RadioButtons inside the GridView control. The code did not addresssed the postback issue which means that the selected radio button was cleared after the postback occurs.

Ready made List Types provided by SharePoint

SharePoint provides some out of the box List Types. Some of the most used ready made list types that SharePoint provides currently are listed below:

Enabling Users to Maintain SharePoint Content

When SharePoint is installed as a corporate intranet, there is a considerable amount of effort involved in maintaining the content to keep the site relevant. As we discussed in the first few parts of this series, SharePoint has many built-in features to reduce the burden on the site administrator.

Using JQuery to perform Ajax calls in ASP.NET MVC

The muscle behind the actual asynchronous calls comes from JavaScript. I looked around at a bunch of existing JavaScript libraries and settled on JQuery because of the way it leverages existing CSS knowledge. The three things that the library should do easily are:



Is it possible with SQL sorted in descending order according to the word?
In Table I shrajeno Mastercard, Visa, Diners etc ...

All Visa are the first in the order, another Mastercard, etc. ...

Something does not work: ORDER BY PaymentType = 'Visa';

I would not want to work in one field PaymentId (111,222,33, etc. ..) and then use the ORDER BY.

Is this possible in such a way?

Linq qry with order by basic questions


I have never tried a group by with linq, now I do and I'm stuck.

I try to convert this sql statement to its linq counterpart :

SELECT COUNT(tb_Mailer_Smses.Id) AS [cnt], tb_Mailer_mailing.Id As [Id]
,SUM( CASE WHEN tb_Mailer_smses.DLRStatus = 200 THEN 1 ELSE 0 END) As [Delivered]
FROM tb_Mailer_mailing INNER JOIN
tb_Mailer_Smses ON tb_Mailer_mailing.Id = tb_Mailer_Smses.MailingId
WHERE tb_Mailer_mailing.ApplicationId = 1
GROUP BY tb_Mailer_mailing.Id

Is there some one who can me show me the convertion process ?

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend