Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / May 2006

Tip: Looking for answers? Try searching our database.

Start form from a console application?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Armin Prosch - 15 May 2006 07:59 GMT
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,
Kevin Hector - 15 May 2006 08:45 GMT
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();
    }
}
Stoitcho Goutsev (100) - 15 May 2006 14:12 GMT
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.

Signature

Stoitcho Goutsev (100)

> Armin
>
[quoted text clipped - 33 lines]
>     }
> }
Kevin Hector - 15 May 2006 14:35 GMT
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
Armin Prosch - 15 May 2006 22:17 GMT
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...
Stoitcho Goutsev (100) - 16 May 2006 13:35 GMT
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.

Signature

Stoitcho Goutsev (100)

> Hi. First I have to thank you for all your suggestions.
> I still have to try your examples with my special application. But to
[quoted text clipped - 10 lines]
> and in that class I wanted to open the form.
> Anyway I still have to try a little bit...
Armin Prosch - 23 May 2006 20:46 GMT
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).

> Armin,
>
[quoted text clipped - 19 lines]
> > and in that class I wanted to open the form.
> > Anyway I still have to try a little bit...

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.