Multi-threading forms?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a "Main" ASP.NET 2 Form that could spawn off other forms using
//e.g.
NewSubForm myNewForm = new NewSubForm();
myNewForm.Show();
myNewForm.Activate();

The other forms perform something intensive "e.g. Invoke Oracle Stored
Procedure that take more than 10 seconds to bring back data". However,
during this time, the main form are not accessible (all white), nor are other
sub-forms that had previously been spawned.

I thought unlike the VB6 days, forms are meant to be multi-threaded to
prevent this from happening? How could I change the form
loading/instantiating to make them more multi-threaded?
 
I don't think you meant to say ASP.NET there did you? This is a Winforms
app not a web app right?

To answer your question, the GUI runs in a single thread. However, you can
easily create new threads and run your Oracle methods from that thread. For
example you could do this:

class MyForm : Form
{
<Constructors And Initializer Code>

void DoOracleProcedure()
{
<Do long DB Code here>
}

void Button1_Click(object sender, EventArgs args)
{
Thread th = new Thread(new ThreadStart(DoOracleProcedure));
th.Start();
}
}

With this, when you click the button a new thread will be created with an
entry point of DoOracleProcedure. When Start is called it will call the
method and the thread will run until the DoOracleProcedure returns. Any
method calls the DoOracleProcedure makes will run in the new thread. This
will keep your GUI responsive.

One thing to note, however. Should you need to update your GUI from a
separate thread there are some loops you need to jump through. Nothing
terribly difficult. Just mildly annoying.
 
Back
Top