473,467 Members | 1,398 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help required: Threading and events/delegates

I am stumped at the moment by something, and the searching I've done
hasn't led to a clear answer. Here is what I want to do:

I have two threads: thread A and thread B. I want Thread B to raise
an event that will be handled within the context of thread A. By
default, an event raised by Thread B will be processed within thread
B's context. That's not what I want.

What is the best / simplest way to accomplish this? I've looked at
using delegates and BeginInvoke, but the way I read it that creates
yet another thread using the thread pool- I want to hand the event to
a specific, already running thread.

Thoughts?

Kelly Adams
Nov 20 '05 #1
9 1053
This might help.

http://msdn.microsoft.com/library/de...isualBasic.asp

hth
Eric
"Kelly Adams" <ke***********@yahoo.com> wrote in message
news:3j********************************@4ax.com...
I am stumped at the moment by something, and the searching I've done
hasn't led to a clear answer. Here is what I want to do:

I have two threads: thread A and thread B. I want Thread B to raise
an event that will be handled within the context of thread A. By
default, an event raised by Thread B will be processed within thread
B's context. That's not what I want.

What is the best / simplest way to accomplish this? I've looked at
using delegates and BeginInvoke, but the way I read it that creates
yet another thread using the thread pool- I want to hand the event to
a specific, already running thread.

Thoughts?

Kelly Adams

Nov 20 '05 #2
This has come up before and I didn't give a great answer, but I think I have
a way to do it now.

Here we go.

On your Thread A class create an object of type
System.windows.forms.control, we only want it for one thing, and that is its
Invoke method.

Create the control AFTER you have started the thread (i.e. call the
constructor in your entry point of the thread).

Use an event to attach between Thread A and Thread B

Thread B raises an event, Thread A grabs this event, the context is still
within thread B

Use your created control's Invoke method to invoke it on the thread it was
created (Thread A) and invoke whatever method you wish.

HTH,
CJ
"Kelly Adams" <ke***********@yahoo.com> wrote in message
news:3j********************************@4ax.com...
I am stumped at the moment by something, and the searching I've done
hasn't led to a clear answer. Here is what I want to do:

I have two threads: thread A and thread B. I want Thread B to raise
an event that will be handled within the context of thread A. By
default, an event raised by Thread B will be processed within thread
B's context. That's not what I want.

What is the best / simplest way to accomplish this? I've looked at
using delegates and BeginInvoke, but the way I read it that creates
yet another thread using the thread pool- I want to hand the event to
a specific, already running thread.

Thoughts?

Kelly Adams

Nov 20 '05 #3
"CJ Taylor" <[cege] at [tavayn] dit commmmm> wrote:
This has come up before and I didn't give a great answer, but I think I have
a way to do it now.

Here we go.

On your Thread A class create an object of type
System.windows.forms.control, we only want it for one thing, and that is its
Invoke method.

Create the control AFTER you have started the thread (i.e. call the
constructor in your entry point of the thread).

Use an event to attach between Thread A and Thread B

Thread B raises an event, Thread A grabs this event, the context is still
within thread B

Use your created control's Invoke method to invoke it on the thread it was
created (Thread A) and invoke whatever method you wish.


Hmmm: interesting. It seems a bit "roundabout", but it might just do
the trick. I'll give it a go in my little test routine: thanks for
the idea!

Nov 20 '05 #4
"Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> wrote:
This might help.

http://msdn.microsoft.com/library/de...isualBasic.asp

hth
Eric


Eric, I've looked over that set of documentation before. Perhaps I am
missing something, but the only reference to passing values between
threads that I can see in that material is here:
http://msdn.microsoft.com/library/de...procedures.asp

And guess what? It uses an event raised by the worker thread to
notify the calling thread that the worker thread has finished some
task...but that event would be executed within the context of the
worker thread. Write a small piece of sample code as they have
described it, with console.writelines to display the thread ID
executing at each stage of execution and you will see what I mean.

Is there some further documentation in this material that you think
answers my question?

Nov 20 '05 #5
I don't know how much more direct you want to get =) Invoke is the only
control I know that does the plumbing between crossing thread contexts. I
suppose you could write that yourself through the API, but then again, who
wants to deal with unmanaged right now.
"Kelly Adams" <ke***********@yahoo.com> wrote in message
news:49********************************@4ax.com...
"CJ Taylor" <[cege] at [tavayn] dit commmmm> wrote:
This has come up before and I didn't give a great answer, but I think I havea way to do it now.

Here we go.

On your Thread A class create an object of type
System.windows.forms.control, we only want it for one thing, and that is itsInvoke method.

Create the control AFTER you have started the thread (i.e. call the
constructor in your entry point of the thread).

Use an event to attach between Thread A and Thread B

Thread B raises an event, Thread A grabs this event, the context is still
within thread B

Use your created control's Invoke method to invoke it on the thread it wascreated (Thread A) and invoke whatever method you wish.


Hmmm: interesting. It seems a bit "roundabout", but it might just do
the trick. I'll give it a go in my little test routine: thanks for
the idea!

Nov 20 '05 #6
excuse me... Invoke is the only method... Control is the only class I know
that contains it...
"CJ Taylor" <[cege] at [tavayn] dit commmmm> wrote in message
news:Oo**************@TK2MSFTNGP10.phx.gbl...
I don't know how much more direct you want to get =) Invoke is the only
control I know that does the plumbing between crossing thread contexts. I
suppose you could write that yourself through the API, but then again, who
wants to deal with unmanaged right now.
"Kelly Adams" <ke***********@yahoo.com> wrote in message
news:49********************************@4ax.com...
"CJ Taylor" <[cege] at [tavayn] dit commmmm> wrote:
This has come up before and I didn't give a great answer, but I think I havea way to do it now.

Here we go.

On your Thread A class create an object of type
System.windows.forms.control, we only want it for one thing, and that is
its
Invoke method.

Create the control AFTER you have started the thread (i.e. call the
constructor in your entry point of the thread).

Use an event to attach between Thread A and Thread B

Thread B raises an event, Thread A grabs this event, the context is
stillwithin thread B

Use your created control's Invoke method to invoke it on the thread it

wascreated (Thread A) and invoke whatever method you wish.


Hmmm: interesting. It seems a bit "roundabout", but it might just do
the trick. I'll give it a go in my little test routine: thanks for
the idea!


Nov 20 '05 #7
"CJ Taylor" <[cege] at [tavayn] dit commmmm> wrote:
I don't know how much more direct you want to get =) Invoke is the only
control I know that does the plumbing between crossing thread contexts. I
suppose you could write that yourself through the API, but then again, who
wants to deal with unmanaged right now.

I'm not disagreeing with you on that point. What is problematic (and
roundabout) for me is that my application has no user interface. Its
a service (currently I'm debugging it as a console application).

To make your approach work, I have to figure out how to instantiate a
control properly in a non-UI application- it needs a window handle
(discovered that right away). I have to add the System.Windows.Forms
class, and instantiate a form I guess, and add the control to that
form...at run time, the compiler still complains about not having a
window handle...I must be missing something here, but I assume its
doable.

That all said, I have to assume that there is some way to do this
(make an event execute in the context of a thread other than the one
generating the event) without using a form control. It seems like it
*should* be easy to do...

Nov 20 '05 #8
In article <p9********************************@4ax.com>, Kelly Adams wrote:
"CJ Taylor" <[cege] at [tavayn] dit commmmm> wrote:
I don't know how much more direct you want to get =) Invoke is the only
control I know that does the plumbing between crossing thread contexts. I
suppose you could write that yourself through the API, but then again, who
wants to deal with unmanaged right now.

I'm not disagreeing with you on that point. What is problematic (and
roundabout) for me is that my application has no user interface. Its
a service (currently I'm debugging it as a console application).

To make your approach work, I have to figure out how to instantiate a
control properly in a non-UI application- it needs a window handle
(discovered that right away). I have to add the System.Windows.Forms
class, and instantiate a form I guess, and add the control to that
form...at run time, the compiler still complains about not having a
window handle...I must be missing something here, but I assume its
doable.


Use the System.Windows.Forms.NativeWindow class to generate a window
handle.

--
Tom Shelton [MVP]
Nov 20 '05 #9
Thank you tom =)

"Tom Shelton" <to*@mtogden.com> wrote in message
news:uS**************@TK2MSFTNGP10.phx.gbl...
In article <p9********************************@4ax.com>, Kelly Adams

wrote:
"CJ Taylor" <[cege] at [tavayn] dit commmmm> wrote:
I don't know how much more direct you want to get =) Invoke is the only
control I know that does the plumbing between crossing thread contexts. Isuppose you could write that yourself through the API, but then again, whowants to deal with unmanaged right now.

I'm not disagreeing with you on that point. What is problematic (and
roundabout) for me is that my application has no user interface. Its
a service (currently I'm debugging it as a console application).

To make your approach work, I have to figure out how to instantiate a
control properly in a non-UI application- it needs a window handle
(discovered that right away). I have to add the System.Windows.Forms
class, and instantiate a form I guess, and add the control to that
form...at run time, the compiler still complains about not having a
window handle...I must be missing something here, but I assume its
doable.


Use the System.Windows.Forms.NativeWindow class to generate a window
handle.

--
Tom Shelton [MVP]

Nov 20 '05 #10

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

Similar topics

77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
22
by: Jeff Louie | last post by:
Well I wonder if my old brain can handle threading. Dose this code look reasonable. Regards, Jeff using System; using System.Diagnostics; using System.IO; using System.Threading;
4
by: Phil G. | last post by:
I was recently struggling to adapt an example I have using delegate methods, IasynResult and AsynCallback. Doing a little research I came across an example, which in fact was being used to...
13
by: Praveen | last post by:
trying to learn plymorphism. My sample is public class Class1 { public static void Main(string args) { Cls1 x = new Cls1(); Cls2 y = new Cls2(); Cls3 y = new Cls3();
5
by: sajin | last post by:
Hi All.. We are using VB .Net 2005 for implementing an API. API needs to generate events. For this client wants us to use Windows Callback (delegate implementation). The intention of using...
4
by: TampaWebDevelopment | last post by:
I am writing a FTP backup service for my servers. Right now, it works well, but it is single threaded and takes a couple of hours to complete a backup of the websites directory, even to a server on...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
1
by: Rymfax | last post by:
I was hoping someone could help me figure something. I have a class that is a downloader and I've created some events on it so that a Windows Form can use those to be notified when to update...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
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...
1
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
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
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.