468,532 Members | 1,651 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,532 developers. It's quick & easy.

VB6 to C# .NET non-blocking ADO approach

Hi,

In my VB6 application I'm using a class/object that is using full-async ADO.

I can start multiple queries, the class stores the ADODB.Recordset object in
an array and waits for the QueryComplete event. This will set the result and
flag 'the query is finished' in the array.

In my WaitForResult() method I wait till the flag 'query is finished' is set
and return to the caller. While waiting I'm calling DoEvents and delay
function to prevent blasting the CPU so my VB6 application keeps responding
(pseudo code at the end of this posting).

In .NET I want to create about the same object as in VB and prevent
application to hang during long running queries. From a MDI application I
want to be able to run multiple queries at the same time.

One advantage is that in C# I'm be able to use threads, I'm using them in
C++ project too, but I'm not sure this is the best way to go. Using events
is IMO not a good approach because I should break-up my code when retrieving
some data.

When using threads I can use a semaphores or event handlers, but while
waiting them my application will block. Seems te be no other way then
calling a messagepump while waiting, is there a preffered way to do this in
..NET?

Another idea is split up the code for queries that almost return immediately
(run in same thread/blocking) and long running queries (creates another
thread/non-blocking).

What is a good approach for writing such class around ADO.NET? And what is a
good approach to prevent desktop applications from blocking?

TIA,

Rene

In pseudo code:

If class.QuerySomething ("Select bla bla" ) Then
' Query successful
Else
' Report error class.GetLastErrMSg()

class.QuerySomething ( Query, Parameters )
Declare rs as new ADODB.Recordset
Open query

QuerySomething = WaitForResult (rs)
class.WaitForResult ( rs )
index = StoreInArray (rs)

While not QueryComplete (index)
DoEvents
' Some other stuff
Wend

WaitForResult = GetResults (index)
FreeObjectInArray (index)
p.s. please note that using this way of programming has a lot of pitfalls,
all forms keeps responding so you must prevent re-entrance of events.

Nov 15 '05 #1
2 2025
Rene,

I believe that you are taking the wrong approach to this. Basically,
you should be using an event-based mechanism, instead of trying to mimic
responsiveness while waiting for a response. Basically, you should be
creating the other thread, and performing your work on that thread. Once
the work is done, you can send a notification to the UI thread for updating
(you don't have to worry about it being tied up), and then proceed from
there.

This is pretty much the model for any multi-threaded application. I
believe that the use of DoEvents in this situation is incorrect, and that
using a thread is the correct way to go. Yes, it breaks up your code, but
that shouldn't be a problem if you design your application correctly. Just
look at this as another step in the overall process. All of the steps don't
have to occur in the scope of one function call. If that was the case, we
would have these super-functions which would be quite unwieldly to use.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Rene" <no@spam.nl> wrote in message
news:O4**************@TK2MSFTNGP10.phx.gbl...
Hi,

In my VB6 application I'm using a class/object that is using full-async ADO.
I can start multiple queries, the class stores the ADODB.Recordset object in an array and waits for the QueryComplete event. This will set the result and flag 'the query is finished' in the array.

In my WaitForResult() method I wait till the flag 'query is finished' is set and return to the caller. While waiting I'm calling DoEvents and delay
function to prevent blasting the CPU so my VB6 application keeps responding (pseudo code at the end of this posting).

In .NET I want to create about the same object as in VB and prevent
application to hang during long running queries. From a MDI application I
want to be able to run multiple queries at the same time.

One advantage is that in C# I'm be able to use threads, I'm using them in
C++ project too, but I'm not sure this is the best way to go. Using events
is IMO not a good approach because I should break-up my code when retrieving some data.

When using threads I can use a semaphores or event handlers, but while
waiting them my application will block. Seems te be no other way then
calling a messagepump while waiting, is there a preffered way to do this in .NET?

Another idea is split up the code for queries that almost return immediately (run in same thread/blocking) and long running queries (creates another
thread/non-blocking).

What is a good approach for writing such class around ADO.NET? And what is a good approach to prevent desktop applications from blocking?

TIA,

Rene

In pseudo code:

If class.QuerySomething ("Select bla bla" ) Then
' Query successful
Else
' Report error class.GetLastErrMSg()

class.QuerySomething ( Query, Parameters )
Declare rs as new ADODB.Recordset
Open query

QuerySomething = WaitForResult (rs)
class.WaitForResult ( rs )
index = StoreInArray (rs)

While not QueryComplete (index)
DoEvents
' Some other stuff
Wend

WaitForResult = GetResults (index)
FreeObjectInArray (index)
p.s. please note that using this way of programming has a lot of pitfalls,
all forms keeps responding so you must prevent re-entrance of events.


Nov 15 '05 #2

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> schreef
in bericht >

This is pretty much the model for any multi-threaded application. I
believe that the use of DoEvents in this situation is incorrect, and that
using a thread is the correct way to go.
The use of DoEvents in VB6 is the only way since VB6 is not multi-threading
and it is not possible to use ADODB Recordsets among processes. And nothing
worse then users hitting alt-ctrl-del because the application is not
responding.
Yes, it breaks up your code, but
that shouldn't be a problem if you design your application correctly.


Because the application would use a lot of forms and queries I'm afraid the
application can easily be messed-up when using threads and events for each
data-retrieval making the code hard to understand. There is a lot of classic
data-retrieval (open recordset, while not .eof addrecord/populate) that can
be done without using threads/events, so I will make a design that only for
long running queries threads will be used. This approach might lockup the
application very shortly in some situations when the server is very busy.

Is the ADO NextRecords() still available in .NET?

Rene


Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by lothar | last post: by
25 posts views Thread by Yves Glodt | last post: by
32 posts views Thread by Adrian Herscu | last post: by
8 posts views Thread by Bern McCarty | last post: by
14 posts views Thread by Patrick Kowalzick | last post: by
399 posts views Thread by =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.