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

Async callbacks in C#

Hi,
I encountered a problem trying to write a code that does the following:

1. Main GUI thread invokes a delegate using BeginInvoke.
2. A secondery thread (from the application's managed thread pool)
starts executing and BeginInvoke returns.
3. Main GUI thread remains responsive while the secondery thread
executes in the background.
4. Secondery thread finishes its execution.
5. Main GUI thread is notified of the completion of the action, and an
event is raised / a callback function executes IN THE MAIN GUI THREAD.

To phase 4 everything works fine; The main thread is responsive and the
secondery thread executes in the background.
Now my problem pops - How can I notify the main thread that the
secondery thread finished its execution, so that it could run a method?
I tried using the callback method parameter of the BeginInvoke method,
but from a little check I performed, it seems that the code executes in
the secondery thread and not in the main GUI thread. If my program was
written in C++, I could have used a window message to notify my main
thread of the completion of the action. The .NET framework does not
contain a PostMessage method to add a message to the main thread's
message pump, and I would really like to avoid using p/invoke. What is
the .NET way to do it?

Many thanks,
John Tiffany.

Nov 17 '05 #1
1 2737
John,

In .NET, you would create a delegate that has the signature of the
method that you want to call on the UI thread. Then, from your worker
thread, you would create an instance of that delegate, and pass it to the
BeginInvoke method on a control that was created on that thread, along with
any parameters that are to be passed to the delegate.

The control will make the call to the delegate on the UI thread, and
update it properly.

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

<em********@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi,
I encountered a problem trying to write a code that does the following:

1. Main GUI thread invokes a delegate using BeginInvoke.
2. A secondery thread (from the application's managed thread pool)
starts executing and BeginInvoke returns.
3. Main GUI thread remains responsive while the secondery thread
executes in the background.
4. Secondery thread finishes its execution.
5. Main GUI thread is notified of the completion of the action, and an
event is raised / a callback function executes IN THE MAIN GUI THREAD.

To phase 4 everything works fine; The main thread is responsive and the
secondery thread executes in the background.
Now my problem pops - How can I notify the main thread that the
secondery thread finished its execution, so that it could run a method?
I tried using the callback method parameter of the BeginInvoke method,
but from a little check I performed, it seems that the code executes in
the secondery thread and not in the main GUI thread. If my program was
written in C++, I could have used a window message to notify my main
thread of the completion of the action. The .NET framework does not
contain a PostMessage method to add a message to the main thread's
message pump, and I would really like to avoid using p/invoke. What is
the .NET way to do it?

Many thanks,
John Tiffany.

Nov 17 '05 #2

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

Similar topics

0
by: eminemabcd | last post by:
Hi, I encountered a problem trying to write a code that does the following: 1. Main GUI thread invokes a delegate using BeginInvoke. 2. A secondery thread (from the application's managed thread...
1
by: Chris Morse | last post by:
WARNING: Verbosity: skip to the very bottom paragraph for succinct version of my question.) Hi- I can't seem to find an answer to this. I am playing around with a variation of the ".NET...
2
by: Ann Huxtable | last post by:
Hi, I want to do two types of async callbacks in C#. One involves spawning a worker thread, and the other does not invlve threads: Case 1 (Span new thread) ---------------------------- class...
0
by: hynek.cihlar | last post by:
A strange behaviour thatI found in ASP.NET 2.0. I am trying to issue a callback request (handled by ICallbackEventHandler and RaiseCallbackEvent) and a regular GET request in the client browser...
6
by: Shak | last post by:
Hi all, Three questions really: 1) The async call to the networkstream's endread() (or even endxxx() in general) blocks. Async calls are made on the threadpool - aren't we advised not to...
15
by: dennis.richardson | last post by:
Greetings all. Here's a problem that's been driving me nuts for the last 48 hours. I'm hoping that someone has come across this before. I have a C# Application that reads a UDP broadcast...
6
AMaxin
by: AMaxin | last post by:
So I have been learning about MultiThreading in VB.NET, and I did up some code based on an example I found HERE Everything seemed to work wonderfully, EXCEPT when I started to get into performing...
1
by: jojoba | last post by:
Hi I am running asp.net ajax (vs 2008) I have the following webservice listed below. However, i have no idea how to call this from javascript. Originally, before i started using the BeginXXX...
2
by: dougmcmurtry | last post by:
I have an Asynchronous socket that sends data to a server for credit card approvals. The socket is kept alive by the server through a heartbeat that sends a "beat" every 90 seconds. Trouble is that...
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
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
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
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...
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
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...

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.