Question Problem in thread.

priyamtheone

Well-known member
Joined
Sep 20, 2007
Messages
96
Programming Experience
Beginner
Is there any way possible in a windows application project to kill the default current thread of the project and run a new thread and then relate the project to it?

My case scenario:-
I have a startup form. In its Load event I want to kill the current thread, create a new thread and run the project through the new thread. When I run, the new thread is being created, everything is ok. But as soon as the default/current thread is being killed the application is closing down. How can I tackle this? Provided, I don't want to hide the startup form or stuffs like that.
Plz help. Regards.
 
If you want to perform some startup checks you can always use the Application Events. I often use the Startup Event to open a login form and if it is valid the app continues if not then it shuts down, the startup form isn't even loaded.
 
Problem in Thread.

Let me ask you one simple thing, what are you trying to achieve and why are you trying it?

Bobby

Here's a brief summary:

As we have seen, if u try to treat a startup form as a splash form and try to close it and goto the 2nd form the application closes down. If u try to do this by writing the following code in the startup form:
Application.Run(frm2ndForm)
Me.Close()

It'll give an error message something like 'More than one message cann't be passed/processed through a single thread. Try using frm2ndForm.ShowDialog();'. Keeping this in mind I'm trying to close the initial thread in which frmStartup is running, create and run a new thread and then open frm2ndForm from it. Hope u understand. Looking forward 4 ur reply.
Thanx.
 
Problem in Thread.

If you want to perform some startup checks you can always use the Application Events. I often use the Startup Event to open a login form and if it is valid the app continues if not then it shuts down, the startup form isn't even loaded.

Would u please focus on the 'Startup Event' part. B'coz if I'm not wrong I can't see it in project properties from where we choose what to run during start up. Am i missing something? Thanx.
 
Sure thing.

Right click the Project and view Properties. Select the Application tab.
Near the bottom of the Application properties you should see a button
(this is also next to the area where you can set a splash screen) ... View Application Events.
Click that and it will open the application events.

Here is an example of what I use
VB.NET:
   Private Sub MyApplication_Startup(ByVal sender As Object, _ 
ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup          

            'do Login
            Dim frmLogin As New Login
            If frmLogin.ShowDialog() = DialogResult.Cancel Then
                e.Cancel = True
            End If

        End Sub

My login form opens first and performs username and password checks to a SQL database. If the login isn't correct it returns DialogResult.Cancel and then e.Cancel = True causes the application to not load, in a nut shell.
 
Problem in Thread.

Thanks demausdauth. I'll definitely look into it and let know how it ends up in my environment. Thanks again.
 
Sure thing.

Right click the Project and view Properties. Select the Application tab.
Near the bottom of the Application properties you should see a button
(this is also next to the area where you can set a splash screen) ... View Application Events.
Click that and it will open the application events.

Here is an example of what I use
VB.NET:
   Private Sub MyApplication_Startup(ByVal sender As Object, _ 
ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup          

            'do Login
            Dim frmLogin As New Login
            If frmLogin.ShowDialog() = DialogResult.Cancel Then
                e.Cancel = True
            End If

        End Sub

My login form opens first and performs username and password checks to a SQL database. If the login isn't correct it returns DialogResult.Cancel and then e.Cancel = True causes the application to not load, in a nut shell.

Hi demausdauth. I studied ur view and it's clear to me. It's perfect to use according to ur situation and control flow. But mine is a little different where ur view needs modification. I'm describing my situation. Please let me know about ur conception on it.

I have frmSplash. That's the startup form of my project. In its load event I don't want to show it, instead it'll perform a checking whether the application is registered. If yes then show frmSplash. If someone clicks on frmSplash, it'll open frmMain. Upto this part is manageable with the code u wrote.
Now if the application is not registered, frmRegister opens up. If someone cancels frmRegister the app exits, but if someone registers the app from here successfully, frmRegister closes and frmMain opens up. This is where the problem is. According to ur code u can open frmRegister as modal window, fine. If it's cancelled the app exits, cool. But if someone registers successfully and u need to show frmMain then u need to allow the app to startup in ur Startup Event. When I do that both frmSplash and frmMain are opening up simultaneously b'coz frmSplash is my startup form.

Hope I could make u clear. There r small tricks to tackle this problem e.g. Set frmSplash.Visible=False and frmSplash.ShowInTaskBar=False in frmSplash_Load event. But I don't rely on tricks. I would choose the proper way. Looking 4ward 4 ur further advice. Thanks again.
 
I'd suggest the following layout:

* Set Application_Exit-Behaviour to "Last form closed".
* Set the Splash-Screen form as StartUp-Form
* Process the OnClick-Event of the Splash-Screen and determine if the application is registered, if yes, close the Splash-Screen and show the MainForm, if no, close the Splash-Screen and show the register-form.

VB.NET:
Me.Close()
Main.Show()

* On cancel in the registration form, simply close this form. This will shutdown the application

Bobby
 
Back
Top