Click here to Skip to main content
15,920,633 members
Home / Discussions / C#
   

C#

 
AnswerRe: can i "%.1f" in C#? Pin
James T. Johnson2-Apr-02 2:44
James T. Johnson2-Apr-02 2:44 
AnswerRe: can i "%.1f" in C#? Pin
Tom Archer2-Apr-02 7:42
Tom Archer2-Apr-02 7:42 
AnswerRe: can i "%.1f" in C#? Pin
moliate2-Apr-02 12:54
moliate2-Apr-02 12:54 
GeneralOverriding the Closing event Pin
1-Apr-02 20:44
suss1-Apr-02 20:44 
GeneralRe: Overriding the Closing event Pin
David Wengier1-Apr-02 20:55
David Wengier1-Apr-02 20:55 
GeneralRe: Overriding the Closing event Pin
1-Apr-02 20:59
suss1-Apr-02 20:59 
GeneralRe: Overriding the Closing event Pin
1-Apr-02 22:57
suss1-Apr-02 22:57 
GeneralRe: Overriding the Closing event Pin
James T. Johnson2-Apr-02 1:19
James T. Johnson2-Apr-02 1:19 
I am truly at a loss... It appears that every single event that is raised to ask if the program/form should close just asks not providing any information as to why its asking.

I've even fiddled with using reflection to get at some internal members, but all I get is "member not found" errors Cry | :((

Here's what I've found if you want to investigate it further

Inside of the Application class is a nested type called ThreadContext.

You can get the Type for this with this bit of code.

public Type GetThreadContextType() {
  MemberInfo[] typesInApp = typeof(Application).FindMembers(
    MemberTypes.NestedType, 
    BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public, 
    new MemberFilter(memFilter), null
  );

  return MemberInfo[0].DeclaringType;
}
 
private bool memFilter(MemberInfo m, object filterCriteria)
{
	if( m.Name.IndexOf("ThreadContext") != -1 )
		return true;
	else
		return false;
}
Now that class has a static method called "FromCurrent" which takes no arguments and returns the ThreadContext for the current application. This is retrieved by looking up a LocalDataStore on the Thread, the LocalDataStoreSlot is stored in a static field named tlsSlot, this slot corresponds to the thread's ThreadContext object.

Once you have an instance of ThreadContext you can call the GetState method passing in the value 0x10 to see if the Thread has an AppQuit message posted or pass in 0x08 to see if the Thread has a Quit message posted.

Here is the code I've been pounding out since you posted your message, if anyone can get it to work I'll buy you a beer the next time you are in the area Smile | :)

Type app = typeof(Application);
MemberInfo[] typesInApp = app.FindMembers(
	MemberTypes.NestedType, 
	BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public,
	new MemberFilter(memFilter), null
);
 
Type typeThreadContext = typesInApp[0].DeclaringType;
object threadContext = Thread.GetData(
	(LocalDataStoreSlot) typeThreadContext.InvokeMember(
		"tlsSlot", 
		BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Static, 
		null, null, new object [] { }
	)
);
 
bool StatePostedAppQuit = (bool) typeThreadContext.InvokeMember(
	"threadState", 
	BindingFlags.GetField | BindingFlags.NonPublic, 
	null, threadContext, new object[] { 0x10 }
);
 
bool StatePostedQuit = (bool) typeThreadContext.InvokeMember(
	"threadState", 
	BindingFlags.GetField  | BindingFlags.NonPublic, 
	null, threadContext, new object[] { 0x08 }
);
You'll need the memFilter method from the first chunk of code above.

Feel free to completely destroy that bit of code, I'm getting an exception on the line that retrieves the threadContext. I've tried by using the FromCurrent method and the method above (which basically does what FromCurrent() does).

Now I get to go take my car in to get a flat tire fixed Mad | :mad:

James

Sonork: Hasaki
"I left there in the morning
with their God tucked underneath my arm
their half-assed smiles and the book of rules.
So I asked this God a question
and by way of firm reply,
He said - I'm not the kind you have to wind up on Sundays."
"Wind Up" from Aqualung, Jethro Tull 1971

GeneralRe: Overriding the Closing event Pin
2-Apr-02 2:15
suss2-Apr-02 2:15 
GeneralRe: Overriding the Closing event Pin
James T. Johnson2-Apr-02 3:34
James T. Johnson2-Apr-02 3:34 
GeneralRe: Overriding the Closing event Pin
Ludwig Stuyck2-Apr-02 20:24
Ludwig Stuyck2-Apr-02 20:24 
Generalstatic members vs namespace helper functions Pin
myfriendofmisery1-Apr-02 14:15
myfriendofmisery1-Apr-02 14:15 
GeneralRe: static members vs namespace helper functions Pin
Tom Archer2-Apr-02 7:50
Tom Archer2-Apr-02 7:50 
GeneralTrouble with CollectionBase Pin
1-Apr-02 12:26
suss1-Apr-02 12:26 
GeneralRe: Trouble with CollectionBase Pin
James T. Johnson1-Apr-02 22:58
James T. Johnson1-Apr-02 22:58 
GeneralRe: Trouble with CollectionBase Pin
Tom Archer2-Apr-02 7:25
Tom Archer2-Apr-02 7:25 
GeneralSendMessage/PostMessage via socket Pin
BLaZiNiX31-Mar-02 22:33
BLaZiNiX31-Mar-02 22:33 
Questionhow to immigrate c++'s union? Pin
31-Mar-02 19:32
suss31-Mar-02 19:32 
AnswerRe: how to immigrate c++'s union? Pin
Paul M Watt31-Mar-02 20:35
mentorPaul M Watt31-Mar-02 20:35 
AnswerRe: how to immigrate c++'s union? Pin
James T. Johnson1-Apr-02 22:55
James T. Johnson1-Apr-02 22:55 
GeneralRe: how to immigrate c++'s union? Pin
2-Apr-02 14:59
suss2-Apr-02 14:59 
QuestionCan a component obtain a reference to its container object? Pin
31-Mar-02 14:02
suss31-Mar-02 14:02 
AnswerRe: Can a component obtain a reference to its container object? Pin
James T. Johnson31-Mar-02 17:58
James T. Johnson31-Mar-02 17:58 
GeneralRe: Can a component obtain a reference to its container object? Pin
1-Apr-02 6:38
suss1-Apr-02 6:38 
GeneralRe: Can a component obtain a reference to its container object? Pin
Andy Smith1-Apr-02 6:50
Andy Smith1-Apr-02 6:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.