Start form from a console application?

G

Guest

Hi,

I want to start a windows form from a console application within a new
thread. Is it reasonable to use the BackgroundWorker component for that? Are
there any other/better startegies for accomplishing this task? (The form is
used to display an encoding preview from windows media encoding)
Creating a new application context and calling Application.Run(appContext)
when a new thread starts doesn't work for me (creates the form but the form
is blocked somehow, the console application continues).


Best Regards,
 
G

Guest

Armin

Hi. I got this working by just grabbing a thread from the ThreadPool, as
shown below. Hope this helps.

Kevin

class Program
{
static void Main(string[] args)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(ShowForm));
Console.WriteLine("Console app running. Press enter to end..");
Console.ReadLine();
}

static void ShowForm(object state)
{
Application.Run(new MyForm());
}
}

class MyForm : Form
{
public MyForm()
{
Button btnClose = new Button();
btnClose.Text = "Close";
btnClose.Click += new EventHandler(btnClose_Click);
Controls.Add(btnClose);
}

void btnClose_Click(object sender, EventArgs e)
{
Close();
}
}
 
S

Stoitcho Goutsev \(100\)

Armin,

I see that you got this running, but I think it is better if you create a
new Thread object and run the ShowForm in there.

Thread t = new Thread(new ThreadStart(ShowForm ));
t.IsBackground = false;
t.Run();

The reason being is that the thread pool threads are limited resource that
are used by the framework also. If one uses thread pool thread one should
free the thread as soon as it possible and not block it for a long time.
Beside this there is no difference between normal threads and thread pool
threads.
 
G

Guest

Stoitcho

Good point, I just wanted to demonstrate using the ThreadPool to launch a
window works as described. AFAIK the BackgroundWorker runs on a ThreadPool
thread so the problem Armin describes must be related to some other issue
e.g. the appContext ctor is blocking the UI thread.

Kevin
 
G

Guest

Hi. First I have to thank you for all your suggestions.
I still have to try your examples with my special application. But to
clarify things up I never used the BackgroundWorker component. It was just a
question if it makes sense in my situation. The solution I tried and what I
have outlined in the first post was using a separate thread which executed a
method (similar to Stoitcho's example). This method called
"Application.Run(appContext)" with the newly created appContext. I think my
problem was that from the console application I instantiated another class
and in that class I wanted to open the form.
Anyway I still have to try a little bit...
 
S

Stoitcho Goutsev \(100\)

Armin,

I suggest to post some compilable sample code - simple console application
that opens an empty form in the way you want.

As for the special object that opens the form it probably doesn't matter in
which thred you create the object what matters is what thread executes the
code that calls Application.Run.
 
G

Guest

Thanks again for your suggestions. I finally got it to run. But unfortunately
now I've run into another problem (described in a new thread).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top