473,324 Members | 2,567 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,324 software developers and data experts.

Asynchronous problem

Hi,

I am using the following code

private void menuItemStart_Click(object sender, System.EventArgs e)
{
DataTable tbRand, tbShow;
int randSize;

....
// schema of tbShow is being loaded from an xsd file
tbRand = DrawEngine.GenerateAllDraws(tbRand, randSize, ref tbShow);
dataGridRandomized.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 GeneratingAllDrawsDelegate(DataTable table,
int tot, ref DataTable show);

private void menuItemStart_Click(object sender, System.EventArgs e)
{
...
GeneratingAllDrawsDelegate dc = new
GeneratingAllDrawsDelegate(DrawEngine.GenerateAllD raws);
AsyncCallback cb = new AsyncCallback(getResultAllDraws);
IAsyncResult ar = dc.BeginInvoke(tbRand, randSize, ref tbShow, cb,
null);
...
}

private void getResultAllDraws(IAsyncResult ar)
{
DataTable tbRand, tbShow;
GeneratingAllDrawsDelegate del;

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

del = (GeneratingAllDrawsDelegate) ((AsyncResult)ar).AsyncDelegate;
tbRand = del.EndInvoke(ref tbShow, ar);
dataGridRandomized.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 1550
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 dataGridRandomized.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 getResultAllDraws(IAsyncResult ar)
{
if( dataGridRandomized.InvokeRequired )
{
dataGridRandomized.BeginInvoke(new AsyncCallback(getResultAllDraws), new object[]{ar});
}
else
{
DataTable tbRand, tbShow;
GeneratingAllDrawsDelegate del;

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

del = (GeneratingAllDrawsDelegate) ((AsyncResult)ar).AsyncDelegate;
tbRand = del.EndInvoke(ref tbShow, ar);
dataGridRandomized.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_Click(object sender, System.EventArgs e)
{
DataTable tbRand, tbShow;
int randSize;

...
// schema of tbShow is being loaded from an xsd file
tbRand = DrawEngine.GenerateAllDraws(tbRand, randSize, ref tbShow);
dataGridRandomized.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 GeneratingAllDrawsDelegate(DataTable table,
int tot, ref DataTable show);

private void menuItemStart_Click(object sender, System.EventArgs e)
{
...
GeneratingAllDrawsDelegate dc = new
GeneratingAllDrawsDelegate(DrawEngine.GenerateAllD raws);
AsyncCallback cb = new AsyncCallback(getResultAllDraws);
IAsyncResult ar = dc.BeginInvoke(tbRand, randSize, ref tbShow, cb,
null);
...
}

private void getResultAllDraws(IAsyncResult ar)
{
DataTable tbRand, tbShow;
GeneratingAllDrawsDelegate del;

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

del = (GeneratingAllDrawsDelegate) ((AsyncResult)ar).AsyncDelegate;
tbRand = del.EndInvoke(ref tbShow, ar);
dataGridRandomized.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
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...
3
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...
2
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...
3
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...
5
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...
4
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. ...
8
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...
7
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...
0
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 ...
6
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.