While(true)Application.DoEvents V Application.Run

F

foldface

Hi
I am doing drag and drop between controls (listview and
treeview) using mouse_event.
What I am after is a book/resource to explain the behaviour
below.

If I do the various Win32 api calls and call Application.Run, it
works. I can see dragging and dropping on the screen.
If I do this:

while(true)
{
Application.DoEvents()
}

it works, while if I do this:

while(true)
{
FormFinder finder = new FormFinder();
FormCollection theForms = finder.FindAll("Employee Transfers");
if(theForms.Count > 0) break;
Application.DoEvents()
}

it doesn't work. The code is NUintForms b.t.w.
Its almost as if theres a timing issue involved, you have to be looking
at the queue at the right minute?

Any advice?

Thanks
F
 
J

Jon Skeet [C# MVP]

I am doing drag and drop between controls (listview and
treeview) using mouse_event.
What I am after is a book/resource to explain the behaviour
below.

If I do the various Win32 api calls and call Application.Run, it
works. I can see dragging and dropping on the screen.
If I do this:

while(true)
{
Application.DoEvents()
}

it works, while if I do this:

while(true)
{
FormFinder finder = new FormFinder();
FormCollection theForms = finder.FindAll("Employee Transfers");
if(theForms.Count > 0) break;
Application.DoEvents()
}

it doesn't work. The code is NUintForms b.t.w.
Its almost as if theres a timing issue involved, you have to be looking
at the queue at the right minute?

Any advice?

Application.Run is a much nicer way of handling things, I think.
Certainly a tight loop on Application.DoEvents is a bad idea.

Shouldn't your test be

if (theForms.Count == 0) break;

though - so you stop processing events when all forms have been closed,
rather than when none of them have been closed?
 
F

foldface

I'm waiting for a form to appear, hence the code.
I'm trying to use DoEvent to keep to the structure of NUnitForms. Is
there a way of exiting the Application.Run once its running?

I would like to know how the internals work?
 
J

Jon Skeet [C# MVP]

I'm waiting for a form to appear, hence the code.
I'm trying to use DoEvent to keep to the structure of NUnitForms. Is
there a way of exiting the Application.Run once its running?

It stops when the form which is passed to it is closed.
I would like to know how the internals work?

I can't help you there, although others no doubt can.

It feels like a bad idea to manually call the message pump like this.
 
S

Sean Hederman

As far as I can see, Application.Run kicks off a non-modal message loop, and
as Jon says will carry on running until the form is closed.
Application.DoEvents also kicks off a message loop (actually the same one as
Run), however since no form is set for this message loop it won't exit the
loop when the form is closed, but rather once the message pump has been
emptied.

When dealing with forms, it is much better to rely on the Framework to
handle the messaging for you (via Form.Show, Form.ShowDialog &
Application.Run) unless you have a really deep understanding of the windows
message pump.
 
F

foldface

When dealing with forms, it is much better to rely on the Framework
to
handle the messaging for you (via Form.Show, Form.ShowDialog &
Application.Run) unless you have a really deep understanding of the windows
message pump.

This is purely for testing using NUnit. NUnitForms seems to rely on
Application.DoEvents and I was trying to work with that. Instead
I'm now doing something like

[Test]
public void Test1()
{
// make my form, etc
Thread thread = new Thread(new ThreadStart(OtherMethod));
thread.Start();
Application.Run()
}

public void OtherMethod()
{
// wait for a bit, do some stuff with the forms
Application.Exit()
}

repeat for other tests.

but this is the best I've come up with rather than 'the' way to do it.
It would
be nice to have a real understanding of how DoEvents and Run works so
that
I have the option of doing it anyway I want. Also I just like knowing
how things work.

Ta
F
 
S

Sean Hederman

I'd spelunk them with Reflector to find out the nitty-gritty's, but my
earlier post gave the gist of it. The message loop is fairly simple:
1. Call PeekMessage to get messages for current thread. If there aren't any
and it's not a form loop then exit out of the loop
2. Call GetMessage on the current thread, and create a Message object from
it.
3. If it's a control, call PreProcessMessage on it, jumping to 6 if the
message was handled.
4. Translate virtual key codes messages to character messages
5. Dispatch the message to it's target
6. If this is for a form, and it's not closing, back to 1

(I've simplified it a bit, but you get the idea)

As I said before, DoEvents executes this procedure without a specific form,
Run(Form) the indicated form, and ShowDialog(Form) runs it with various
modal options set.

When dealing with forms, it is much better to rely on the Framework to
handle the messaging for you (via Form.Show, Form.ShowDialog &
Application.Run) unless you have a really deep understanding of the windows
message pump.

This is purely for testing using NUnit. NUnitForms seems to rely on
Application.DoEvents and I was trying to work with that. Instead
I'm now doing something like

[Test]
public void Test1()
{
// make my form, etc
Thread thread = new Thread(new ThreadStart(OtherMethod));
thread.Start();
Application.Run()
}

public void OtherMethod()
{
// wait for a bit, do some stuff with the forms
Application.Exit()
}

repeat for other tests.

but this is the best I've come up with rather than 'the' way to do it.
It would
be nice to have a real understanding of how DoEvents and Run works so
that
I have the option of doing it anyway I want. Also I just like knowing
how things work.

Ta
F
 
Joined
Jul 18, 2008
Messages
1
Reaction score
0
Hello,
what should i do, when i must use Application.Run!
I need the GUI, that i started, to test it with NUnitforms...

public class ProgramTEST
{
public ProgramTEST()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyApplicationContext(new string[] {})); // -->This bolcks the course of the application.
}

public ControlTester buttonTester;

public void Test()
{
buttonTester = new ControlTester("_okButton", "myForm");

buttonTester.FireEvent("Click");
}
}

On this following line, my program stops:.

Application.Run(new MyApplicationContext(new string[] {}));

or, when you write:

ProgramTEST MyTest = new ProgramTEST(); // here my program stops, tell i close the GUI!!!But i need the GUI!
MyTest.Test();

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top