system thread creating a form and getting cross thread error

G

Gina_Marano

Good day,

I thought I had a nifty idea but am hitting walls and looking for best
way to implement.

I am creating a telnet server that mimics a visual form and main menu.
Works very well so far...

for example if there are 9 menu items I display them when the telnet
session connects.

Next step, when they press 1 I want to execute the onclick event for
menu item 1. Have that coded, not as nicely as I wanted. currently
using PerformClick. I generically wanted to call it and pass args but
can't figure that part out as I don't know (since this is generic) the
actual event name. (any hints here would be nice but currently just
setting public variables).

ok, so each session is its own thread. The thread creates its own copy
of the menu form. My attempt to be thread safe. So if everyone is
using menuitem 1 I don't have critical sections etc.

Problem is that when i call performclick I get "Control '' accessed
from a thread other than the thread it was created on". I think this
is probably only in the IDE if my memory is correct.

Thoughts on how to better implement this?

(Why am I doing it this way? Because currently we have about 25-30
menu/sub items and 5000+ lines of code to manage the state etc... If
we can visually create the menu/sub menus and their events it is very
easy to add/disable/hide menu items and look at the function each one
provides)

~Gina_M~
 
M

miher

Hi,

As the error message states a control can be modified only from the thread
that owns it. To modify a control from a separate thread You must use the
Invoke method of the control. This method requires You to supply a delegate
which will be executed in that thread which owns the control.
Read more about the invoke method here :
http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx

Hope You find this useful.
-Zsolt
 
G

Gina_Marano

Hi,

As the error message states a control can be modified only from the thread
that owns it. To modify a control from a separate thread You must use the
Invoke method of the control. This method requires You to supply a delegate
which will be executed in that thread which owns the control.
Read more about the invoke method here :http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx

Hope You find this useful.
-Zsolt

I did this:

public void ExecuteClick(ToolStripMenuItem aControl)
{
if (aControl.Owner.InvokeRequired)
{
ExecuteClickCallback d = ExecuteClick;
Invoke(d, new object[] { aControl });
}
else
{
aControl.PerformClick();
}
}

and received the following: "Invoke or BeginInvoke cannot be called on
a control until the window handle has been created."

note: I have created the form but it is not visible/shown.

lFrmMenu = new FormMenu();
lFrmMenu.ExecuteClick(MenuItem1);

Thanks for your very prompt answer. I really appreciate it!

~Gina_M~
 
M

miher

Hi,

I was not able to reproduce Your problem, so i can only give a tip. You can
force handle creation by getting the value of the Handle property. However
be careful, cause as i know this will create the handle on the current
thread(no matter which one is that) and can lead to messing up things.

-Zsolt

Gina_Marano said:
Hi,

As the error message states a control can be modified only from the
thread
that owns it. To modify a control from a separate thread You must use the
Invoke method of the control. This method requires You to supply a
delegate
which will be executed in that thread which owns the control.
Read more about the invoke method here
:http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx

Hope You find this useful.
-Zsolt

I did this:

public void ExecuteClick(ToolStripMenuItem aControl)
{
if (aControl.Owner.InvokeRequired)
{
ExecuteClickCallback d = ExecuteClick;
Invoke(d, new object[] { aControl });
}
else
{
aControl.PerformClick();
}
}

and received the following: "Invoke or BeginInvoke cannot be called on
a control until the window handle has been created."

note: I have created the form but it is not visible/shown.

lFrmMenu = new FormMenu();
lFrmMenu.ExecuteClick(MenuItem1);

Thanks for your very prompt answer. I really appreciate it!

~Gina_M~
 
G

Gina_Marano

Hi,

I was not able to reproduce Your problem, so i can only give a tip.  You can
force handle creation by getting the value of the Handle property. However
be careful, cause as i know this will create the handle on the current
thread(no matter which one is that) and can lead to messing up things.

-Zsolt

"Gina_Marano" <[email protected]> az alábbiakat írta a következo üzenetben

I did this:
       public void ExecuteClick(ToolStripMenuItem aControl)
       {
           if (aControl.Owner.InvokeRequired)
           {
               ExecuteClickCallback d = ExecuteClick;
               Invoke(d, new object[] { aControl });
           }
           else
           {
               aControl.PerformClick();
           }
       }
and received the following: "Invoke or BeginInvoke cannot be called on
a control until the window handle has been created."
note: I have created the form but it is not visible/shown.
           lFrmMenu = new FormMenu();
           lFrmMenu.ExecuteClick(MenuItem1);
Thanks for your very prompt answer. I really appreciate it!
~Gina_M~- Hide quoted text -

- Show quoted text -

Thanks much Zsolt! I will try it with care.

~Gina_M~
 
G

Gina_Marano

The instance has been created, but the window handle has not.  And won't  
be until you show the form (well, you can force it to be created, but you 
shouldn't have to).

It doesn't really make sense to start clicking on a form or other control 
until it's been shown, so at this point primarily the issue is that you  
haven't shown the form.

Without a concise-but-complete code sample that demonstrates the problem  
you're having, it's impossible to suggest what the best fix to the problem  
might be.  But the basic idea to correct the exception you're seeing isto  
make sure you've shown the control before you attempt to use it.

Pete

Thanks Pete,

lFrmMenu = new FormMenu();
IntPtr aHandle = lFrmMenu.Handle;
currMenuItem = lFrmMenu.MenuItem1;

works.

Basically I am using the IDE to visually design my menu/sub menu
system that will be displayed over telnet. I could create objects and
such but what I really want is the visual designer. I could also build
my own designer, have done so in the past (rudimentory) but this just
works so well.... so far.

~Gina_M~
 

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