473,805 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Threading...

I think I'm doing this wrong :

I have a class with a public shared method such as follows:

public shared sub myFunction
dim frm as new myFrm
dim t as new Threading.Threa d(Addressof myFrm.myShowDia log
t.start
end sub

In the frm - the myShowDialog just does a "me.showdialog" .

The idea is that this function will display a form, but then return control
back to the calling routine immediately without waiting for the form to be
closed.

It works, but I've had several reports of the form that is displayed
"crashing" as it is being displayed, with random errors (I've not got a
sample) or the whole application just disappearing (without error).

Can anybody assist, either by confirming or otherwise that what I'm doing is
reasonable, or perhaps letting me know a better way.

Regards
Simon

ps Previously posted by mistake to vb.controls group.

--
=============== =============== ==
Simon Verona
Dealer Management Service Ltd
Stewart House
Centurion Business Park
Julian Way
Sheffield
S9 1GD

Tel: 0870 080 2300
Fax: 0870 735 0011

--
=============== =============== ==
Simon Verona
Dealer Management Service Ltd
Stewart House
Centurion Business Park
Julian Way
Sheffield
S9 1GD

Tel: 0870 080 2300
Fax: 0870 735 0011
Jul 17 '06 #1
14 1643
well im kinda curious on why it looks like your attempting to show another
form on a different thread....are you meaning to put the form on another
thread, and if so why?
--
-iwdu15
Jul 17 '06 #2

Simon Verona wrote:
I think I'm doing this wrong :

I have a class with a public shared method such as follows:

public shared sub myFunction
dim frm as new myFrm
dim t as new Threading.Threa d(Addressof myFrm.myShowDia log
t.start
end sub

In the frm - the myShowDialog just does a "me.showdialog" .

The idea is that this function will display a form, but then return control
back to the calling routine immediately without waiting for the form to be
closed.
If this is what you want, why not just .Show (rather than .ShowDialog)
the form? Anyway...
>
It works, but I've had several reports of the form that is displayed
"crashing" as it is being displayed, with random errors (I've not got a
sample) or the whole application just disappearing (without error).

Can anybody assist, either by confirming or otherwise that what I'm doing is
reasonable, or perhaps letting me know a better way.
I think you are falling foul of the rule whereby UI elements cannot be
safely interacted with from threads other than their 'owning' thread -
that on which they are created. So here the myFrm is created on the
thread that enters this procedure, but then passed to a different
thread to show itself. Thus this second thread will be accessing UI
elements it didn't create -bad.

The fix (if there are deeper reasons why you need to do this sort of
thing) is to move the form creation from before thread-creation to
after it. That is, at the moment you are saying:

Create form
Create thread (giving it the form)
Start thread - thread goes on to show form

Instead you should say

Create thread
Start thread - thread goes on to create form, then show it.

Probably the logical place for this new routine would be a Shared
procedure in myFrm:

Public Shared Sub NewShowDialog
Dim f As New myFrm
f.ShowDialog
End Sub

then replace your current myFunction with

Dim t As New Thread(AddressO f myFrm.NewShowDi alog)
t.Start

But please first consider my first question about .Show vs
..ShowDialog...

--
Larry Lard
Replies to group please
When starting a new topic, please mention which version of VB/C# you
are using

Jul 17 '06 #3
Good question...

Maybe if I explain the application....

I have a main form, which displays a list of entries. The application
allows you to click on an entry to open a form to view/update the details of
that entry.

However, I wanted to allow multiple entries to be displayed together.

Also, I wanted to encapsulate the display of the form within a class (it is
used in many parts of the application as well, so I felt it would be
easier).

If I display the form without creating the thread, the form dies as soon as
the subroutine exits back to the main form (IIRC!), so I came up with this
as a workaround. It also seemed to have the affect of making the
application seem to run smoother (though that could just be me). If I don't
create the thread and just use showdialog, then the original form is blocked
until it returns.

I don't know why it should be causing a problem, but if there is a better
way of acheiving the same thing without creating the thread then I'm up for
it!

Regards
Simon

--
=============== =============== ==
Simon Verona
Dealer Management Service Ltd
Stewart House
Centurion Business Park
Julian Way
Sheffield
S9 1GD

Tel: 0870 080 2300
Fax: 0870 735 0011

"iwdu15" <jmmgoalsteraty ahoodotcomwrote in message
news:C1******** *************** ***********@mic rosoft.com...
well im kinda curious on why it looks like your attempting to show another
form on a different thread....are you meaning to put the form on another
thread, and if so why?
--
-iwdu15

Jul 17 '06 #4
See my previous reply, I've had problem with the form disappearing when the
subroutine exits... (I'm presuming the object is killed on exit?)

I understand what you are saying about the frm creation and use being on
seperate threads. I must admit to not thinking of having a shared entry on
the form to display itself!

Regards
Simon

--
=============== =============== ==
Simon Verona
Dealer Management Service Ltd
Stewart House
Centurion Business Park
Julian Way
Sheffield
S9 1GD

Tel: 0870 080 2300
Fax: 0870 735 0011

"Larry Lard" <la*******@hotm ail.comwrote in message
news:11******** *************@m 73g2000cwd.goog legroups.com...
>
Simon Verona wrote:
>I think I'm doing this wrong :

I have a class with a public shared method such as follows:

public shared sub myFunction
dim frm as new myFrm
dim t as new Threading.Threa d(Addressof myFrm.myShowDia log
t.start
end sub

In the frm - the myShowDialog just does a "me.showdialog" .

The idea is that this function will display a form, but then return
control
back to the calling routine immediately without waiting for the form to
be
closed.

If this is what you want, why not just .Show (rather than .ShowDialog)
the form? Anyway...
>>
It works, but I've had several reports of the form that is displayed
"crashing" as it is being displayed, with random errors (I've not got a
sample) or the whole application just disappearing (without error).

Can anybody assist, either by confirming or otherwise that what I'm doing
is
reasonable, or perhaps letting me know a better way.

I think you are falling foul of the rule whereby UI elements cannot be
safely interacted with from threads other than their 'owning' thread -
that on which they are created. So here the myFrm is created on the
thread that enters this procedure, but then passed to a different
thread to show itself. Thus this second thread will be accessing UI
elements it didn't create -bad.

The fix (if there are deeper reasons why you need to do this sort of
thing) is to move the form creation from before thread-creation to
after it. That is, at the moment you are saying:

Create form
Create thread (giving it the form)
Start thread - thread goes on to show form

Instead you should say

Create thread
Start thread - thread goes on to create form, then show it.

Probably the logical place for this new routine would be a Shared
procedure in myFrm:

Public Shared Sub NewShowDialog
Dim f As New myFrm
f.ShowDialog
End Sub

then replace your current myFunction with

Dim t As New Thread(AddressO f myFrm.NewShowDi alog)
t.Start

But please first consider my first question about .Show vs
.ShowDialog...

--
Larry Lard
Replies to group please
When starting a new topic, please mention which version of VB/C# you
are using

Jul 17 '06 #5
if you want to show a new form on a different thread, then the new thread
must then create the form object:

Private Delegate Sub delNewThread

Private Sub btnNewThread_Cl ick(...)

Dim x as New delNewThread(Ad dressOf CreateNewFormTh readBegin)

Me.Invoke(x)

End Sub

Private Sub CreateNewFormTh readBegin()

Dim frm as New Form2

frm.ShowDialog( )

End Sub
that will create a new form on a different thread then show it
--
-iwdu15
Jul 17 '06 #6
well, if you wan to do it that way, then make sure our thread safe. Threads
can only access objects they create, for instance your Form1 cannot access
things on your other thread's form and vice versa, keep that in mind. To
access methods on other threads, use the threads Invoke method, dont call the
method directly. Keeping that in mind, there wouldnt be a need to call
"ShowDialog " and show the form on a new thread....i dont know if that would
cause issues or not, so your best bet would be calling "Show", not
"ShowDialog "
--
-iwdu15
Jul 17 '06 #7
Larry

I had a go at implementing your thought, but of course, the "real" code is
not quite as simple as the example I posted:

The real code passes a variable through to the form when constructing ie

dim frm as new myfrm(myvar)
dim t as new threading.threa d(addressof frm.myShowDialo g)
t.start

I can't work out how to create a shared function that I can call on another
thread AND pass a parameter!

Regards
Simon

--
=============== =============== ==
Simon Verona
Dealer Management Service Ltd
Stewart House
Centurion Business Park
Julian Way
Sheffield
S9 1GD

Tel: 0870 080 2300
Fax: 0870 735 0011

"Larry Lard" <la*******@hotm ail.comwrote in message
news:11******** *************@m 73g2000cwd.goog legroups.com...
>
Simon Verona wrote:
>I think I'm doing this wrong :

I have a class with a public shared method such as follows:

public shared sub myFunction
dim frm as new myFrm
dim t as new Threading.Threa d(Addressof myFrm.myShowDia log
t.start
end sub

In the frm - the myShowDialog just does a "me.showdialog" .

The idea is that this function will display a form, but then return
control
back to the calling routine immediately without waiting for the form to
be
closed.

If this is what you want, why not just .Show (rather than .ShowDialog)
the form? Anyway...
>>
It works, but I've had several reports of the form that is displayed
"crashing" as it is being displayed, with random errors (I've not got a
sample) or the whole application just disappearing (without error).

Can anybody assist, either by confirming or otherwise that what I'm doing
is
reasonable, or perhaps letting me know a better way.

I think you are falling foul of the rule whereby UI elements cannot be
safely interacted with from threads other than their 'owning' thread -
that on which they are created. So here the myFrm is created on the
thread that enters this procedure, but then passed to a different
thread to show itself. Thus this second thread will be accessing UI
elements it didn't create -bad.

The fix (if there are deeper reasons why you need to do this sort of
thing) is to move the form creation from before thread-creation to
after it. That is, at the moment you are saying:

Create form
Create thread (giving it the form)
Start thread - thread goes on to show form

Instead you should say

Create thread
Start thread - thread goes on to create form, then show it.

Probably the logical place for this new routine would be a Shared
procedure in myFrm:

Public Shared Sub NewShowDialog
Dim f As New myFrm
f.ShowDialog
End Sub

then replace your current myFunction with

Dim t As New Thread(AddressO f myFrm.NewShowDi alog)
t.Start

But please first consider my first question about .Show vs
.ShowDialog...

--
Larry Lard
Replies to group please
When starting a new topic, please mention which version of VB/C# you
are using

Jul 17 '06 #8
look at my code example above
--
-iwdu15
Jul 17 '06 #9
Thanks for the example...

However, I'm not 100% positive how I would modify your example to pass a
variable to the form.

I presume that I can't set a local private variable (declared outside of ay
subroutines) and then access it from within the CreateNewFormTh readBegin
sub? I'm presuming that can I can't directly pass any variables directly
through to the CreateNewFromTh readBegin (ie modifying it to Private Sub
CreateNewFromTh readBegin(myvar as string) )

Apologies if I've missed the obvious - Can I use the facts that it's 5pm and
very hot to mitigate my stupidity? <G>

Thanks
Simon

--
=============== =============== ==
Simon Verona
Dealer Management Service Ltd
Stewart House
Centurion Business Park
Julian Way
Sheffield
S9 1GD

Tel: 0870 080 2300
Fax: 0870 735 0011

"iwdu15" <jmmgoalsteraty ahoodotcomwrote in message
news:D6******** *************** ***********@mic rosoft.com...
look at my code example above
--
-iwdu15

Jul 17 '06 #10

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

Similar topics

65
6771
by: Anthony_Barker | last post by:
I have been reading a book about the evolution of the Basic programming language. The author states that Basic - particularly Microsoft's version is full of compromises which crept in along the language's 30+ year evolution. What to you think python largest compromises are? The three that come to my mind are significant whitespace, dynamic typing, and that it is interpreted - not compiled. These three put python under fire and cause...
2
2994
by: Egor Bolonev | last post by:
hi all my program terminates with error i dont know why it tells 'TypeError: run() takes exactly 1 argument (10 given)' =program==================== import os, os.path, threading, sys def get_all_files(path): """return all files of folder path, scan with subfolders
77
5392
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for the moment. I'd be *very* grateful if people with any interest in multi-threading would read it (even just bits of it - it's somewhat long to go through the whole thing!) to check for accuracy, effectiveness of examples, etc. Feel free to mail...
6
555
by: CK | last post by:
I have the following code in a windows service, when I start the windows service process1 and process2 work fine , but final process (3) doesnt get called. i stop and restart the windows service and the final process(3) gets called. what am I doing wrong with the threading? by the way Directory.GetFiles(IncomingXMLPath1).Length is some global outcome from process 1. Thanks 1)
2
2249
by: Vjay77 | last post by:
In this code: Private Sub downloadBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) If Not (Me.downloadUrlTextBox.Text = "") Then Me.outputGroupBox.Enabled = True Me.bytesDownloadedTextBox.Text = "" Me.totalBytesTextBox.Text = ""
11
5043
by: Paul Sijben | last post by:
I am stumped by the following problem. I have a large multi-threaded server accepting communications on one UDP port (chosen for its supposed speed). I have been profiling the code and found that the UDP communication is my biggest drain on performance! Communication where the client and the server are on the same machine still takes 300ms or sometimes much more per packet on an Athlon64 3000+ running Linux (Fedora Core 5 x64). I must...
17
6440
by: OlafMeding | last post by:
Below are 2 files that isolate the problem. Note, both programs hang (stop responding) with hyper-threading turned on (a BIOS setting), but work as expected with hyper-threading turned off. Note, the Windows task manager shows 2 CPUs on the Performance tab with hyper-threading is turned on. Both Python 2.3.5 and 2.4.3 (downloaded from python.org) have this problem. The operating system is MS Windows XP Professional.
0
1597
by: kingcrowbar.list | last post by:
Hello Everyone I have been playing a little with pyGTK and threading to come up with simple alert dialog which plays a sound in the background. The need for threading came when in the first version i made, the gui would freeze after clicking the close button until pygame finished playing the sound. In Windows it was acceptable because it could be ignored easily, but in
7
2379
by: Mike P | last post by:
I am trying to write my first program using threading..basically I am moving messages from an Outlook inbox and want to show the user where the process is up to without having to wait until it has finished. I am trying to follow this example : http://www.codeproject.com/cs/miscctrl/progressdialog.asp But although the messages still get moved, the progress window never does anything. Here is my code in full, if anybody who knows...
126
6760
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard: http://www.cse.wustl.edu/~schmidt/ACE.html the same way that the STL (and subsequently BOOST) have been subsumed? Since it already runs on zillions of platforms, they have obviously worked most of the kinks out of the generalized threading and processes idea (along with many...
0
9718
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10107
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.