|
||
|
|
#1 (permalink) |
|
Forum Expert
|
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.
__________________
|
|
|
|
|
|
#2 (permalink) |
|
Forum Expert
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
|
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." |
|
|
|
|
|
#3 (permalink) |
|
Forum Expert
|
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);
}
}
Code:
SafeInvokeHelper.Invoke( Control, "the_method", parms ) 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 ) );
SafeInvokeHelper - Author/Original Article
__________________
Last edited by o0_Sithid_0o; 11-30-2006 at 04:45 PM. |
|
|
|
|
|
#4 (permalink) |
|
ConnectUO Creator
Join Date: Jan 2004
Age: 27
Posts: 4,825
|
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();
});
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. |
|
|
|
|
|
#5 (permalink) |
|
Forum Expert
|
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 |
|
|
|
|
|
#6 (permalink) | |
|
ConnectUO Creator
Join Date: Jan 2004
Age: 27
Posts: 4,825
|
Quote:
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 |
|
|
|
|
|
|
#7 (permalink) |
|
Forum Expert
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
|
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." |
|
|
|
|
|
#9 (permalink) | |
|
ConnectUO Creator
Join Date: Jan 2004
Age: 27
Posts: 4,825
|
Quote:
__________________
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 |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|