• 0

[C#] threads and Application.Run() ?


Question

Doing some not very smart application, but running into a lot of trouble with it..

the problem:

when the application starts, there's a login and check for updates form.

when a user logins, the 'check updates' part is enabled, and the main application window should open

the updates window and the application window can work separatly.

i start the login form with

Application.Run(new Login_Form());

and when the approval button is pressed

i want to hide this form, and show both 'update' and 'main' forms, each with a different thread.

but bad things allways happen at this stage... :no:

how do i do "Application.Run()" twice?

Link to comment
Share on other sites

11 answers to this question

Recommended Posts

  • 0

so i'm creating new thread and tell him to do something like:

thread task:

Main_Form main = new Main_Form();

main.Show()

and the same for the 'update' window.

and what is the Application.Run() thread?

the one that started the 'login' form?

can i kill it?

Link to comment
Share on other sites

  • 0

yes, it all dies

but then i cant close the first form = the login

i cant do Close(), not Dispose(), only Hide(),

and then, when i close the main window - application keeps running - cos the login window is not cosed but hidden.

Link to comment
Share on other sites

  • 0

This is what i'm trying now.

But it does not work for me - seems like the 'normal' thread (System.Threading.Thread) does not have a message queue and a message\event loop

so that form is shown for a split second, disappears, and the application ends.

are there special threads for GUI objects?

in java there's only one Message Queue for app., and diffenent threads use the same one, done with worker threads and with invoke \ invokeLater functions.

trying to do it in c# but with no success.

Link to comment
Share on other sites

  • 0

In your case, you'd want the new worker threads to loop as many times as needed to finish a job, and if there's nothing to do, you can Sleep() them for a while (Y)

Link to comment
Share on other sites

  • 0
This is what i'm trying now.

But it does not work for me - seems like the 'normal' thread (System.Threading.Thread) does not have a message queue and a message\event loop

so that form is shown for a split second, disappears, and the application ends.

are there special threads  for GUI objects?

in java there's only one Message Queue for app., and diffenent threads use the same one, done with worker threads and with invoke \ invokeLater functions.

trying to do it in c# but with no success.

585402696[/snapback]

This is what I would do. Have Application.Run launch a new instance of your main form. In the form's load event handler, show the logon screen. If the user authenticates, then continue to show your main form, otherwise, reshow the logon, or exit. If they authenticate, spawn the thread to do your update, if the option was checked. Also, note that you shouldn't update UI from a thread other than the UI's main thread. Granted, if your worker thread instantiates its own form, then it can update it, but it shouldn't try to update the main form without using Invoke.

Link to comment
Share on other sites

  • 0

i think it works.. he he :)

not sure that it's the right way..

1) started the main form with Application.Run()

2) run the 'login' from the load

did it with form.invoke() - donno if it's the right thing..

3) the login gets the main window as a paremeter, and calls "continue to run" if passes, and "exit" if not

- donno how to do it in other way

that's it i think.

and the login is not in a sparate thread, but the 'update' is.

---

now, will be vary happy to hear some explanation about what happens there...

'cos i dont seem to understand it

i know how to run work thread - to do not gui functions.

but dont understand those gui thread stuff, and what the Application.Run has to do with it.

thanks.

Link to comment
Share on other sites

  • 0
i think it works.. he he :)

not sure that it's the right way..

1) started the main form with Application.Run()

2) run the 'login' from the load

did it with form.invoke() - donno if it's the right thing..

3) the login gets the main window as a paremeter, and calls "continue to run" if passes, and "exit" if not

- donno how to do it in other way

that's it i think.

and the login is not in a sparate thread, but the 'update' is.

---

now, will be vary happy to hear some explanation about what happens there...

'cos i dont seem to understand it

i know how to run work thread - to do not gui functions.

but dont understand those gui thread stuff, and what the Application.Run has to do with it.

thanks.

585404527[/snapback]

You wouldn't need to use Invoke for the login form. It is being instanced on the main form's thread. If you do a ShowDialog(this), 'this' being the main form's instance, the login form will block the main form's thread until it returns, so you won't need to call a "continue to run" method.

I don't know how you're doing your login form, but usually you check the DialogResult. You can set your login form's OK/Cancel(or whatever) buttons to have DialogResult values. If the login fails, have the login form prompt the user with another message box to retry or cancel. Based on what happens there, you can return an appropriate DialogResult to the main form.

// in the FormLoad handler
LoginForm login = new LoginForm();

if( login.ShowDialog(this) == DialogResult.OK )
{
    // do other initialization
}
else
{
    // cleanup exit the app
}

Here's an example of what your Login may look like

// inside the OK/Login button click handler
if( DoAuthentication() == true )
{
    this.DialogResult = DialogResult.OK;
    this.Close();
}
else
{
    // throw up a message box asking the user to try again, or cancel
    if( MessageBox.Show("Invalid username/password combination", 
        "Authentication Failure", MessageBoxButtons.RetryCancel, MessageBoxIcon.Exclamation) == DialogResult.Retry )
    {
       // set focus to username box
    }
    else
    {
        this.DialogResult = DialogResult.Cancel;
        this.Close();
    }
}

There are a few more ways to do this, but maybe this will help you understand some more of what you can do.

Link to comment
Share on other sites

  • 0

Thanks a lot

didnt know about DialogResult nor ShowDialog, both sound very appropriate for my prog.

Will try it first thing in the morning.

:sleep: Good night and thank you

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.