Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C#

C# C# Discussion

Reply
 
Thread Tools Display Modes
Old 11-03-2006, 01:59 AM   #1 (permalink)
Forum Expert
 
Cheetah2003's Avatar
 
Join Date: Jun 2006
Location: Laramie, Wyoming
Age: 36
Posts: 355
Send a message via ICQ to Cheetah2003 Send a message via AIM to Cheetah2003 Send a message via MSN to Cheetah2003 Send a message via Yahoo to Cheetah2003
Default Cross-thread MessageBox.Show() ?

I've been working on a GUI for RunUO for a couple months now.

I discovered MessageBox.Show() and need to call this method from a few places in World.cs, but I get that irritating '{"Cross-thread operation not valid: Control 'RunUO_UI' accessed from a thread other than the thread it was created on."}'

So I had to disable the checking for this, currently. But I was wondering if anyone knew the proper way to cross-thread MessageBox.Show?

I know how to use delegates to do things like modify the text of a control and so forth, but creating a new control as a child of the form on another thread... I can't seem to locate any documentation on how to do that in MSDN.
__________________
Take a walk on The Wild side.
Cheetah2003 is offline   Reply With Quote
Old 11-07-2006, 03:21 PM   #2 (permalink)
Forum Expert
 
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
Default

there is a class called backgroundworker. there is lots of examples..

google:Cross-thread operation not valid
__________________
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."
noobie is offline   Reply With Quote
Old 11-30-2006, 06:47 AM   #3 (permalink)
Forum Expert
 
o0_Sithid_0o's Avatar
 
Join Date: Nov 2004
Location: Hiding his old posts from Daat99.
Age: 22
Posts: 2,321
Send a message via AIM to o0_Sithid_0o Send a message via MSN to o0_Sithid_0o
Default

You still looking for a way to avoid the "Cross-Thread Operation Not Valid" exception?

I happen to be looking for something to help me out of this paticular issue ... without screwing with some of the other stuff again.

SafeInvokeHelper.cs
Code:
using System;
using System.Collections;
using System.Reflection;
using System.Reflection.Emit;

public class SafeInvokeHelper
{
    static readonly ModuleBuilder builder;
    static readonly AssemblyBuilder myAsmBuilder;
    static readonly Hashtable methodLookup;

    static SafeInvokeHelper()
    {
        AssemblyName name = new AssemblyName();
        name.Name = "temp";
        myAsmBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run);
        builder = myAsmBuilder.DefineDynamicModule("TempModule");
        methodLookup = new Hashtable();
    }

    public static object Invoke(System.Windows.Forms.Control obj, string methodName, params object[] paramValues)
    {
        Delegate del = null;
        string key = obj.GetType().Name + "." + methodName;
        Type tp;

        lock (methodLookup) 
        {
            if (methodLookup.Contains(key)) 
                tp = (Type)methodLookup[key];
            else
            {
                Type[] paramList = new Type[obj.GetType().GetMethod(methodName).GetParameters().Length];
                int n = 0;
                foreach (ParameterInfo pi in obj.GetType().GetMethod(methodName).GetParameters()) paramList[n++] = pi.ParameterType;
                TypeBuilder typeB = builder.DefineType("Del_" +  obj.GetType().Name + "_" + methodName, TypeAttributes.Class | TypeAttributes.AutoLayout | TypeAttributes.Public |  TypeAttributes.Sealed, typeof(MulticastDelegate), PackingSize.Unspecified);
                ConstructorBuilder conB = typeB.DefineConstructor(MethodAttributes.HideBySig | MethodAttributes.SpecialName |            MethodAttributes.RTSpecialName, CallingConventions.Standard, new Type[] { typeof(object), typeof(IntPtr) });
                conB.SetImplementationFlags(MethodImplAttributes.Runtime);
                MethodBuilder mb = typeB.DefineMethod( "Invoke", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig, obj.GetType().GetMethod(methodName).ReturnType, paramList );
                mb.SetImplementationFlags( MethodImplAttributes.Runtime ); 
                tp = typeB.CreateType();
                methodLookup.Add(key, tp);
            }
        }

        del = MulticastDelegate.CreateDelegate(tp, obj, methodName);
        return obj.Invoke(del, paramValues);
    }
}
Usage:
Code:
SafeInvokeHelper.Invoke( Control, "the_method", parms )
Would look like this.
Code:
SafeInvokeHelper.Invoke( m_PatchSwapper, "Update_ProgressBar", percent <= 100 ? percent : 100, true );
SafeInvokeHelper.Invoke( m_PatchSwapper, "Update_Label", m_PatchSwapper._PercentLabel, String.Format( "{0}%", percent <= 100 ? percent : 100 ) );
Havnt tested it with a MessageBox before, not sure if it will even work with one. Its worth a shot though eh?


SafeInvokeHelper - Author/Original Article
__________________

Quote:
Originally Posted by Radwen View Post
Give me a reason... to like you.
Quote:
Originally Posted by excuse me miss, but uh... View Post
11/f/n.korea
Priceless.

Last edited by o0_Sithid_0o; 11-30-2006 at 04:45 PM.
o0_Sithid_0o is offline   Reply With Quote
Old 11-30-2006, 02:54 PM   #4 (permalink)
ConnectUO Creator
 
Jeff's Avatar
 
Join Date: Jan 2004
Age: 27
Posts: 4,825
Default

You should look into anonymous methods

for instance, lets say you want to invoke progress bar update from another thread.

Code:
int progress = 50;
progressBar.Invoke((MethodInvoker)delegate
{
   progressBar.Value = 50;
   progressBar.Invalidate();
});
its that simple.

I personally seen your code as more bloat but to each his own. Looks good tho.

@topic MessageBox.Show()

should simply do something like

Code:
myForm.Invoke( (MethodInvoker)delegate
{
  MessageBox.Show( "message", "caption" );
});
__________________
Jeff Boulanger
ConnectUO - Core Developer

Want to help make ConnectUO better? Click here to submit your ideas/requests
Use your talent to compete against other community members in RunUO hosted coding competitions

If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team.


Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO

Last edited by Jeff; 11-30-2006 at 02:57 PM.
Jeff is offline   Reply With Quote
Old 11-30-2006, 04:41 PM   #5 (permalink)
Forum Expert
 
o0_Sithid_0o's Avatar
 
Join Date: Nov 2004
Location: Hiding his old posts from Daat99.
Age: 22
Posts: 2,321
Send a message via AIM to o0_Sithid_0o Send a message via MSN to o0_Sithid_0o
Default

I didnt write the SafeInvokeHelper class, I just happen to stumble onto it and decided to give it a try. Its been working fine for me.

SafeInvokeHelper
__________________

Quote:
Originally Posted by Radwen View Post
Give me a reason... to like you.
Quote:
Originally Posted by excuse me miss, but uh... View Post
11/f/n.korea
Priceless.
o0_Sithid_0o is offline   Reply With Quote
Old 12-01-2006, 12:46 AM   #6 (permalink)
ConnectUO Creator
 
Jeff's Avatar
 
Join Date: Jan 2004
Age: 27
Posts: 4,825
Default

Quote:
Originally Posted by o0_Sithid_0o
I didnt write the SafeInvokeHelper class, I just happen to stumble onto it and decided to give it a try. Its been working fine for me.

SafeInvokeHelper
I dont doubt it works, but it is 1.0 code tbh 1.0 .net can lick my sack, ArrayList....:shutters:
__________________
Jeff Boulanger
ConnectUO - Core Developer

Want to help make ConnectUO better? Click here to submit your ideas/requests
Use your talent to compete against other community members in RunUO hosted coding competitions

If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team.


Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO
Jeff is offline   Reply With Quote
Old 12-01-2006, 03:48 PM   #7 (permalink)
Forum Expert
 
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
Default

why do you think it is 1.0 code?

@sithid:btw, there is a better solution on the same link if you havent noticed..
__________________
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."
noobie is offline   Reply With Quote
Old 12-01-2006, 07:58 PM   #8 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

System.Collections.HashTable, most likely.
Sep102 is offline   Reply With Quote
Old 12-04-2006, 06:05 PM   #9 (permalink)
ConnectUO Creator
 
Jeff's Avatar
 
Join Date: Jan 2004
Age: 27
Posts: 4,825
Default

Quote:
Originally Posted by noobie
why do you think it is 1.0 code?
ArrayList and Hashtable is why, tbh if u had 2.0 why in gods name would you use those. They do nothing but make room for error and slow your application.
__________________
Jeff Boulanger
ConnectUO - Core Developer

Want to help make ConnectUO better? Click here to submit your ideas/requests
Use your talent to compete against other community members in RunUO hosted coding competitions

If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team.


Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO
Jeff is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5