By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,899 Members | 1,196 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,899 IT Pros & Developers. It's quick & easy.

Multi-Threading: Accessing Main Thread Objects

P: n/a
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

Jun 27 '08 #1
Share this Question
Share on Google+
5 Replies


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

Marc
Jun 27 '08 #2

P: n/a
On Thu, 26 Jun 2008 15:36:43 -0700, Mythran <My*****@community.nospam>
wrote:
[...]
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/micro...6c7700d2cf8a1/

We just had this discussion. :)

Pete
Jun 27 '08 #3

P: n/a
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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 27 '08 #4

P: n/a
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?

Jun 29 '08 #5

P: n/a
On Sun, 29 Jun 2008 09:31:30 -0700, Benjamin Vigneaux <be**@mi.clwrote:
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.
You are off topic and the question appears to have nothing at all to do
with C# or .NET. I am positive that there is a better newsgroup for the
question. Not just so that we keep this newsgroup's signal-to-noise ratio
high, but especially so you can get useful answers to your question.

However well-informed we may be (and you may be overly optimistic on that
point), there's no way we can provide the sort of detailed information a
newsgroup actually on-topic for your question could do for you.

Pete
Jun 30 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.