472,371 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

Marshalling to a particular thread

I've written a class which does some long running background processing and
returns multiple results via events. The class has an Execute() method which
creates a thread and runs an internal method _execute() on it.

When events fire they are running on the worker thread. To use this class
from a form with event handlers that manipulate the UI, you have to write

public void MyForm_DocumentReady(object Sender, DocumentReadyEventArgs e) {
if (InvokeRequired)
Invoke(new DocumentReadyDelegate(MyForm_DocumentReady), new object[]
{Sender, e);
else {
//do stuff on UI thread

But I can't expect people to know they have to do that!

Invoke() searches for the root windowed parent of the control on which it is
invoked, and marshals to that thread. If I made the class a derivative of
Control I could do the marshalling internally using Invoke. But this class
won't necessarily be a parented control.

Getting a reference to the thread that calls Execute() is easy. You just
take snaffle a reference to the current thread when entering the Execute()
method. Can anyone tell me how to say, "run this method on that thread"
without using Invoke?
Nov 24 '05 #1
2 2075
Hi Peter,

You can try the approach used by System.IO.FileSystemWatcher class. It
has a property called SynchronizingObject which, if set, is used to
marshall the event handler calls.
But I can't expect people to know they have to do that!

Then they have to set SynchronizingObject to "this". You can see that
in the form designer, when you drag a FileSystemWatcher into a form,
its SynchronizingObject is default to "this".


Nov 24 '05 #2
Peter Wone wrote:
Getting a reference to the thread that calls Execute() is easy. You just
take snaffle a reference to the current thread when entering the Execute()
method. Can anyone tell me how to say, "run this method on that thread"
without using Invoke?

No - there's no such concept, as the thread which you want to run the
method on has to actively be looking for things to run. You can't just
interrupt it and tell it to do something.

A better solution would be to use an ISynchronizeInvoke object which
had to be passed in on construction (or on the call to Execute, or
whatever) if synchronization was required. If UI code wanted to execute
events on the UI thread, it could just pass a control in (as Control
implements ISynchronizeInvoke). Other code may wish to marshall to the
consumer of a queue, for instance, by placing the work item on the
queue being consumed. ISynchronizeInvoke is the .NET interface designed
for just this task.


Nov 24 '05 #3

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

Similar topics

by: Animesh | last post by:
Hi All, I don't know whethher this is possible or not. This is the result of a bad design problem. Here I go; I have a structure like this: typedef struct _s_index_entry { char *doc_id;...
by: Jerry Houston | last post by:
In a winform application, I use worker threads to perform asynchronous tasks (e.g., mail merge with Word). The thread procs need to gather some user options from controls in the UI, and I know...
by: Gregory Goeppel | last post by:
I have a component (with NO user interface) in C# that fires events from a seperate thread. The client of the C# component does processing with UI elements and other non thread safe variables...
by: Steve | last post by:
I have two objects, each on a different thread. Object 1 periodically fires an event, which Object 2 sinks. How can I marshall the event from Object 1 to the thread on Object 2?
by: Bryce Calhoun | last post by:
Hello, First of all, this is a .NET 1.1 component I'm creating. SUMMARY ----------------------- This component that I'm creating is, for all intents and purposes, a document parser (I'm...
by: RJ Lohan | last post by:
Howdy, I have a legacy DLL for which I have a problem marshalling a parameter type of char**. The function header (C++) is as so; extern "C" __declspec(dllexport) int __stdcall...
by: Paul | last post by:
Hi all, I'm trying to trigger an event from threadB which will be processed by threadA. It sounds simple, but I cannot seem to find any documentation about this anywhere.
by: tonymoore20 | last post by:
Hi all, I'm trying to make a C# wrapper class for a commonly used c dll. I'm having problems with one particular function, any help is gratefully accepted. Here is the function call as it is...
by: calenlas | last post by:
Hi all, I'm taking my first steps into C# <--C++ DLL Interop and unfortunately I've run into (what seems to be) a very complicated case as my first task. Perhaps someone here can help me. I...
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.