473,790 Members | 2,437 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to marshal to UI thread in managed code

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 generally
not a very clean solution.

If I were writing in unmanaged code, I would likely use
CoMarshalInterT hreadInterfaceI nStream. I could also post a message back to
the main thread. Is something like this possible in managed code or are
there ways other than Control.Invoke to pass an interface pointer to a
different thread?

One other potential challenge is that my code is not responsible for
starting the secondary thread; it is started by the framework in which my
code executes. For a detailed discussion of what exactly I'm trying to
accomplish, see
http://groups.google.ca/group/micros...3ce7d2f03e67e3.

Thanks!
Notre
Dec 12 '05 #1
14 6963
Hi,

"Notre Poubelle" wrote:
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 generally
not a very clean solution.

If I were writing in unmanaged code, I would likely use
CoMarshalInterT hreadInterfaceI nStream. I could also post a message back to
the main thread. Is something like this possible in managed code or are
there ways other than Control.Invoke to pass an interface pointer to a
different thread?

One other potential challenge is that my code is not responsible for
starting the secondary thread; it is started by the framework in which my
code executes. For a detailed discussion of what exactly I'm trying to
accomplish, see
http://groups.google.ca/group/micros...3ce7d2f03e67e3.

Thanks!
Notre


If all you need is synchronization against non UI code, try the lock
operator to make code MT safe or use other .NET synchronization classes like
Mutex or ManualResetEven t and AutoResetEvent.

Honestly I don't see where your program is going. You want to call something
and you can only do it on the main UI thread but there are no controls not
even a form? Then why *do* you have a UI thread? It is hard to give advice if
the program or the intent is unclear.

Kind regards,
--
Tom Tempelaere.
Dec 13 '05 #2
Hi Tom,

Thank you for your reply. I am writing a VSPackage (a DLL that extends
Visual Studio). My package does not introduce any UI of its own. As my DLL
runs inside Visual Studio, there is of course a UI, but this UI is managed by
Visual Studio. None of the core UI elements in Visual Studio are written
using managed control elements; so I cannot just get a handle of some control
in the Visual Studio UI and call Control.Invoke on it.

As described in the other post, I need to use methods on the
IVsMonitorSelec tion interface. Visual Studio has an architecture where you
can request a service, which gives you back an interface, such as
IVsMonitorSelec tion. As I am integrating with Visual Studio, there are
certain scenarios (such as when the build completes) where my DLL is called
back on a thread other than the main UI thread. In this case I need to
perform some logic that requires the services provided by the
IVsMonitorSelec tion interface. Unfortunatey, the IVsMonitorSelec tion
interface is only designed to work on the main UI thread. Therefore, I need
to switch thread context back to the main UI thread to use the
IVsMonitorSelec tion interface. One way to achieve this is to create a hidden
Windows Form of my own and call form.Invoke. This is not the cleanest
approach (I hope!) and has caused some secondary problems (see the other post
for details). So, I am looking for some way to do what I used to be able to
do in unmanged code: marshal an interface pointer to my own thread. Or, if I
knew how, I could post a message to the main UI thread where I could call
IVsMonitorSelec tion.

As noted, I don't explicitly create the worker thread, so this could add
some complexity to the problem.

Hopefully this clarifies the goal of what I'm trying to achieve.

Notre
Dec 13 '05 #3
Hi

From your description, I understand that you have discussed the VSIP
problem with our another SP in that google link your provide.
Since I am not familar with IDE extension development, based on my
knowledge about thread marshaling in .NET, so far we do use Control.Invoke
to marshal the call across the threads, which is internally implemented by
SendMessge to the thread where the control lies. As for commonly COM
apartment and thread access is implement ed internally by CLR underlying.
There is no such an equivalent .NET call with
CoMarshalInterT hreadInterfaceI nStream related call.

If the Control.Invoke did not work for you, I think you need to contact
MSPSS directly.
http://support.microsoft.com

If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Dec 14 '05 #4
Hi Charles,

Thank you for your reply. I don't see the attached example (maybe it's the
MSDN newsreader that's blocking it). Would it be possible for you to email
it to me? My email address is no************@ online.yahoo.co m; please remove
the .online from the address.

Thanks,

Notre
Dec 14 '05 #5
Hi again Charles,

I used Outlook Express (rather than the web-based news reader) and was able
to get your attachment, so no need to email it to me. Thank you!

I'm rather confused by the example though. Not being a VB expert, I may be
reading the sample wrong, but it seems to me that the code sample is starting
a new worker thread and when the worker thread completes, it calls a delegate
that's handled on the form. This works great in your example.

My problem is, that I don't have a form or anything else that dervies from
Control, from which I can call Invoke. My application doesn't create any UI,
and the UI that drives my code is not managed, so I can't grab hold of
anything and reuse it for the purpose of calling Invoke.

Notre
Dec 14 '05 #6
Hi Notre

The principal of the example is that you don't need to know the caller in
order to raise an event on its own thread.

The key is in OnCompleted(), which would run on your worker thread. You will
see that it does not know anything about the main thread, but still manages
to raise an event on it.

Is that what you are trying to achieve, or have I misunderstood?

Charles
"Notre Poubelle" <no************ @online.nospam> wrote in message
news:48******** *************** ***********@mic rosoft.com...
Hi again Charles,

I used Outlook Express (rather than the web-based news reader) and was
able
to get your attachment, so no need to email it to me. Thank you!

I'm rather confused by the example though. Not being a VB expert, I may
be
reading the sample wrong, but it seems to me that the code sample is
starting
a new worker thread and when the worker thread completes, it calls a
delegate
that's handled on the form. This works great in your example.

My problem is, that I don't have a form or anything else that dervies from
Control, from which I can call Invoke. My application doesn't create any
UI,
and the UI that drives my code is not managed, so I can't grab hold of
anything and reuse it for the purpose of calling Invoke.

Notre

Dec 14 '05 #7
Hi Charles.

I guess I've got two things in mind. One, my worker thread shouldn't know
about the caller. Your example does a good job of demonstrating that.

The second thing is not so much a goal as a constraint. My code does not
create a .NET UI nor does it have access to the .NET UI anywhere else. In
your example, although the worker thread doesn't know about the main UI
thread, (I believe) it does know that there's a Windows Form control, from
which Invoke can be called to switch execution to the main thread. I can't
do this, because no Windows Form nor anything else deriving from Control
exists. So, there's nothing on which I can call Invoke. Given this, I
cannot use Control.Invoke; I must use some alternate approach like using
CoMarshalInterT hreadInterfaceI nStream (in unmanaged code) or posting a
message to the UI thread (don't know how to do this in managed code).

Notre
Dec 15 '05 #8
Hi Notre

Can you implement ISynchronizeInv oke in your main code? If so, this link may
help

http://discuss.joelonsoftware.com/de...gn.4.244582.16

Charles
"Notre Poubelle" <no************ @online.nospam> wrote in message
news:BB******** *************** ***********@mic rosoft.com...
Hi Charles.

I guess I've got two things in mind. One, my worker thread shouldn't know
about the caller. Your example does a good job of demonstrating that.

The second thing is not so much a goal as a constraint. My code does not
create a .NET UI nor does it have access to the .NET UI anywhere else. In
your example, although the worker thread doesn't know about the main UI
thread, (I believe) it does know that there's a Windows Form control, from
which Invoke can be called to switch execution to the main thread. I
can't
do this, because no Windows Form nor anything else deriving from Control
exists. So, there's nothing on which I can call Invoke. Given this, I
cannot use Control.Invoke; I must use some alternate approach like using
CoMarshalInterT hreadInterfaceI nStream (in unmanaged code) or posting a
message to the UI thread (don't know how to do this in managed code).

Notre

Dec 15 '05 #9
Hi Charles,

Thanks for the response and the link. I did review the reference, but to be
quite honest I don't know if I followed everything that was said there.

I'm not sure if I could implement the ISynchronizeInv oke interface. The
only implementation I could find of this was in System.Windows. Forms.Control.
I started to look at the Invoke method with a disassembler, but got lost in
all the details. I'd hate to have to redo all the work MS did in
implementing this, although maybe that's the only way, short of creating a
temporary form and (somehow) resolving the other problems that I've run into
using that approach.

Notre
Dec 16 '05 #10

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

Similar topics

2
2046
by: awk | last post by:
Hi All I have a com dll written in VB6 (it's a User Function Library for my crystal reports - this allows me to write custom functions for Crystal which can be applied in Crystal formulas - none of this is relevant to the problem though (((I think)))). Anyway, the VB dll has one method that takes a string as an argument and returns a string. It passes the string arg to a C# library which is where the problem lies. The string arg...
4
8452
by: Paul | last post by:
I'm porting a C++ function which reads a file in binary mode, sometimes reading 2 bytes into an unsigned short, 4 bytes into a long, etc into a C# implementation. When using the FileStream.Read function, I'd like to code it to read the size of a C# ushort, for example. The documentation says the sizeof() is only supported in unsafe mode. Is there any equivalent to sizeof() that I can use in managed code?
0
1703
by: nygiantswin2005 | last post by:
I would like to know how do I pass a pointer to a struct from managed code to unmanaged code. For example if I create structure like this in managed code. StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi ) ] public struct MYSTRUCT { public String Text;
0
2694
by: Johannes Unfried | last post by:
Problem Best practice needed to marshal STL data from managed code to unmanaged code & vice vers Details managed code is written in managed C++ & accesses the unmanaged code (i.e. lives in a mixed mode DLL unmanaged code is written in unmanaged C++ & resides in an unmanaged DL Product used is VS.NET 200 Please tell me what is the best practice for getting and setting data fro unmanaged to managed part when I have a class with...
6
1369
by: Daren Hawes | last post by:
Hi, My web host has a low security setting on the shared .net server I use. I cannot use third party dll's or install assemblies in the GAC. All I need to do is verify that a FTP login is true. I do not need to move files etc, simply check if the ftp details that were collected in a web form actually authenticate with the remote ftp server. Can this be completed using managed code only?
4
2862
by: kimberly.walker | last post by:
Im very new to coding in C++ so use to coding in C#. My question is how to pass some values (string) from unmanaged code to managed code. I have two source files on a win32 console application one I used #pramga managed to change to managed code but I need to get the values from the unmanaged code and pass it to the managed code. Thanks...
6
8016
by: Ananas | last post by:
Hi, My native C++ function creates a dynamic array. I'm marshalling it to managed code and got to delete after. How to make it: c++ code: void CreateArrayInside( pTestStruct &TestStruct, unsigned int &size) {
10
5772
by: =?Utf-8?B?UmF2aSBTaGFua2Fy?= | last post by:
Hi, I have a requirement where I need to call methods written in VB.Net from a C++ code. I have been going crazy reading various articles on the net w.r.t interop and marshalling but an more confused now that before. the signature I need to conform in the VB.Net DLL is int MyAppInitFunc(int argc, char** argv) To this extent I have tried the following
0
956
by: oleop | last post by:
I am trying to call dll from the managed code.eveyrting works OK until i'm trying to use fucntions that use complex (containing arrays of structures) structures as a parameters. Let's say private static extern int myFunc(ref STRUCT2 someStrust);
0
10413
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...
1
10145
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
9986
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
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
7530
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
6769
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5422
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...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2909
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.