473,388 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

GUI thread

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
Nov 16 '05 #1
11 4233
Hello Cheya,

You should look into the InvokeRequired property.
http://msdn.microsoft.com/library/de...uiredtopic.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/de...ms06112002.asp
HTH,

Kyril

"Nenad Dobrilovic" <ch*******@hotmail.com> wrote in message
news:u5**************@TK2MSFTNGP10.phx.gbl...
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

Nov 16 '05 #2
Kyril Magnos wrote:
Hello Cheya,

You should look into the InvokeRequired property.
http://msdn.microsoft.com/library/de...uiredtopic.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/de...ms06112002.asp
HTH,

Kyril

"Nenad Dobrilovic" <ch*******@hotmail.com> wrote in message
news:u5**************@TK2MSFTNGP10.phx.gbl...
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



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.
Nov 16 '05 #3
Nenad Dobrilovic <ch*******@hotmail.com> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Jon Skeet [C# MVP] wrote:
Nenad Dobrilovic <ch*******@hotmail.com> wrote:
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.)


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?
Nov 16 '05 #5
Nenad Dobrilovic <ch*******@hotmail.com> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Jon Skeet [C# MVP] wrote:
Nenad Dobrilovic <ch*******@hotmail.com> wrote:
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.

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?
Nov 16 '05 #7
Nenad Dobrilovic <ch*******@hotmail.com> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
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

Nov 16 '05 #9
Nenad Dobrilovic <ch*******@hotmail.com> wrote:
I'm sorry to bother you, but I have more questions :)
Sure, no problem.
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?


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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10
Jon Skeet [C# MVP] wrote:
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.
Nov 16 '05 #11
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

On Fri, 16 Jul 2004 10:22:44 +0200, Nenad Dobrilovic
<ch*******@hotmail.com> wrote:
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


Nov 16 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: adeger | last post by:
Having trouble with my first forays into threads. Basically, the threads don't seem to be working in parallel (or you might say are blocking). I've boiled my problems to the following short code...
4
by: Gilles Leblanc | last post by:
Hi I have started a small project with PyOpenGL. I am wondering what are the options for a GUI. So far I checked PyUI but it has some problems with 3d rendering outside the Windows platform. I...
7
by: Ivan | last post by:
Hi I have following problem: I'm creating two threads who are performing some tasks. When one thread finished I would like to restart her again (e.g. new job). Following example demonstrates...
4
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the...
5
by: Razzie | last post by:
Hi all, A question from someone on a website got me thinking about this, and I wondered if anyone could explain this. A System.Threading.Timer object is garbage collected if it has no...
16
by: droopytoon | last post by:
Hi, I start a new thread (previous one was "thread timing") because I have isolated my problem. It has nothing to do with calling unmanaged C++ code (I removed it in a test application). I...
9
by: mareal | last post by:
I have noticed how the thread I created just stops running. I have added several exceptions to the thread System.Threading.SynchronizationLockException System.Threading.ThreadAbortException...
13
by: Bob Day | last post by:
Using vs2003, vb.net I start a thread, giving it a name before start. Code snippet: 'give each thread a unique name (for later identification) Trunk_Thread.Name = "Trunk_0_Thread" ' allow...
7
by: Charles Law | last post by:
My first thought was to call WorkerThread.Suspend but the help cautions against this (for good reason) because the caller has no control over where the thread actually stops, and it might have...
3
by: John Nagle | last post by:
There's no way to set thread priorities within Python, is there? We have some threads that go compute-bound, and would like to reduce their priority slightly so the other operations, like...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.