473,480 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem in opening form through Threads

14 New Member
hi,

I have created a Windows application. I want to launch a form using thread. But this is not working well.

Following is my code:
Expand|Select|Wrap|Line Numbers
  1.     Private Sub cmdShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdShow.Click
  2.         Dim thForm As Threading.Thread
  3.         Dim i As Int32
  4.  
  5.         For i = 0 To 20
  6.             thForm = New Threading.Thread(AddressOf ShowForm)
  7.             thForm.Start()
  8.         Next
  9.     End Sub
  10.  
  11.     Private Sub ShowForm()
  12.         Dim F2 As Form2
  13.  
  14.         F2 = New Form2
  15.         F2.Show()
  16.     End Sub
  17.  
In the above example 'cmdShow' is a button. On clicking a button i want to open form2 using a separate thread.

The problem is that, 'Form2' become disappear after launching.
But when i use F2.ShowDialog(), it will working well.

I can't be able to understand the problem. Can anyone help me?

Regards,
Ashish
Jul 7 '08 #1
5 975
Plater
7,872 Recognized Expert Expert
All GUI objects are to be run in the same GUI thread.
Running them in other threads prevents them from getting the windows messages correctly.
I think there is a way around this, but generally, there never be a need to run GUI objects in seperate threads.
Jul 7 '08 #2
Ashish Gupra
14 New Member
All GUI objects are to be run in the same GUI thread.
Running them in other threads prevents them from getting the windows messages correctly.
I think there is a way around this, but generally, there never be a need to run GUI objects in seperate threads.
hi,

thanks for reply..

Now i have solved my problem. I have use the following code:

Private Sub cmdShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdShow.Click
Dim thForm As Threading.Thread
Dim i As Int32

For i = 0 To 20
thForm = New Threading.Thread(AddressOf ShowForm)
thForm.Start()
Next
End Sub

Private Sub ShowForm()
Dim F2 As Form2
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(AddressOf ShowForm))
Else
F2 = New Form2
F2.Show(Me)
End If
End Sub

The above code is working well.

Regards,
Ashish
Jul 8 '08 #3
Plater
7,872 Recognized Expert Expert
Why do you need your gui object to run in a different thread?
Jul 8 '08 #4
Ashish Gupra
14 New Member
Why do you need your gui object to run in a different thread?
Hi,

In my application these forms are like alerts and i dont know how many alerts occur in a minute. Also alerts get its records for database. So i do not want to get busy my main thread to launch these alerts...

Is there any other solution to avoid using threads?
Also Please lat me know future problem in using thread in such operations.

Regards,
Ashish
Jul 9 '08 #5
Curtis Rutland
3,256 Recognized Expert Specialist
I suggest that you look into using BackgroundWorkers. They let you execute operations on separate threads in a thread-safe way.
Jul 9 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
4007
by: Vinay Aggarwal | last post by:
I have been thinking about the lazy initialization and double checked locking problem. This problem is explain in detail here http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html...
0
595
by: Thompson Yip | last post by:
From time to time, I randomly receive "Can't updated, Currently locked" error 3218 or 3246 from the following code in one of my form with pessimistic lock for 70 users environment. Any problem with...
7
2597
by: Simon Verona | last post by:
I have a problem in my application which I believe is due to open handles.. . The symptom that users report is that after they have been using the application for a while, it will randomly just...
4
7111
by: MrL8Knight | last post by:
Hello, I am trying to build a simple php form based shopping cart using a cookie with arrays. I need to use 1 cookie because each order will have over 20 items. With that said, I realize I need to...
5
2916
by: Alan T | last post by:
I got this kind of error recently: When I tried to open a form I got error something like this: One of more errors encountered while loading the designer. The errors are listed below. Some...
1
1644
by: robertmeyer1 | last post by:
Hey, I am having a problem with opening some forms. I have several forms. The forms are based off the same table, tblClient. Each form has a sbf inserted into it. These sbf’s are each based...
2
2317
by: Brian K. | last post by:
I've read several posts on this problem, since it's happening to me right now. Can't seem to get rid of this. Using A2K2 on a large XP network. PC front ends linked to a backend on a shared...
4
2501
by: cyrous xiao | last post by:
I hvae three thread and a thread that is used to show a winform. The code: Thread ThreadDialog= new Thread (new ThreadStart (ShowDialog)); //Show a winform Dialog ThreadDialog.Start ();...
0
920
by: thiago777 | last post by:
Hi! Im still trying to make my application work in an event-driven way. It had worked so far with my threads, but the problem Im having is only when modifying a GUI component from the event handler...
0
7033
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,...
0
6903
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
7027
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,...
0
7071
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...
1
6726
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5318
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,...
0
2987
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...
0
1291
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 ...
1
557
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.