473,756 Members | 8,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(GUIThrea dStart)
GUIThread.Start ()

Thread.CurrentT hread.Sleep(100 0)
End Sub

Private Sub MyThread()
MyForm.ShowDial og()
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 1276
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******** ************@co mcast.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(GUIThrea dStart)
GUIThread.Start ()

Thread.CurrentT hread.Sleep(100 0)
End Sub

Private Sub MyThread()
MyForm.ShowDial og()
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.OpenFor m. 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******* *************@c omcast.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(GUIThrea dStart)
GUIThread.Start ()

Thread.CurrentT hread.Sleep(100 0)
End Sub

Private Sub MyThread()
MyForm.ShowDial og()
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******** ************@co mcast.com...
<snip>... Sub Main creates a new instance of A with Dim MyClass as New A, and then I make the call
MyClass.OpenFor m. 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
4673
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
3879
by: * ProteanThread * | last post by:
but depends upon the clique: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=954drf%24oca%241%40agate.berkeley.edu&rnum=2&prev=/groups%3Fq%3D%2522cross%2Bposting%2Bversus%2Bmulti%2Bposting%2522%26ie%3DUTF-8%26oe%3DUTF-8%26hl%3Den ...
0
3787
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 and white CCITT4 compressed files (frames) inside the tiff. Every now and then I receive a mixture of black and white CCITT4 and JPEG compressed files, and sometimes just multi-page tiffs with JPEG only. The code runs great when dealing with the...
6
8179
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
4893
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 a table called COURSES. This table has 2 fields CATEGORY_ID, COURSE_NAME. The CATEGORY_ID is a FK in COURSES and a PK in CATEGORIES. I want to populate the course list box based on any category(s)
4
17874
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 problem is the charset? How I can avoid this warning? But the worst thing isn't the warning, but that the program doesn't work! The program execute all other operations well, but it don't print the converted letters: for example, in the string...
5
5766
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 know a good place to start looking for some theory on the subject of multi user applications? I know only bits and pieces, like about transactions, but a compendium of possible approches to multi user programming would be very appreciated!
5
3283
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 this? Private Sub cmdPreview_Click() On Error GoTo Err_Handler 'Purpose: Open the report filtered to the items selected in the list box. 'Author: Allen J Browne, 2004. http://allenbrowne.com Dim varItem As Variant 'Selected items
1
9316
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 "PAR.UniqueID" could not be bound. The multi-part identifier "PAR.PAR_Status" could not be bound. The multi-part identifier "Salary.New_Salary" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part...
2
4657
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 file. What is the best way to acheive this with webforms ?
0
9462
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
10046
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9886
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9722
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...
1
7259
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
6542
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5155
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...
1
3817
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
2677
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.