Re: simple AppKit / multithreading question
Re: simple AppKit / multithreading question
- Subject: Re: simple AppKit / multithreading question
- From: "Dennis C. De Mars" <email@hidden>
- Date: Sat, 12 Mar 2005 11:24:08 -0800
On Mar 11, 2005, at 5:44 AM, Aurélien Hugelé wrote:
Hi list !
my app is heavily multithreaded, and needs to modify the GUI. I know
that the interface mods needs to be done in the main thread.
Its is very convenient to use performSelectorInMainThread: for my
secondary threads... but still, i have a question :
most of my threads need to modify the enabled/disabled state or the
hidden/shown state of my widgets. I use setEnabled:YES/NO or
setHidden:YES/NO
but i can not use performSelectorInMainThread:withObject: here! the
argument of this method must be NSObjects ... not BOOL !
There may be a more straightforward way to do it, but you could define
a category on NSObject with the following methods:
class MyBoolInvocation;
typedef void (MyBoolIMP) (id, SEL, BOOL);
-(void) performSelectorOnMainThread:(SEL)theSelector
withBool:(BOOL)theBool
{
[self
performSelectorOnMainThread:@selector(performSelectorWithBoolArg:)
withObject:[MyBoolInvocation
newWithSelector:theSelector withBool:theBool]];
}
- (void) peformSelectorWithBoolArg:(MyBoolInvocation*)boolArgs
{
((MyBoolIMP)[self methodForSelector:[boolArgs boolSelector]])(self,
[boolArgs boolSelector], [boolArgs boolValue]);
}
The class MyBoolInvocation is just a utility class you would define
that has instance variables for a selector and a bool value. You
initialize it with newWithSelector:withBool: and retrieve the values
with boolSelector and boolValue. It's just a way to pass a struct as a
class; I could have used a dictionary but since neither of the values
that need to be passed are objects, I thought the auxiliary class was
easier for illustrative purposes.
I haven't compiled or tested this, but this gives you the general idea.
At least you would only have to do this once for all API methods that
need a BOOL, but of course you'd have to have separate methods for int
and float.
- Dennis D.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden