473,406 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Synchronize the functions

I have an application that is asynchronous in design. For example.

obj.SendCommand(string cmd)

and the result will be returned in a callback function.

public void obj_OnCommandFinished(object sender, string result)

Will that be possible to turn combine these two functions so that i can get
the result through synchronous calls?

result = SendCommand(string cmd)

If yes, how should I implement that? My current design is to use a flag to
determine whether the results has returned or not. However, i have to poll
this flag and sleep the thread in order to wait for the result.

Any suggestions?

Thanks!

Jun 23 '07 #1
2 1731
Cheryl wrote:
I have an application that is asynchronous in design. For example.

obj.SendCommand(string cmd)

and the result will be returned in a callback function.

public void obj_OnCommandFinished(object sender, string result)

Will that be possible to turn combine these two functions so that i can
get the result through synchronous calls?

result = SendCommand(string cmd)

If yes, how should I implement that? My current design is to use a flag
to
determine whether the results has returned or not. However, i have to
poll this flag and sleep the thread in order to wait for the result.

Any suggestions?

Thanks!
Hi Cheryl,

What you're after is the [Auto,Manual]ResetEvent classes. They provide the
necessary infrastructure to wait on an event, without having to poll a
flag.

A typical use would be to initialise the event, call SendCommand, then wait
on the event. In the callback function, you would signal the event, which
would wake up the waiters on the event.

Take a look at the following example:

///
using System;
using System.Reflection;
using System.Threading;

public class AsyncObj
{
public delegate void CFH (object sender, string result);
public event CFH CommandFinished;

public void SendCommand (string cmd)
{
Thread isc = new Thread(InternalSendCommand);
isc.Start(cmd);
}

private void InternalSendCommand (object param)
{
Thread.Sleep(1500);

if (CommandFinished != null)
CommandFinished(this, string.Format("Hello, {0}.", param));
}
}

public class EntryPoint
{
private AutoResetEvent _are;

public static void Main ()
{
EntryPoint ep = new EntryPoint();
ep.InternalMain();
}

private void InternalMain ()
{
AsyncObj ao = new AsyncObj();

_are = new AutoResetEvent(false);

ao.CommandFinished += OnCommandFinished;
ao.SendCommand("Tom");
_are.WaitOne();

Console.WriteLine("Finished");
}

private void OnCommandFinished (object sender, string result)
{
Console.WriteLine(result);
_are.Set();
}
}
///

What's of interest is in the EntryPoint class, where the AutoReset event is
constructed and waited on (in InternalMain()), and signalled in
OnCommandFinished(...)

--
Tom Spink
University of Edinburgh
Jun 23 '07 #2
On Jun 22, 11:29 pm, "Cheryl" <justtosa...@excite.comwrote:
I have an application that is asynchronous in design. For example.

obj.SendCommand(string cmd)

and the result will be returned in a callback function.

public void obj_OnCommandFinished(object sender, string result)

Will that be possible to turn combine these two functions so that i can get
the result through synchronous calls?

result = SendCommand(string cmd)

If yes, how should I implement that? My current design is to use a flag to
determine whether the results has returned or not. However, i have to poll
this flag and sleep the thread in order to wait for the result.

Any suggestions?

Thanks!
I would encapsulate that logic in another class and use the
IAsyncResult pattern. That class will have BeginSendCommand and
EndSendCommand methods that operate asynchronously. BeginSendCommand
returns an IAsyncResult that you can use to wait on the operation by
calling IAsyncResult.AsyncWaitHandle.WaitOne(). You could also add a
method called SendCommand that operates synchronously and uses the
other methods internally to do that.
Jun 23 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Thomas Rademacher | last post by:
Hallo, I want to synchronize the main function of my script. I havn't a class structure in my script. def main(): # my code to synchronized if __name__=='__main__': main()
2
by: Roger Godefroy | last post by:
Hi, I have to develop a customer relation application. I would like to synchronize databases, so the user can work at the office and at home. Internet isn't available at home. What would be the...
6
by: alex via SQLMonster.com | last post by:
Hi, anybody can help me. How can i synchronize 2 tables on 2 different sql servers 2000 i mean TABLE1(col1, col2, col3, col4) and TABLE1(col1, col2, col3, col4, col5, col6) the first 3...
4
by: gene.ellis | last post by:
Using a web interface, I am placing text into a SQL database. From time to time, I would like to synchronize one of my other tables in the database with the table that I am inserting content into....
8
by: Zlatko Matić | last post by:
There is a form (single form) and a combobox. I want that current record of the form is adjusted according to selected value in the combobox. Cuurrent record should be the same as the value in the...
1
by: Hewit | last post by:
DataTable Class documentation says. Thread Safety "This type is safe for multithreaded read operations. You must synchronize any write operations." In my web based application (ASP.Net)...
2
by: Rodusa | last post by:
I am trying to Synchronize anonymous users with authenticated uses using Profile_MigrateAnonymous. However Profile_MigrateAnonymous only carries information in one direction, for instance, if I...
1
by: Stephen | last post by:
Hi, Is there a sample application using ASP .NET to synchronize folder contents? I want to synchronize folder contents on 2 different machines Thanks, Stephen
0
by: krupa.p | last post by:
Hi All, I am trying to write a C# .Net desktop application that synchronizes files/folders on the PC with those on a Windows CE device, when the device is connected to the PC via ActiveSync. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.