mutlithreading between different classes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

A Thread is created on form1 that addresses a sub in another class. When
attempting to invoke and sub on the main form I recieve an error about a
Window Handler needing to be created first.

Is there a special procedure that is needed to be followed while invoking a
sub from and different class in which the thread was created? How is this
accomplished?

Code from classX(not the class with the form):

Invoke(New Form1.BUNYDelta(AddressOf Form1.DeltaProperty), _
Form1.AssetProgressBar1, "Maximum",
"Form1._InventoryDB_BUNY_DataSet.tbl_Asset.Count", "State")
 
PCI said:
A Thread is created on form1 that addresses a sub in another class.
When attempting to invoke and sub on the main form I recieve an
error about a Window Handler needing to be created first.

Is there a special procedure that is needed to be followed while
invoking a sub from and different class in which the thread was
created? How is this accomplished?

Code from classX(not the class with the form):

Invoke(New Form1.BUNYDelta(AddressOf Form1.DeltaProperty), _
Form1.AssetProgressBar1, "Maximum",
"Form1._InventoryDB_BUNY_DataSet.tbl_Asset.Count", "State")


After reading it several times, I'm still not sure where the code is
located, where you call invoke and what classes are in the Form and so on.
Could you please describe it once more?


Armin
 
There is form1. (Public Class form1)
there is classAdded.

form1 is the gui. classAdded is an additional seperate class.

on form1 i and starting a thread that calls a sub on the "classAdded". The
sub on ClassAdded is required to update an object (progressbar1) on form1.

when i attempt to invoke this in the classAdded sub, i recieve an error
stating that a window handle must be created.
 
Rick B said:
There is form1. (Public Class form1)
there is classAdded.

form1 is the gui. classAdded is an additional seperate class.

on form1 i and starting a thread that calls a sub on the
"classAdded". The sub on ClassAdded is required to update an object
(progressbar1) on form1.

when i attempt to invoke this in the classAdded sub, i recieve an
error stating that a window handle must be created.


It was not clear to me because of this line:


If you call Invoke this way, it must be a member of classAdded. You have to
call the Form's Invoke method. I also don't see how the class gets the
reference to the Form. I also don't know what you intend with "New
Form1.BUNYDelta". What is this? A delegate?

Writing this, I guess Invoke is a method in the class that creates an object
array from the parameters passed and calls the Form's Invoke method, right?

Ok, back to the point. Is your problem that the Form has already been shown
and you despite still get an error, or do you want to know how to call
Invoke before the Form has been shown? To solve the latter, call the Form's
Createhandle method before Invoking the method. IMO this should be done in
the Form before calling classAdded because it's up to the Form to make sure
that it has been created in order to be invoked.


Armin
 
Rick B said:
when i call the forms creathandle it says that the handle is
created.


You did not directly answer my questions, so it's hard to help you. Do you
really call the Invoke method of the Form that's handle has been created?
Maybe you create a new instance in classAdded?


Armin
 
sorry, i am a little confused, i am not familar with handles.

i am attempting to inkvoke a sub on the main form from the class, using the
delegate on the form.
 
Rick B said:
sorry, i am a little confused, i am not familar with handles.

A handle is a number that Windows uses to identify a window. Whenever a
window is created, it gets a unique number.

Before .Net, you had to call API functions to do things with a window, like
showing the window. You had to pass the handle to let the function know
which window is to be addressed.

A Form is a class that encapsulates this behavior. Instead of calling an API
function, you call the Show method. Internally, the Show method calls the
API function and passes the handle that has been stored internally
when the window has been created. You can retrieve the handle from the
Form's Handle property.

If you create an instance of a Form, the window is /not/ created
immediatelly. It will be created automatically but not before you do things
that need the window to be created, like when executing Show.

Using Invoke is like sending a message to the window. This requires that the
window must have been created before. As I wrote above, having a Form
instance does not necessarily mean that a window has been created, too. If
you want to use Invoke, you must ensure that the sender will come upon a
Form that's windows has been created. The error you get says that this did
not happen, thus I suggested to call Createhandle which forces the creation
of the window.

i am attempting to inkvoke a sub on the main form from the class,
using the delegate on the form.


Again, you didn't answer my question: Do you create a new instance of the
form in classAdded and call it's Invoke method, or do you call the Invoke
method of the Form that you have shown before? If you create a new instance,
it explains why you get the error because the handle of the Form has not
been created yet.


Armin
 
no, there is only one instance of the form.

Armin Zingler said:
A handle is a number that Windows uses to identify a window. Whenever a
window is created, it gets a unique number.

Before .Net, you had to call API functions to do things with a window, like
showing the window. You had to pass the handle to let the function know
which window is to be addressed.

A Form is a class that encapsulates this behavior. Instead of calling an API
function, you call the Show method. Internally, the Show method calls the
API function and passes the handle that has been stored internally
when the window has been created. You can retrieve the handle from the
Form's Handle property.

If you create an instance of a Form, the window is /not/ created
immediatelly. It will be created automatically but not before you do things
that need the window to be created, like when executing Show.

Using Invoke is like sending a message to the window. This requires that the
window must have been created before. As I wrote above, having a Form
instance does not necessarily mean that a window has been created, too. If
you want to use Invoke, you must ensure that the sender will come upon a
Form that's windows has been created. The error you get says that this did
not happen, thus I suggested to call Createhandle which forces the creation
of the window.




Again, you didn't answer my question: Do you create a new instance of the
form in classAdded and call it's Invoke method, or do you call the Invoke
method of the Form that you have shown before? If you create a new instance,
it explains why you get the error because the handle of the Form has not
been created yet.


Armin
 

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