How to program this?

  • Thread starter Anders Eriksson
  • Start date
A

Anders Eriksson

I have an xml file that contains a number of commands. These commands
must be executed in sequence. One of the commands calls a async function
that returns directly and when it finish it raise an event.

How do I create a program that executes the commands in a sequence but
"waits" for the async function to raise an finishEvent?

It seems like this is a problem that many have already solved. Please
enlighten me!

// Anders
 
A

Anders Eriksson

It is already solved a dozen different ways. But without knowing what
your "command" is or how you're actually executing them, it's hard to
know what might be the best way to do it.

Here's one way:

object objWait = new object();
AsyncObject async = new AsyncObject();

async.Finished += (sender, e) =>
{
lock (objWait)
{
Monitor.Pulse(objWait);
}
};

lock (objWait)
{
async.ExecuteAsync();

Monitor.Wait(objWait);
}

The above code assumes the hypothetical type "AsyncObject"; this is
really just whatever you actually have that raises the event when the
async operation is done. It assumes that the event is never raised on
the same thread that calls the hypothetical ExecuteAsync() method.

In the current version of .NET, you also have the Task Parallel Library,
which provides an OO way of representing tasks, including async ones you
can wait on.

There is also the CTP available now showing the proposed new async/wait
features for C# 5.0, which provide a language-supported way to do the
same, the main advantage being that the waiting code doesn't tie up a
thread while it's waiting.

Pete

I'm not sure that I grasp this but here is a bit more info
I have a COM server that contains all functions for the hardware. I have
made a class that wraps this COMserver. My program is an Winform
application and the call of the async method is done from the GUI thread.

As I understand this makes your code not functioning, or at least the
program will hang forever waiting for an event that never will be detected.

Is there someway of doing this without using threads?
I get confused everything I have to do threads...

// anders
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top