473,387 Members | 1,455 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,387 software developers and data experts.

Multi-threading causes problems

Max
Playing around with multi-threading programs and ran into this little
problem that maybe someone here could explain... Basically I have a
class which launches a form object. Once the form is launched (using
ShowDialog) I still needed both to be available, so my solution was to
start the form in a new thread. This way the program itself can still
use the class and the user can interact with the form. So here's how I
did this:
Public Sub OpenForm()
GUIThread = New Thread(GUIThreadStart)
GUIThread.Start()

Thread.CurrentThread.Sleep(1000)
End Sub

Private Sub MyThread()
MyForm.ShowDialog()
End Sub

I didn't put in all the definitions, but basically MyThread() is the
starting point for the GUIThread. The problem comes in where you see
that Sleep(1000) line. If I don't put that line in the form opens up
completely unresponsive and essentially looks frozen. Why does that
happen? I thought as soon as I start the new thread it just goes along
its own road. Why do I need to pause the execution of the main thread in
order to have the form be able to show?
Nov 20 '05 #1
4 1248
Max,

something is not right here. Your ShowDialog is static (shared)? Or MyForm
is instance reference?
Where it was instantiated?

I used successfully similar technique, but form was instantiated ( = new
MyForm) in the thread too. And no sleep was required. It could be necessary
only if after OpenForm thread goes into tight loop and doesn't allow other
threads to run.

HTH
Alex

"Max" <ma*****@yahoo.com> wrote in message
news:zo********************@comcast.com...
Playing around with multi-threading programs and ran into this little
problem that maybe someone here could explain... Basically I have a
class which launches a form object. Once the form is launched (using
ShowDialog) I still needed both to be available, so my solution was to
start the form in a new thread. This way the program itself can still
use the class and the user can interact with the form. So here's how I
did this:
Public Sub OpenForm()
GUIThread = New Thread(GUIThreadStart)
GUIThread.Start()

Thread.CurrentThread.Sleep(1000)
End Sub

Private Sub MyThread()
MyForm.ShowDialog()
End Sub

I didn't put in all the definitions, but basically MyThread() is the
starting point for the GUIThread. The problem comes in where you see
that Sleep(1000) line. If I don't put that line in the form opens up
completely unresponsive and essentially looks frozen. Why does that
happen? I thought as soon as I start the new thread it just goes along
its own road. Why do I need to pause the execution of the main thread in
order to have the form be able to show?

Nov 20 '05 #2
Max
Yea something strange is going on, for example, if the first thread
finishes before the form is closed the form also seems to freeze for
some reason. I have to suspend the main thread just before it ends and
then resume it at the end of the second thread. The way my program runs
is, I have some class A and a form B, inside class A I define a new
instance of B, something like Private MyForm as New B. Sub Main creates
a new instance of A with Dim MyClass as New A, and then I make the call
MyClass.OpenForm. The idea is to have the form opened and allow the user
to interact with it, but let the program execution continue and not
close the form even if it reaches the end. So my program ends only when
both the main thread and the GUIThread have ended.

Do you notice anything wrong with what I’m doing?
AlexS wrote:
Max,

something is not right here. Your ShowDialog is static (shared)? Or MyForm
is instance reference?
Where it was instantiated?

I used successfully similar technique, but form was instantiated ( = new
MyForm) in the thread too. And no sleep was required. It could be necessary
only if after OpenForm thread goes into tight loop and doesn't allow other
threads to run.

HTH
Alex

"Max" <ma*****@yahoo.com> wrote in message
news:zo********************@comcast.com...

Playing around with multi-threading programs and ran into this little
problem that maybe someone here could explain... Basically I have a
class which launches a form object. Once the form is launched (using
ShowDialog) I still needed both to be available, so my solution was to
start the form in a new thread. This way the program itself can still
use the class and the user can interact with the form. So here's how I
did this:
Public Sub OpenForm()
GUIThread = New Thread(GUIThreadStart)
GUIThread.Start()

Thread.CurrentThread.Sleep(1000)
End Sub

Private Sub MyThread()
MyForm.ShowDialog()
End Sub

I didn't put in all the definitions, but basically MyThread() is the
starting point for the GUIThread. The problem comes in where you see
that Sleep(1000) line. If I don't put that line in the form opens up
completely unresponsive and essentially looks frozen. Why does that
happen? I thought as soon as I start the new thread it just goes along
its own road. Why do I need to pause the execution of the main thread in
order to have the form be able to show?


Nov 20 '05 #3
Hi Max,

There is no need in my opinion for multithreading, as long as the user is
not handling form1 it does nothing untill it get a message that a user has
something done on that form and than an event will be raised.

However with the form.showdialog that is not even the situation, because
than your main form is disabled and will be enabled back when the showform
gives a message back (which is standard when the dialogform closes).

I hope this helps?

Cor

Nov 20 '05 #4

"Max" <ma*****@yahoo.com> wrote in message
news:n6********************@comcast.com...
<snip>... Sub Main creates a new instance of A with Dim MyClass as New A, and then I make the call
MyClass.OpenForm. The idea is to have the form opened and allow the user
to interact with it, but let the program execution continue and not
close the form even if it reaches the end. So my program ends only when
both the main thread and the GUIThread have ended.


I don't see where and how you start main message pump for application. There
should be somewhere Application.Run call. Additionally, you should create
form on UI thread. I think it is due to how dialog messages are dispatched
by main message loop and which controls are on threaded form. Attach main
form event handler to Closing event of threaded form and then you will be
notified when form is closed. You can then Sleep or Join to wait until
threaded form is closed - Join would be better.

HTH now
Alex
Nov 20 '05 #5

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

Similar topics

4
by: Frank Jona | last post by:
Intellisense with C# and a multi-file assembly is not working. With VB.NET it is working. Is there a fix availible? We're using VisualStudio 2003 Regards Frank
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
0
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
6
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
6
by: Joe | last post by:
I have 2 multi-list boxes, 1 displays course categories based on a table called CATEGORIES. This table has 2 fields CATEGORY_ID, CATEGORY_NAME The other multi-list box displays courses based on...
4
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
5
by: bobwansink | last post by:
Hi, I'm relatively new to programming and I would like to create a C++ multi user program. It's for a project for school. This means I will have to write a paper about the theory too. Does anyone...
5
by: dkelly925 | last post by:
Is there a way to add an If Statement to the following code so if data in a field equals "x" it will launch one report and if it equals "y" it would open another report. Anyone know how to modify...
1
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier...
2
by: Aussie Rules | last post by:
Hi, I have a site that Iwant to either display my text in english or french, based on the users prefernces ? I am new to webforms, but I know in winforms, this is pretty easy with a resource...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.