473,414 Members | 1,632 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,414 software developers and data experts.

Is there any way to call asynchronously WITHOUT using the thread pool ?

I miss the old way when asynchronous calls are driven my window messages.

Yes a thread pool might greatly improve application performance but there
are time I don't want my event to be fire in a thread differ from the
calling thread. When a delegate is called in another thread it could be
frustrating for I have to consider thread occurrence issues in all the codes
it may invoke along my event chain. Sure the "Control.BeginInvoke()" could
be a work around but it is not always a good solution for I have to create
and initialize a Control object in my main thread and shared for all other
classes.

Is there any way my "OnReceive()" TCP message handler to be called
aschronously in my main thread by default ??

Thanks in advance!

Jun 27 '06 #1
3 1570
Sidanalism wrote:
Is there any way my "OnReceive()" TCP message handler to be called
aschronously in my main thread by default ??


You can call *any* method asynchronously using a delegate. I'm not
sure about an event handler however. I suppose inside the OnReceive
event handler, you can call another method asynchronously which does
the actual work:

VB.Net code:

'The sig for this delegate matches "SubThatDoesTheWork" below

Private Delegate Sub InvokerDelegate(...)

Private Sub OnReceive(...)
Dim Invoker As New InvokerDelegate(AddressOf SubThatDoesTheWork)

'This calls the sub asynchronously
Dim AsyncResult As IAsyncResult
AsyncResult = Invoker.BeginInvoke(Nothing, Nothing)

'Do other work here while the sub is being executed

'You should always call EndInvoke. This could be done elsewhere as
well
'If the sub is not finished, EndInvoke will wait until it is.
Invoker.EndInvoke(AsyncResult)
End Sub

Private Sub SubThatDoesTheWork(...)
'Actual work here
End Sub
Take care that "SubThatDoesTheWork" does not try to access the UI
improperly. I'm not sure if that would be a cross thread call, in
which case it would have to be marshalled correctly to the UI thread.

Hope this helps,

Chris

Jun 27 '06 #2
Hi Sidanalism,

In a .NET 2.0 Windows Forms app you can use this method:

System.Windows.Forms.WindowsFormsSynchronizationCo ntext.Current.Post

to run code asynchronously on the UI thread without having to create a
Control.

"Sidanalism" <me@sidanla.com> wrote in message
news:Oq**************@TK2MSFTNGP04.phx.gbl...
I miss the old way when asynchronous calls are driven my window messages.

Yes a thread pool might greatly improve application performance but there
are time I don't want my event to be fire in a thread differ from the
calling thread. When a delegate is called in another thread it could be
frustrating for I have to consider thread occurrence issues in all the
codes
it may invoke along my event chain. Sure the "Control.BeginInvoke()" could
be a work around but it is not always a good solution for I have to create
and initialize a Control object in my main thread and shared for all other
classes.

Is there any way my "OnReceive()" TCP message handler to be called
aschronously in my main thread by default ??

Thanks in advance!

Jun 27 '06 #3
Hi Chris,

Delegate BeginInvoke uses the ThreadPool.

"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@x69g2000cwx.googlegr oups.com...
Sidanalism wrote:
Is there any way my "OnReceive()" TCP message handler to be called
aschronously in my main thread by default ??


You can call *any* method asynchronously using a delegate. I'm not
sure about an event handler however. I suppose inside the OnReceive
event handler, you can call another method asynchronously which does
the actual work:

VB.Net code:

'The sig for this delegate matches "SubThatDoesTheWork" below

Private Delegate Sub InvokerDelegate(...)

Private Sub OnReceive(...)
Dim Invoker As New InvokerDelegate(AddressOf SubThatDoesTheWork)

'This calls the sub asynchronously
Dim AsyncResult As IAsyncResult
AsyncResult = Invoker.BeginInvoke(Nothing, Nothing)

'Do other work here while the sub is being executed

'You should always call EndInvoke. This could be done elsewhere as
well
'If the sub is not finished, EndInvoke will wait until it is.
Invoker.EndInvoke(AsyncResult)
End Sub

Private Sub SubThatDoesTheWork(...)
'Actual work here
End Sub
Take care that "SubThatDoesTheWork" does not try to access the UI
improperly. I'm not sure if that would be a cross thread call, in
which case it would have to be marshalled correctly to the UI thread.

Hope this helps,

Chris

Jun 27 '06 #4

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

Similar topics

2
by: Julia | last post by:
Hi, I am using Asynchronously Delegates,and I wonder Can I call EndInvoke to end the processing before THE Asynchronously action ended? Thanks.
2
by: Rakesh | last post by:
Hey, I am using delegates for asynchoronus communication. Its internally use threads from thread pool. I want to know whether this thread pool is on Application level or System level? If...
3
by: Dave | last post by:
I have a piece of hardware connected via serial port. I need to send a variety of commands to the box and route the responses back from the receive thread to the correct method call. So e.g. I...
4
by: Tim Gallivan | last post by:
Hello group, I'm trying to develop a proof of concept webservice which asynchronously calls a function in a DLL. The function raises an event when it is finished, and works when used as part of...
5
by: Paul Hasell | last post by:
Hi, I'm trying to invoke a web method asynchronously but just can't seem to get it to tell me when it has finished! Below is the code I am (currently) using: private void...
5
by: Stephen Barrett | last post by:
I have read many threads related to async fire and forget type calls, but none have addressed my particular problem. I have a webpage that instantiates a BL object and makes a method call. The...
4
by: Paul | last post by:
Hi, I've been struggling with this today, I'm developing a DotNet2.0 website in C# that needs to call a long running data query. Obviously this is a good candidate for an Asynchronous call, so...
15
by: colin | last post by:
Hi, Im familiar with c,c++ etc, and Ive spent a week trying to write my first app in c# it works reasonably well, but im having difficulty getting to grips with inter thread signalling etc. I...
6
by: HolyShea | last post by:
All, Not sure if this is possible or not - I've created a class which performs an asynchronous operation and provides notification when the operation is complete. I'd like the notification to be...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.