Multi-Threading: Accessing Main Thread Objects

M

Mythran

I've read some documentation about accessing objects created in other
threads and the code below is similar to what I've gathered for what I have
to do:

private delegate void SetCursorCallBack(Cursor cursor);

// - Checks to see if the current thread is the creator of the current Form.
If the form was
// created on another thread, call Invoke so the delegate is ran in the
creating thread.
private void SetCursor(Cursor cursor)
{
if (this.InvokeRequired()) {
SetCursorCallBack callBack = new SetCursorCallBack(SetCursor);
Invoke(callBack, cursor);
} else {
this.Cursor = cursor;
}
}

// This event gets fired in the secondary thread (not the thread that
created the form).
private void SomeVarName_SomeEventFiredFromAnotherThread(
object sender,
EventArgs e
)
{
SetCursor(Cursors.Default);
}

The comments in the code above are for YOUR consumption, this isn't exactly
what I have as I have snipped quite a bit of the custom code that is
irrelevant.

Is there a less-involved way to accomplish setting the properties of an
object created in a thread other than the current thread? If not, I can do
this fine, and I understand it, just wondering :)

Thanks,
Mythran
 
M

Marc Gravell

// here could be any thread
this.Invoke((MethodInvoker) delegate
{ // this bit runs on the UI thread
this.Cursor = Cursors.Default;
});

Marc
 
L

Linda Liu[MSFT]

Thanks Marc and Pete for your excellent replies!

Hi Mythran,

Your code to handle the cross-thread operation is correct. But if you want
a convenient way to do that, as Marc and Pete have suggested, you can use
anonymous method and MethodInvoker to do it.

For example:

private void SetCursor(Cursor cursor)
{
this.Invoke((MethodInvoker)delegate { this.Cursor = cursor; });

}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Mythran

Peter Duniho said:
[...]
Is there a less-involved way to accomplish setting the properties of an
object created in a thread other than the current thread? If not, I can
do this fine, and I understand it, just wondering :)

Please see:
http://groups.google.com/group/micr...csharp/browse_thread/thread/c326c7700d2cf8a1/

We just had this discussion. :)

Pete

wow, your right, exactly what I asked...google must not have indexed that
yet, I didn’t get a hit off it before I posted...or maybe I just missed
it...anywho, thanks.

Mythran
 
B

Benjamin Vigneaux

Ok, perhaps i'm going a little bit of topic with my next question... but I
didn't find a appropiate group to post this question and since everyone here
knows a bit of everything perhaps I could get an answer.

Basically what I need if to build a homeserver, well.. at least, set it up,
cause I have 3 computers that I could clusterize ,even though a friend told
me that clusterizing migh not be the best idea form what i have in mind,
which is hosting a website/blog and file server, and the idea is that these
3 computers share one single harddrive.

Which SO would be appropiate to do this (ok, it's pretty obvious that
windows server would be an obvious suggestion, but i can't afford it right
now) perhaps a linux distro? is clusterizing a good idea for this? or
perhaps set it up as some kind of distributed database?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top