473,692 Members | 2,260 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to marshal code to the original thread

I have a class that handles events from another class. Unfortunately
these events arrive on a different thread. How can I marshal the code
execution to the thread on which the caller class was created.

'example

public class MyClass
{
private ClassWithEvents cls = new ClassWithEvents ();
public MyClass()
{
cls.Hangup += new HangupEventHand ler(cls_Hangup) ;
}
private void cls_Hangup(obje ct sender, HangupEvent e)
{
// this happens on a different thread
// it needs to be marshalled to the original thread.
}
}
Feb 7 '06 #1
20 14302
Frank,

You can't unless the thread you want to marshal to is an UI thread. If it is
you can use Control.Invoke or Control.BeginIn voke for some of the controls
created in the thread.
--

Stoitcho Goutsev (100)

"Frank Rizzo" <no**@none.co m> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
I have a class that handles events from another class. Unfortunately these
events arrive on a different thread. How can I marshal the code execution
to the thread on which the caller class was created.

'example

public class MyClass
{
private ClassWithEvents cls = new ClassWithEvents ();
public MyClass()
{
cls.Hangup += new HangupEventHand ler(cls_Hangup) ;
}
private void cls_Hangup(obje ct sender, HangupEvent e)
{
// this happens on a different thread
// it needs to be marshalled to the original thread.
} }

Feb 7 '06 #2
Stoitcho,

Well, that's not completely true. You could do it on a non UI thread,
but it would have to be a custom mechanism, and the thread would have to be
processing messages of some sort in a loop (like windows messages do).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Stoitcho Goutsev (100)" <10*@100.com> wrote in message
news:ua******** *****@TK2MSFTNG P09.phx.gbl...
Frank,

You can't unless the thread you want to marshal to is an UI thread. If it
is you can use Control.Invoke or Control.BeginIn voke for some of the
controls created in the thread.
--

Stoitcho Goutsev (100)

"Frank Rizzo" <no**@none.co m> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
I have a class that handles events from another class. Unfortunately
these events arrive on a different thread. How can I marshal the code
execution to the thread on which the caller class was created.

'example

public class MyClass
{
private ClassWithEvents cls = new ClassWithEvents ();
public MyClass()
{
cls.Hangup += new HangupEventHand ler(cls_Hangup) ;
}
private void cls_Hangup(obje ct sender, HangupEvent e)
{
// this happens on a different thread
// it needs to be marshalled to the original thread.
} }


Feb 7 '06 #3
Nicholas Paldino [.NET/C# MVP] wrote:
Stoitcho,

Well, that's not completely true. You could do it on a non UI thread,
but it would have to be a custom mechanism, and the thread would have to be
processing messages of some sort in a loop (like windows messages do).

I am open to that. Would you have some type of an example?

Regards
Feb 7 '06 #4
Frank,

Is this for COM interop, by chance?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Frank Rizzo" <no**@none.co m> wrote in message
news:eT******** ******@TK2MSFTN GP09.phx.gbl...
Nicholas Paldino [.NET/C# MVP] wrote:
Stoitcho,

Well, that's not completely true. You could do it on a non UI
thread, but it would have to be a custom mechanism, and the thread would
have to be processing messages of some sort in a loop (like windows
messages do).

I am open to that. Would you have some type of an example?

Regards

Feb 7 '06 #5
Nicholas Paldino [.NET/C# MVP] wrote:
Frank,

Is this for COM interop, by chance?


Sadly, yes. I am creating a VB6-friendly wrapper around work done by
Asterisk .NET
(http://www.gotdotnet.com/codegallery...8-da800f4f84c3)
and Asterisk Java (http://asterisk-java.sourceforge.net/) fellas. So,
the events work great while in .NET code however, when I pass on an
event to VB6 code, it doesn't work so well, because the event was
originally fired on a different thread.

Regards
Feb 7 '06 #6
Frank,

There is a better way to go about this.

What you want to do is marshal the interface pointer to the global
interface table. This will give you a cookie value (an unsigned int) which
you can store where you need to.

Then, in the event that is fired on another thread, you can get the
cookie, access the global interface table, and get a correctly marshaled
interface pointer to the VB object. Then, you can call your VB object
normally, and then release the interface from the table.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Frank Rizzo" <no**@none.co m> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Nicholas Paldino [.NET/C# MVP] wrote:
Frank,

Is this for COM interop, by chance?


Sadly, yes. I am creating a VB6-friendly wrapper around work done by
Asterisk .NET
(http://www.gotdotnet.com/codegallery...8-da800f4f84c3)
and Asterisk Java (http://asterisk-java.sourceforge.net/) fellas. So, the
events work great while in .NET code however, when I pass on an event to
VB6 code, it doesn't work so well, because the event was originally fired
on a different thread.

Regards

Feb 7 '06 #7
Not sure who's talking about calling a COM object, as far as I understand
it, the client is a VB6 application which calls into a .NET class through
COM interop and is called back from another thread as the original callers
thread.

Willy.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:OF******** *****@TK2MSFTNG P14.phx.gbl...
| Frank,
|
| There is a better way to go about this.
|
| What you want to do is marshal the interface pointer to the global
| interface table. This will give you a cookie value (an unsigned int)
which
| you can store where you need to.
|
| Then, in the event that is fired on another thread, you can get the
| cookie, access the global interface table, and get a correctly marshaled
| interface pointer to the VB object. Then, you can call your VB object
| normally, and then release the interface from the table.
|
| --
| - Nicholas Paldino [.NET/C# MVP]
| - mv*@spam.guard. caspershouse.co m
|
| "Frank Rizzo" <no**@none.co m> wrote in message
| news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
| > Nicholas Paldino [.NET/C# MVP] wrote:
| >> Frank,
| >>
| >> Is this for COM interop, by chance?
| >
| > Sadly, yes. I am creating a VB6-friendly wrapper around work done by
| > Asterisk .NET
| >
(http://www.gotdotnet.com/codegallery...8-da800f4f84c3)
| > and Asterisk Java (http://asterisk-java.sourceforge.net/) fellas. So,
the
| > events work great while in .NET code however, when I pass on an event to
| > VB6 code, it doesn't work so well, because the event was originally
fired
| > on a different thread.
| >
| > Regards
|
|
Feb 7 '06 #8
My understanding is that there is a VB6 wrapper. It could be a COM
object (that is how I would do it) which makes the calls into the .NET
objects, passing itself (or another object) to be called when the event is
fired.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:OS******** ******@TK2MSFTN GP14.phx.gbl...
Not sure who's talking about calling a COM object, as far as I understand
it, the client is a VB6 application which calls into a .NET class through
COM interop and is called back from another thread as the original callers
thread.

Willy.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote
in
message news:OF******** *****@TK2MSFTNG P14.phx.gbl...
| Frank,
|
| There is a better way to go about this.
|
| What you want to do is marshal the interface pointer to the global
| interface table. This will give you a cookie value (an unsigned int)
which
| you can store where you need to.
|
| Then, in the event that is fired on another thread, you can get the
| cookie, access the global interface table, and get a correctly marshaled
| interface pointer to the VB object. Then, you can call your VB object
| normally, and then release the interface from the table.
|
| --
| - Nicholas Paldino [.NET/C# MVP]
| - mv*@spam.guard. caspershouse.co m
|
| "Frank Rizzo" <no**@none.co m> wrote in message
| news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
| > Nicholas Paldino [.NET/C# MVP] wrote:
| >> Frank,
| >>
| >> Is this for COM interop, by chance?
| >
| > Sadly, yes. I am creating a VB6-friendly wrapper around work done by
| > Asterisk .NET
| >
(http://www.gotdotnet.com/codegallery...8-da800f4f84c3)
| > and Asterisk Java (http://asterisk-java.sourceforge.net/) fellas. So,
the
| > events work great while in .NET code however, when I pass on an event
to
| > VB6 code, it doesn't work so well, because the event was originally
fired
| > on a different thread.
| >
| > Regards
|
|

Feb 7 '06 #9
Nicholas Paldino [.NET/C# MVP] wrote:
My understanding is that there is a VB6 wrapper. It could be a COM
object (that is how I would do it) which makes the calls into the .NET
objects, passing itself (or another object) to be called when the event is
fired.

No. It is exactly as Willy said in the parent post.
Feb 7 '06 #10

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

Similar topics

0
1894
by: William Stacey | last post by:
The following code works, but I can't figure out why. I take a struct with two members, a single byte and byte. I then marshal the whole struct to a byte. I create a new struct (without init'ing any members) and marshal the tmp array back to the new struct. The new struct shows the same data. The single type is easy, that just gets copied. The byte ref type is the interesting part. The address (i.e ref) of the original byte gets...
0
1016
by: BilMuh | last post by:
Hello Esteemed Developers and Esteemed Experts, I have a background-running-thread in my application that is designed and developed at Windows Forms (.NET) by using Visual C++ .NET Standard on Windows 2000 Professional O.S. Whenever the required job is done in that background-running-thread, I would like to Enable and Start the Timer to do the related job from the background-running-thread. It is written in MSDN Library that "It...
14
6950
by: Notre Poubelle | last post by:
Hello, I have a worker thread that needs to call a method that only works on the main/UI thread. One way to accomplish this in managed code is to use Control.Invoke. However, in my case I don't have a .NET UI control from which I can call Invoke. I have tried to create a temporary, invisible Windows form and call Invoke against this, and while this sort of worked, it highlighted additional problems (which I have not resolved) and is...
2
1913
by: Pierre Rouleau | last post by:
Hi all, When using Python 2.4.x on a Win32 box, marshal.loads(marshal.dumps(1e66666)) returns 1.0 instead of infinity as it should and does under Python 2.5 (also running on Win32 ). This problem was reported in another thread here by Peter Hansen http://groups-beta.google.com/group/comp.lang.python/browse_frm/thread/5c2b4b2a88c8df4/f216739705c9304f?lnk=gst&q=simplejson&rnum=5#f216739705c9304f Is this considered an important enough...
1
5243
by: sabys | last post by:
I've been noticing a memory leak with the following sample code snippet. Can someone please advise. Have a C# Winforms app with the following code on a button-click event. private void button1_Click(object sender, System.EventArgs e) { IntPtr p1 = new IntPtr(-1); string inputStr;
1
2677
by: Frankie | last post by:
I have been learning about asynchronous method calls and I keep coming across this term, "marshal" and I would like to know what it means - specifically. AFAIKT, it means "send" but I suspect there is a bit more than that. Looking to online docs, MSDN talks about the System.Runtime.Interop.Marshal class - but I don't think that's relevant to what I'm seeing. Maybe it is... The following is an example of the sort of thing I've been...
2
7201
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get Marshal.PtrToStructure to copy the all the data into the "other" array? unsafe public struct DeadReckoning {
0
966
by: Zac Burns | last post by:
Greetings, It seems that marshal.load will lock the problem if the file object (in this case a pipe) is not ready to be read from - even if it's done in a thread. The use case here is in writing a scripting interface for perforce using the -G option (http://www.perforce.com/perforce/doc.072/manuals/cmdref/o.gopts.html#1040647). We are using the sub process module to communicate through pipes to
0
2220
by: yogiam | last post by:
I have a server listening for connections. Client connects and both start streaming webcam images. This works fine as long as there is only one client connected(running as a Thread). When another client connects, the new thread starts streaming as well. The camera is accessed in a mutual exclusive way by locking which fails some times and I get AccessViolationException. Below is the code executing in the thread. The scenario is like this :...
0
8544
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9090
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8968
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8810
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7633
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6462
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4325
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2977
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1961
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.