GUI thread

  • Thread starter Thread starter Nenad Dobrilovic
  • Start date Start date
N

Nenad Dobrilovic

Hi,
I have big problem. I made generic form which can be rendered, and as a
result of that action, I get System.Windows.Forms.Form object.
Rendering must be done in GUI thread (one which has message pump), but I
can't garantee that the using of Render function will be made in GUI thread.
This is Render function:

public object Render(Synchronizer.T sync)
{
return sync.Render(this); <-- this function is making real Form
}

I want that Render function is doing in GUI thread allways. Can anyone
help me?
Thanx, Cheya
 
Hello Cheya,

You should look into the InvokeRequired property.
http://msdn.microsoft.com/library/d...ndowsformscontrolclassinvokerequiredtopic.asp

A basic example:
<pseudo-code>
public delegate void TestMeDelegate();
public void TestMe()
{
if(!this.InvokeRequired)
{
//update ui elements as needed...
}
else
{
TestMeDelegate delegate = new TestMeDelegate();
this.BeginInvoke(delegate);
}
}
</pseudo-code>

Also, here is a great article on Multi-threading WinForms apps.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms06112002.asp


HTH,

Kyril
 
Kyril said:
Hello Cheya,

You should look into the InvokeRequired property.
http://msdn.microsoft.com/library/d...ndowsformscontrolclassinvokerequiredtopic.asp

A basic example:
<pseudo-code>
public delegate void TestMeDelegate();
public void TestMe()
{
if(!this.InvokeRequired)
{
//update ui elements as needed...
}
else
{
TestMeDelegate delegate = new TestMeDelegate();
this.BeginInvoke(delegate);
}
}
</pseudo-code>

Also, here is a great article on Multi-threading WinForms apps.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms06112002.asp


HTH,

Kyril

The problem is that InvokeRequired property, Invoke() method, etc. is
defined in System.Windows.Forms.Control class.
But, my class is generic form (it is not inherited from
System.Windows.Forms.Control), which can be 'rendered' using Render()
method and Synchronize object.
I want to tell GUI thread 'do this to me' from my object.
 
Nenad Dobrilovic said:
The problem is that InvokeRequired property, Invoke() method, etc. is
defined in System.Windows.Forms.Control class.
But, my class is generic form (it is not inherited from
System.Windows.Forms.Control), which can be 'rendered' using Render()
method and Synchronize object.
I want to tell GUI thread 'do this to me' from my object.

Then you'll need to know *a* control (any control) which is running on
that thread. Bear in mind that there can in fact be several GUI threads
- however many you call Application.Run on, basically. (Or however many
you create controls in, depending on your view of things.)
 
Jon said:
Then you'll need to know *a* control (any control) which is running on
that thread. Bear in mind that there can in fact be several GUI threads
- however many you call Application.Run on, basically. (Or however many
you create controls in, depending on your view of things.)

That means that GUI thread can be any thread - if I create control in
that thread? Or, I must call Application.Run on that thread to become
GUI thread?
 
Nenad Dobrilovic said:
That means that GUI thread can be any thread - if I create control in
that thread? Or, I must call Application.Run on that thread to become
GUI thread?

You should use Application.Run to run a message pump in a given thread.
However, you should only access a control from the thread which created
it.
 
Jon said:
You should use Application.Run to run a message pump in a given thread.
However, you should only access a control from the thread which created
it.
OK, I understend that one thread must have message pump - lets call it
'GUI thread'.
Does controle can be created and accesed in a thread other than GUI
thread? How it receives messages from message pump in that case? Does
entire form must be created in that thread also?
 
Nenad Dobrilovic said:
OK, I understend that one thread must have message pump - lets call it
'GUI thread'.

No, *at least* one thread must have a message pump if you want to use a
GUI.
Does controle can be created and accesed in a thread other than GUI
thread? How it receives messages from message pump in that case? Does
entire form must be created in that thread also?

I don't know whether you can create a form in one thread and create its
child controls in another, then run a message pump on both threads -
but it sounds like a hideously bad idea to me, and one which will
*probably* break.

You can, however, run two different forms in two different threads.
 
Jon Skeet [C# MVP] wrote:

I'm sorry to bother you, but I have more questions :)
You can, however, run two different forms in two different threads.
And each one *must* have it's own message pump? Or one message pump is
enought?
Is there any way to start message pump other than Application.Run()?

Thank you
 
Nenad Dobrilovic said:
I'm sorry to bother you, but I have more questions :)

Sure, no problem.
And each one *must* have it's own message pump? Or one message pump is
enought?

I believe there's a message pump per thread. I'm not an expert on the
Win32 side of things, to be honest, but I can't see how you could
easily share a message pump between different threads if each message
has to go to a specific thread.
Is there any way to start message pump other than Application.Run()?

I believe showing a modal dialog will run a message pump for the
duration of the dialog's visibility, but other than that, I don't know
of any way of using the built-in one. You could use P/Invoke to call
the Win32 functions yourself, of course - but why do you want to avoid
calling Application.Run?
 
Jon said:
I believe showing a modal dialog will run a message pump for the
duration of the dialog's visibility, but other than that, I don't know
of any way of using the built-in one. You could use P/Invoke to call
the Win32 functions yourself, of course - but why do you want to avoid
calling Application.Run?

Thank you. I don't want to avoid Application.Run(). I was just curious
about it.
 
Hi Nenad:

Yes, Windows will create a message queue for each thread that creates
a window.

I suppose technically you could write your own message pump by
Pinvoking the Win32 API (GetMessage), but that would be ugly.

--
Scott
http://www.OdeToCode.com

Jon Skeet [C# MVP] wrote:

I'm sorry to bother you, but I have more questions :)
You can, however, run two different forms in two different threads.
And each one *must* have it's own message pump? Or one message pump is
enought?
Is there any way to start message pump other than Application.Run()?

Thank you
 

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

Back
Top