tags:

views:

356

answers:

6

Hey I am fairly new to the c# programming language and have enjoyed my experience so far. I normally develop applications in java but a need for the win32 library has lead me to c# so far the languages seem relatively similar. however a problem I am having at the moment is a simple piece of code that can be best explained in this example

I am trying to print a piece of string to console then display the windows form then print another piece of string to console. however the result i get is first string is printed then the form is displayed, i then have to close the form before the last string is printed. the question i would like to ask is is there anyway to get this working so the second print to console is displayed immediately after the form is displayed. im guessing it has something to do with threading but I am not entirely sure how to implement this

A: 

My suggestion would be to start with a Console application. You can always open a WinForm from a console application...which would give you exactly what you're looking for. (you might want to think about multi-threading as well.

Justin Niessner
I have started with a console application. I think the solution may lie in multithreading thanks for your help
SteveCapegoat
A: 

are you using form.showDialog() or form.show()?

form.showDialog() will block until the form is closed, while form.show() will not.

echo
I'm using form.show()
SteveCapegoat
ok, good. but i have no idea how to solve your problem. :)
echo
+1  A: 
static class Program
{
    [STAThread]
    static void Main()
    {
        Console.WriteLine("first string");
        var form = new Form1();
        form.Show();
        Console.WriteLine("the second string");
        Application.Run();
    }
}
Eric Dahlvang
this code works but im trying to have the second string appear after the application.run(); the problem im having is the application has to close before this will appear
SteveCapegoat
Well, this is just hokey if you ask me...but, you could just call Application.Exit when the form closes, and modify your Main() like this:Console.WriteLine("first string");var form = new Form1();form.Show();Application.Run();Console.WriteLine("the second string");Application.Run();Then, you will be left with only the console window displaying both strings.
Eric Dahlvang
A: 

I would either start with a console app, and run the application from the static main function. This is from memory - I haven't had to mess with Winforms for years:

[STAThread]
static void Main()
{
  Application.Run(new YourMainForm()); 
}

or

I would redirect Console.Out and shove that stream into a some sort of control like a text box or list box. This is a lot of code but doable... I have written this before for on-site debugging but don't have the code handy.

Jason Jackson
A: 

Hey everyone thanks for your answers I made some progress with what im trying to achieve but im not sure how correct or thread safe it is here is the code i got to run

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows.Forms;

    namespace Project1
    {
    class Class2
    {

        [STAThread]
        static void Main()
        {
           Console.WriteLine("hello");
           Class2 t = new Class2();
           t.test();
           Console.WriteLine("second string");
        }



        public void test()
        {
            Thread t = new Thread(new ThreadStart(StartNewStaThrea));
            t.Start();
        }


        private void StartNewStaThrea()
        { 
            Application.Run(new Form1()); 
        }

    }
}

pls let me know what you think of this solution

SteveCapegoat
Note: you should edit your original question with this info instead of posting as an answer.
Robert
Sorry Rob I will keep this noted for the future
SteveCapegoat
+2  A: 

It sounds like you want to be able to output messages to the console while the form is being displayed, correct? The basic issue is that as long as the form is visible, there must be a message loop running, handling events for the form. The message loop is inside Application.Run, so once you call it, it won't return until the form is closed (as you discovered).

So if you want to write to the console while the form is visible, you have a couple of options. One, as you mentioned, is to use multiple threads. Let your main thread run the message loop, and start up a second thread to write to the console. But that's not necessary--you can also write to the console from within an event handler, directly or indirectly. There's nothing wrong with doing a Console.WriteLine from within a button click handler. Or you can have your button handler call a method in your Program class, and do the writing there.

As for which solution is better, it would help to know more about what you're trying to accomplish. I assume that you don't just want to write stuff to the console--what else is it that you want to do while the form is being displayed?

Aaron
Hi Aaron thank you for your answer, this is indeed what im trying to accomplish. so far i have managed to achieve something basic with the threading idea i have posted an answer here let me know what you think. what im trying to accomplish is an application that follows the MVC design pattern. when creating these applications I like to start with an instance of the model and the view being created from the driver class although perhaps this is bad practice as there should be very little code to do with the application here as possible
SteveCapegoat
You shouldn't have to use threads just to implement an MVC architecture (and I would caution you against using threads unless you have to--they are complicated and notoriously hard to get right). Just because your model, view, and driver are in separate classes doesn't mean they have to run in separate threads. A Google search for "windows forms mvc" should give you some pointers on how to implement MVC in a Windows Forms app.
Aaron