473,769 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Asynchronous problem

Hi,

I am using the following code

private void menuItemStart_C lick(object sender, System.EventArg s e)
{
DataTable tbRand, tbShow;
int randSize;

....
// schema of tbShow is being loaded from an xsd file
tbRand = DrawEngine.Gene rateAllDraws(tb Rand, randSize, ref tbShow);
dataGridRandomi zed.DataSource = tbShow;
....
}

This code works ok, and data is being showed correctly in datagrid.
Now I am trying to convert it to asynchronous code as shown below.

public delegate DataTable GeneratingAllDr awsDelegate(Dat aTable table,
int tot, ref DataTable show);

private void menuItemStart_C lick(object sender, System.EventArg s e)
{
...
GeneratingAllDr awsDelegate dc = new
GeneratingAllDr awsDelegate(Dra wEngine.Generat eAllDraws);
AsyncCallback cb = new AsyncCallback(g etResultAllDraw s);
IAsyncResult ar = dc.BeginInvoke( tbRand, randSize, ref tbShow, cb,
null);
...
}

private void getResultAllDra ws(IAsyncResult ar)
{
DataTable tbRand, tbShow;
GeneratingAllDr awsDelegate del;

// schema of tbShow is being loaded from an xsd file

del = (GeneratingAllD rawsDelegate) ((AsyncResult)a r).AsyncDelegat e;
tbRand = del.EndInvoke(r ef tbShow, ar);
dataGridRandomi zed.DataSource = tbShow;

}

When I execute this code, no data is being shown in datagrid(when
passing tbRand as DataSource same behaviour was given). I also tried
to place the tbShow into a DataSet and write the data to an XML file,
but no file is being created. Also no exception is being rised.

Can someone help me figure my problem out.
Thanks in Advance

Nov 17 '05 #1
2 1565
The problem is that the async callback is being executed on a worker thread not on the UI thread. When you set the datasource, the datagrid tries to update itself on the wrong thread. You need to ste the datasource on the UI thread by using dataGridRandomi zed.BeginInvoke and pass it a delegate that will be executed on the UI thread. One common way to do this is to re-use the async callback method like this:

private void getResultAllDra ws(IAsyncResult ar)
{
if( dataGridRandomi zed.InvokeRequi red )
{
dataGridRandomi zed.BeginInvoke (new AsyncCallback(g etResultAllDraw s), new object[]{ar});
}
else
{
DataTable tbRand, tbShow;
GeneratingAllDr awsDelegate del;

// schema of tbShow is being loaded from an xsd file

del = (GeneratingAllD rawsDelegate) ((AsyncResult)a r).AsyncDelegat e;
tbRand = del.EndInvoke(r ef tbShow, ar);
dataGridRandomi zed.DataSource = tbShow;
}
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi,

I am using the following code

private void menuItemStart_C lick(object sender, System.EventArg s e)
{
DataTable tbRand, tbShow;
int randSize;

...
// schema of tbShow is being loaded from an xsd file
tbRand = DrawEngine.Gene rateAllDraws(tb Rand, randSize, ref tbShow);
dataGridRandomi zed.DataSource = tbShow;
...
}

This code works ok, and data is being showed correctly in datagrid.
Now I am trying to convert it to asynchronous code as shown below.

public delegate DataTable GeneratingAllDr awsDelegate(Dat aTable table,
int tot, ref DataTable show);

private void menuItemStart_C lick(object sender, System.EventArg s e)
{
...
GeneratingAllDr awsDelegate dc = new
GeneratingAllDr awsDelegate(Dra wEngine.Generat eAllDraws);
AsyncCallback cb = new AsyncCallback(g etResultAllDraw s);
IAsyncResult ar = dc.BeginInvoke( tbRand, randSize, ref tbShow, cb,
null);
...
}

private void getResultAllDra ws(IAsyncResult ar)
{
DataTable tbRand, tbShow;
GeneratingAllDr awsDelegate del;

// schema of tbShow is being loaded from an xsd file

del = (GeneratingAllD rawsDelegate) ((AsyncResult)a r).AsyncDelegat e;
tbRand = del.EndInvoke(r ef tbShow, ar);
dataGridRandomi zed.DataSource = tbShow;

}

When I execute this code, no data is being shown in datagrid(when
passing tbRand as DataSource same behaviour was given). I also tried
to place the tbShow into a DataSet and write the data to an XML file,
but no file is being created. Also no exception is being rised.

Can someone help me figure my problem out.
Thanks in Advance


Nov 17 '05 #2
Thanks for your help, problem has been solved successfully

Nov 17 '05 #3

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

Similar topics

3
2825
by: Corne Oosthuizen | last post by:
I'm writing a Telnet Server application using Asynchronous sockets. I spawn a listener thread to handel incomming connections and create a separate client socket for each new connection. I then set the new client socket to BeginReceive(). My problem: When two client socket connections send data
3
2493
by: Matthew King | last post by:
Hi all I've written a asynchronous socket client class, but i've found that in order to consume it I have to use events, and cannot simply for example SocketClient client = new SocketClient(110, "some.server.com") client.Connect() client.SendData("Hello World") Instead I have to wait for the async method to raise a Connected event, and call client.SendData from there, for complex chains of operations this because a nightmare chain of...
2
2335
by: Darryl A. J. Staflund | last post by:
Hi there, Can anyone tell me why invoking a single SQL insert statement (well, rather, a method that performs a SQL insert) using an asynchronous delegate should result in twice the number of records being inserted? Am I just making a mistake somewhere? (though I don't get double the records if I call the method synchronously.) Thanks, Darryl
3
3993
by: usenetaccount | last post by:
In a newly created test app, to maximize client performance I tried to make two SOAP method calls in tandem (the soap methods execute some specified query), as each call includes a large amount of idle time by the client as it waits for a query to execute and return a dataset. As a test, I initially wrote two versions: ' // Sequential & Synchronous Dim webobj As com.mycompany.webobject Dim ds1 As System.Data.DataSet Dim ds2 As...
5
5594
by: archana | last post by:
Hi all i am having application which is using asychronous web request. At a time i am processing 5 urls asynchronously. Application working properly for 5 asynchronous call. But sometimes CPU usage suddently increases to 100 %. Can some one tell me why this is happening.
4
3821
by: taskswap | last post by:
I have a legacy application written in C that I'm trying to convert to C#. It processes a very large amount of data from many clients (actually, upstream servers - this is a mux) simultaneously. I've read through what must be dozens of ways to do socket communication in C#, and it seems they all devolve into three basic options - Socket.Select, IOCP through a native interface, and Asynchronous callbacks. I'm fine using Asynchronous...
8
1356
by: Paul Rubin | last post by:
I'd like to suggest adding a builtin abstract class to Python called AsynchronousException, which would be a subclass of Exception. The only asynchronous exception I can think of right now is KeyboardInterrupt, so KeyboardInterrupt would become a subclass of AsynchronousException instead of being a direct subclass of Exception. There's been talk of adding ways of raising asynchronous exceptions across threads from user code, so those...
7
9716
by: Siv | last post by:
Hi, I have a stored procedure that I want to execute and then wait in a loop showing a timer whilst it completes and then carry on once I get notification that it has completed. The main reason for this being to stop the user thinking the application has frozen when in fact it is just waiting for a long SP to complete. Another reason for doing it like this is that I also have had a problem in the past where the SP takes longer than the...
0
1757
by: Bishoy George | last post by:
Hi, I have a asp.net 2.0 web application. I want to implement the asynchronous model through http handler in web.config ------------------------------------------------------------------------------------------------------------------------- My web.config file: ---------------------
6
7007
by: Pat B | last post by:
Hi, I'm writing my own implementation of the Gnutella P2P protocol using C#. I have implemented it using BeginReceive and EndReceive calls so as not to block when waiting for data from the supernode. Everything I have written works fine sending and receiving uncompressed data. But now I want to implement compression using the deflate algorithm as the Gnutella protocol accepts: Accept-Encoding: deflate Content-Encoding: deflate in the...
0
9422
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10035
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9851
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8863
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7403
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.