Click here to Skip to main content
15,890,579 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
AnswerRe: Knowing about the prerequisites of .net Pin
Vipin_Arora17-Apr-12 21:08
Vipin_Arora17-Apr-12 21:08 
Questiondebug asp.net (vb) code on visual studio 2003 in windows 7 Pin
Member 88023079-Apr-12 7:32
Member 88023079-Apr-12 7:32 
AnswerRe: debug asp.net (vb) code on visual studio 2003 in windows 7 Pin
Not Active9-Apr-12 8:02
mentorNot Active9-Apr-12 8:02 
QuestionAccessing and adding Items to a Collection that is in a Collection Pin
D3STROYRofWRLDS6-Apr-12 11:27
D3STROYRofWRLDS6-Apr-12 11:27 
AnswerRe: Accessing and adding Items to a Collection that is in a Collection Pin
Eddy Vluggen7-Apr-12 22:06
professionalEddy Vluggen7-Apr-12 22:06 
QuestionAccessing functions or subs from another class. Pin
Member 85988664-Apr-12 5:03
Member 85988664-Apr-12 5:03 
AnswerRe: Accessing functions or subs from another class. Pin
Eddy Vluggen4-Apr-12 5:21
professionalEddy Vluggen4-Apr-12 5:21 
QuestionAsynchronous webservice calls and threading Pin
CDP18024-Apr-12 0:19
CDP18024-Apr-12 0:19 
I have a client program that uses a webservice for communication with its server. Up to now all calls to the webservice have been synchronous, but now they must be made asynchronously. The webservice has been wrapped into classes that handle and log communication errors and perform all conversions between entities and the structs used by the webservice. This way the client is practically unaware of the existence of the webservice and might as well be sitting on top of the application logic on the server. This compatibility will be lost when using asynchronous calls (the real application logic does not provide any methods and callbacks for asynchronous calls), but that is not really important. It would only have been useful in some testing scenarios where we might have wanted to take the webservice out of the picture.

Inside the wrapper classes, an asynchronous call will look something like this:
public void GetSomeEntityBySomeParameter(int SomeParameter)
{
    try
    {
        // the Webservice object is auto-generated from the WSDL of the service and an instance
        // has been created in the constructor, as well as hooking up the event handler method to
        // receive the results
        this.Webservice.GetSomeEntityBySomeParameterAsync(SomeParameter);
    }
    
    catch(Exception e)
    {
        // error while sending the request
        // here comes code to do some logging and
        // handling if possible
    }
}

private void Webservice_GetSomeEntityBySomeParameterCompleted(object sender, SomeEventArgs e)
{
    SomeEntity Result = null;
    

    try
    {
        if(e.Result.ErrorReport != null)
        {
            // somme error has occurred on the server.
            // let's use the error info in the result to do some logging
            // and prepare some error info for the client
        }
        else
        {
            // let's convert the struct to an entity
            Result = ConvertSomeStructToEntity(e.Result.Value);
        }

        // *************** Big Mistake! ***************
        // this raises an event to notify the caller that the 
        // asynchronous call has been completed
        OnGetSomeEntityBySomeParameterFinished(Result);
    }
    
    catch(Exception e)
    {
        // something unexpected happened
        // little options to handle this, but let's
        // at least log all we can to provide some
        // info for debugging
    }
}


This last step, notifying the object that requested the data, is the part that gives me trouble. A separate thread has been started for the asynchronous call and also the Webservice_GetSomeEntityBySomeParameterCompleted() method will be called from that thread. Once I'm done and raise my own event, the event handler of the caller will also be executed by that thread.

That's wrong in many ways. Usually the calls will be initiated from some object around the UI. The UIs from Microsoft simply throw an exception when changes to the UI are made by another thread. In this case the UI is not from Microsoft and reacts very well to multithreading, but there still are side effects. Also, it is simply unacceptable to lose control over which part of the code is executed by any given thread.

In the end I need some safe way to raise the GetSomeEntityBySomeParameterFinished event, so that its event handler is executed by the same thread that requested the data. I'm very careful here because pulling a thread away from what it is currently doing and making it do something else instead (if that's possible at all) is just asking for even more trouble


Edit: Added some typical exception handling, just for the sake of completeness

Edit^2: I have been looking at the System.Windows.Threading.Dispatcher class, which seems to do what I want to have. Unfortunately, as the namespace tells, that's WPF and I can't afford to include WPF just to get to use this class. Also, things like Control.Invoke would not help, even if the UI had such methods. My problems seem to come from a race condition in the presenters, which are not UI classes.
I'm invincible, I can't be vinced




modified 4-Apr-12 9:39am.

AnswerRe: Asynchronous webservice calls and threading Pin
Eddy Vluggen4-Apr-12 5:10
professionalEddy Vluggen4-Apr-12 5:10 
GeneralRe: Asynchronous webservice calls and threading Pin
CDP18024-Apr-12 5:58
CDP18024-Apr-12 5:58 
SuggestionRe: Asynchronous webservice calls and threading Pin
Eddy Vluggen4-Apr-12 10:53
professionalEddy Vluggen4-Apr-12 10:53 
AnswerRe: Asynchronous webservice calls and threading Pin
cpkilekofp23-Apr-12 10:43
cpkilekofp23-Apr-12 10:43 
QuestionNumberGroupSeparator not working? Pin
Bernhard Hiller1-Apr-12 22:46
Bernhard Hiller1-Apr-12 22:46 
AnswerRe: NumberGroupSeparator not working? Pin
Eddy Vluggen1-Apr-12 23:37
professionalEddy Vluggen1-Apr-12 23:37 
GeneralRe: NumberGroupSeparator not working? Pin
Bernhard Hiller2-Apr-12 0:56
Bernhard Hiller2-Apr-12 0:56 
GeneralRe: NumberGroupSeparator not working? Pin
Eddy Vluggen2-Apr-12 1:05
professionalEddy Vluggen2-Apr-12 1:05 
GeneralRe: NumberGroupSeparator not working? Pin
VJ Reddy13-Apr-12 6:10
VJ Reddy13-Apr-12 6:10 
QuestionHow to select a feature using mouse click in DotSpatial Framework? Pin
Chandan Kumar Rath29-Mar-12 20:56
Chandan Kumar Rath29-Mar-12 20:56 
AnswerRe: How to select a feature using mouse click in DotSpatial Framework? Pin
Pete O'Hanlon29-Mar-12 21:14
mvePete O'Hanlon29-Mar-12 21:14 
QuestionHas StringBuilder gotten more efficient in VS2010? Pin
jesarg29-Mar-12 5:16
jesarg29-Mar-12 5:16 
AnswerRe: Has StringBuilder gotten more efficient in VS2010? Pin
so_soul29-Mar-12 5:45
so_soul29-Mar-12 5:45 
GeneralRe: Has StringBuilder gotten more efficient in VS2010? Pin
Not Active29-Mar-12 6:31
mentorNot Active29-Mar-12 6:31 
QuestionRe: Has StringBuilder gotten more efficient in VS2010? Pin
Stohn29-Mar-12 5:53
Stohn29-Mar-12 5:53 
AnswerRe: Has StringBuilder gotten more efficient in VS2010? Pin
jesarg29-Mar-12 7:15
jesarg29-Mar-12 7:15 
Questionhow to monitor ftp files by c#.net Pin
eng.rania27-Mar-12 4:06
eng.rania27-Mar-12 4:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.