473,396 Members | 1,918 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,396 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
this

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 2131
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".

Regards,
Thi

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.

Jon

Nov 24 '05 #3

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

Similar topics

4
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;...
3
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...
2
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...
1
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?
10
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...
2
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...
1
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.
0
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...
2
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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.