dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Program stops working when the form looses focus

  1. #1
    Join Date
    Aug 2009
    Posts
    4

    Program stops working when the form looses focus

    Guru's,
    I am brand spanking new to c#. I am using VS 2005 and trying to modify a console app to a windows app.
    I added a form with a couple of textbox and got the main proc in program.cs to update the textboxes in the form. I changed the properties and got it to build etc.
    Miraculously it also works. But the issue I am having is anytime the form looses focus, the program stops working. It will not work even when the window gets focus back.
    Any help will be much appreciated.
    Thanks.

  2. #2
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Program stops working when the form looses focus

    can you describe what you mean by "stops working"? can you post the code?

  3. #3
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Program stops working when the form looses focus

    Also what does the program do? As already mentioned, please post the code and show us what your program is supposed to do.

  4. #4
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,539

    Re: Program stops working when the form looses focus

    Quote Originally Posted by hianupam_cg View Post
    Guru's,
    I am brand spanking new to c#. I am using VS 2005 and trying to modify a console app to a windows app.
    I added a form with a couple of textbox and got the main proc in program.cs to update the textboxes in the form. I changed the properties and got it to build etc.
    Miraculously it also works. But the issue I am having is anytime the form looses focus, the program stops working. It will not work even when the window gets focus back.
    Any help will be much appreciated.
    Thanks.
    Why you dont simple create a new WinForm Application and then adding all the stuff you have in your console to your forms application in the way it will fit. ( Some things like writing to a console will not bring you the wanted effect in a WinForm Application as this would only write to your 'Direct Window ' then
    IMHO much easier then to figure around whats wrong with your application.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  5. #5
    Join Date
    Aug 2009
    Posts
    4

    Re: Program stops working when the form looses focus

    Thanks all for replying to my thread.
    Brief synopsis of the program:
    It calls a soap function continuously to check for new data:
    Here is the call to the soap function:

    namespace eDataFeedClient



    {
    public class GetData
    {
    public collectionActualValueData getmyace(String uid, String pswd, String url)
    {
    // create the web service and point it at the correct URL
    EDataFeedService service = new EDataFeedService();
    service.Url = url;
    // Create the WS-Security token for the SOAP header
    UsernameToken userToken = new UsernameToken(uid,
    pswd,
    PasswordOption.SendPlainText);
    service.RequestSoapContext.Security.Tokens.Add(userToken);

    // Create the network credentials and assign
    // them to the service credentials
    NetworkCredential netCredential = new NetworkCredential(uid, pswd);
    Uri uri = new Uri(service.Url);
    ICredentials credentials = netCredential.GetCredential(uri, "Basic");
    service.Credentials = credentials;

    // Be sure to set PreAuthenticate to true or else
    // authentication will not be sent.
    service.PreAuthenticate = true;

    // this property is set to prevent 505 HTTP Version Not Supported in Tomcat
    ServicePointManager.Expect100Continue = false;

    // set this property to signal the server to compress the response
    service.EnableDecompression = true;

    // call the web service
    return(service.getAllAce());
    }

    public static void Main()
    {
    Application.Run(new frmdisplay());
    }


    }
    }

    The above code is in my program.cs
    The code below is in my display.cs

    namespace eDataFeedClient{



    public partial class frmdisplay : Form
    {
    public frmdisplay()
    {
    InitializeComponent();
    }






    public string AceTime
    {
    get { return this.lblAce_Time_Value.Text; }
    set { this.lblAce_Time_Value.Text = value; }

    }

    public string AceVal
    {
    get { return this.lblAce_Val_Value.Text; }
    set { this.lblAce_Val_Value.Text = value; }

    }

    public string TestVal
    {
    set { this.lblTest.Text = value; }

    }

    private void cmdStartApp_Click(object sender, EventArgs e)
    {
    String OUT_DATE_FORMAT = "HH:mm";
    collectionActualValueData adata;
    String uid = "someuid";
    String pswd = "somepassword";
    String url = "someurl";
    // Create a new GetData Class
    GetData gdata = new GetData();
    for (int i = 0; i < 500; i = i + 1)
    {
    adata = gdata.getmyace(uid, pswd, url);
    foreach (actualValueData node in adata.actualValueData)
    {

    this.AceTime = node.timestamp.ToString(OUT_DATE_FORMAT);
    this.AceVal = node.value.ToString();
    this.TestVal = i.ToString();
    }
    this.Refresh();
    Thread.Sleep(1000);
    }
    }



    }
    }

    I can build this and run it. The form shows up and when I click the button it goes and fetches the data. But the moment I click on anything else like say a different window, the data in the form stops refreshing. It won't start when I click back on the form.

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Program stops working when the form looses focus

    Can you wrap that code in [ code][ /code] tags please so that we can easily read it?

  7. #7
    Join Date
    Aug 2009
    Posts
    4

    Re: Program stops working when the form looses focus

    Quote Originally Posted by BigEd781 View Post
    Can you wrap that code in [ code][ /code] tags please so that we can easily read it?
    Apologies.. Here goes again

    Thanks all for replying to my thread.
    Brief synopsis of the program:
    It calls a soap function continuously to check for new data:
    Here is the call to the soap function:
    Code:
    namespace eDataFeedClient
    
    
    
    {
    public class GetData
    {
    public collectionActualValueData getmyace(String uid, String pswd, String url)
    {
    // create the web service and point it at the correct URL
    EDataFeedService service = new EDataFeedService();
    service.Url = url;
    // Create the WS-Security token for the SOAP header
    UsernameToken userToken = new UsernameToken(uid,
    pswd,
    PasswordOption.SendPlainText);
    service.RequestSoapContext.Security.Tokens.Add(userToken);
    
    // Create the network credentials and assign
    // them to the service credentials
    NetworkCredential netCredential = new NetworkCredential(uid, pswd);
    Uri uri = new Uri(service.Url);
    ICredentials credentials = netCredential.GetCredential(uri, "Basic");
    service.Credentials = credentials;
    
    // Be sure to set PreAuthenticate to true or else
    // authentication will not be sent.
    service.PreAuthenticate = true;
    
    // this property is set to prevent 505 HTTP Version Not Supported in Tomcat
    ServicePointManager.Expect100Continue = false;
    
    // set this property to signal the server to compress the response
    service.EnableDecompression = true;
    
    // call the web service
    return(service.getAllAce());
    }
    
    public static void Main()
    {
    Application.Run(new frmdisplay());
    }
    
    
    } 
    }
    The above code is in my program.cs
    The code below is in my display.cs
    Code:
    namespace eDataFeedClient{ 
    
    
    
    public partial class frmdisplay : Form
    {
    public frmdisplay()
    {
    InitializeComponent();
    }
    
    
    
    
    
    
    public string AceTime
    {
    get { return this.lblAce_Time_Value.Text; }
    set { this.lblAce_Time_Value.Text = value; }
    
    }
    
    public string AceVal
    {
    get { return this.lblAce_Val_Value.Text; }
    set { this.lblAce_Val_Value.Text = value; }
    
    }
    
    public string TestVal
    {
    set { this.lblTest.Text = value; }
    
    }
    
    private void cmdStartApp_Click(object sender, EventArgs e)
    {
    String OUT_DATE_FORMAT = "HH:mm";
    collectionActualValueData adata;
    String uid = "someuid";
    String pswd = "somepassword";
    String url = "someurl";
    // Create a new GetData Class
    GetData gdata = new GetData();
    for (int i = 0; i < 500; i = i + 1)
    {
    adata = gdata.getmyace(uid, pswd, url);
    foreach (actualValueData node in adata.actualValueData)
    {
    
    this.AceTime = node.timestamp.ToString(OUT_DATE_FORMAT);
    this.AceVal = node.value.ToString();
    this.TestVal = i.ToString();
    }
    this.Refresh();
    Thread.Sleep(1000);
    }
    }
    
    
    
    }
    }
    I can build this and run it. The form shows up and when I click the button it goes and fetches the data. But the moment I click on anything else like say a different window, the data in the form stops refreshing. It won't start when I click back on the form.

  8. #8
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,539

    Re: Program stops working when the form looses focus

    As much as I can see you havn't defined the threading Model like we usually do
    Code:
    
    static class Program {
        /// <summary>
        ///The Main Entypoint of the application.
        /// </summary>
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainMDI());
        }
    }
    But why not really opening a brandnew WinForm Project and copying that lines into it. Shouldn't be to much work and you would get rid of any issues depending on your change from Console to WinForm
    Last edited by JonnyPoet; August 11th, 2009 at 08:24 AM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  9. #9
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Program stops working when the form looses focus

    your application should be built on a thread apartment to interact with os. so you need to use [STAThread] attribute for your main method.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  10. #10
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,539

    Re: Program stops working when the form looses focus

    Quote Originally Posted by toraj58 View Post
    your application should be built on a thread apartment to interact with os. so you need to use [STAThread] attribute for your main method.
    Yep. Exactly what I showed him in my post
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  11. #11
    Join Date
    Aug 2009
    Posts
    4

    Re: Program stops working when the form looses focus

    Quote Originally Posted by JonnyPoet View Post
    As much as I can see you havn't defined the threading Model like we usually do
    Code:
    
    static class Program {
        /// <summary>
        /// Der Haupteinstiegspunkt für die Anwendung.
        /// </summary>
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainMDI());
        }
    }
    But why not really opening a brandnew WinForm Project and copying that lines into it. Shouldn't be to much work and you would get rid of any issues depending on your change from Console to WinForm

    Guru's thanks. This is exactly what I needed. It is working beautifully now !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)