SetForegroundWindow in Visual C# Express

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris242

    SetForegroundWindow in Visual C# Express

    Is it possible to use the syntax SetForegroundWi ndow in Visual C# Express? I
    have tried an have had no luck. I'm also having a ruff time with DLLImport
    as well. I think this may have something to do with my issue with
    SetForegroundWi ndow. Thanks
  • Mattias Sjögren

    #2
    Re: SetForegroundWi ndow in Visual C# Express

    [color=blue]
    >Is it possible to use the syntax SetForegroundWi ndow in Visual C# Express?[/color]

    Sure, as long as you declare the function first.

    [color=blue]
    >I have tried an have had no luck.[/color]

    Can you post the code you tried with?

    [color=blue]
    >I'm also having a ruff time with DLLImport as well.[/color]

    www.pinvoke.net is a good place to look for DllImport method
    declarations.


    Mattias

    --
    Mattias Sjögren [C# MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • Chris242

      #3
      Re: SetForegroundWi ndow in Visual C# Express

      Actually I figured that part out now my code is freezing. I'm sure it is
      some newbie error but coud you take a look at my code please?

      using System;
      using System.Collecti ons.Generic;
      using System.Componen tModel;
      using System.Data;
      using System.Drawing;
      using System.Text;
      using System.Windows. Forms;
      using System.Runtime. InteropServices ;

      namespace WindowsApplicat ion1
      {
      public partial class Form1 : Form
      {

      [DllImport("kern el32.dll")]
      static extern void Sleep(uint dwMilliseconds) ;

      public Form1()
      {
      InitializeCompo nent();

      }

      private void button1_Click(o bject sender, EventArgs e)
      {

      MessageBox.Show ("You have five seconds to get into the
      game!");
      Sleep(5000);
      string x = textBox2.Text;
      string y = textBox3.Text;
      uint a = 0;
      int b = 0;
      int z = 0;
      try
      {
      a = Convert.ToUInt3 2(x);
      b = Convert.ToInt32 (y);
      }
      catch
      {

      }
      while (z < b)
      {

      SendKeys.Send(t extBox1.Text);
      z = z++;
      Sleep(a);
      }
      }
      }
      }

      Comment

      • Mattias Sjögren

        #4
        Re: SetForegroundWi ndow in Visual C# Express

        >Actually I figured that part out now my code is freezing. I'm sure it is[color=blue]
        >some newbie error but coud you take a look at my code please?[/color]

        Well you're calling Sleep, the whole point of that is to sleep or
        freeze the thread. You should never Sleep a UI thread (and if you
        should sleep at all, call System.Threadin g.Thread.Sleep rather than
        using PInvoke).


        Mattias

        --
        Mattias Sjögren [C# MVP] mattias @ mvps.org
        http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
        Please reply only to the newsgroup.

        Comment

        • Chris242

          #5
          Re: SetForegroundWi ndow in Visual C# Express

          Okay, I totaly understand what you mean by NOT sleeping at the UI Level. My
          only thing is I need to send that key at that time. Is there any other way
          to do this? Get rid of sleep and make it more efficent?

          using System;
          using System.Collecti ons.Generic;
          using System.Componen tModel;
          using System.Data;
          using System.Drawing;
          using System.Text;
          using System.Windows. Forms;
          using System.Runtime. InteropServices ;

          namespace WindowsApplicat ion1
          {
          public partial class Form1 : Form
          {

          public Form1()
          {
          InitializeCompo nent();

          }

          private void button1_Click(o bject sender, EventArgs e)
          {

          MessageBox.Show ("You have five seconds to get into the
          game!");
          System.Threadin g.Thread.Sleep( 5000);
          int z = 0;
          while (z++ < Convert.ToInt32 (textBox3.Text) )
          {
          SendKeys.SendWa it(textBox1.Tex t);

          System.Threadin g.Thread.Sleep( Convert.ToInt32 (textBox2.Text) );
          }
          }
          }
          }

          "Mattias Sjögren" wrote:
          [color=blue]
          > Well you're calling Sleep, the whole point of that is to sleep or
          > freeze the thread. You should never Sleep a UI thread (and if you
          > should sleep at all, call System.Threadin g.Thread.Sleep rather than
          > using PInvoke).
          >
          >
          > Mattias
          >
          > --
          > Mattias Sjögren [C# MVP] mattias @ mvps.org
          > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
          > Please reply only to the newsgroup.
          >[/color]

          Comment

          • Chris242

            #6
            Re: SetForegroundWi ndow in Visual C# Express

            backgroundWorke r for the win!

            "Mattias Sjögren" wrote:
            [color=blue][color=green]
            > >Actually I figured that part out now my code is freezing. I'm sure it is
            > >some newbie error but coud you take a look at my code please?[/color]
            >
            > Well you're calling Sleep, the whole point of that is to sleep or
            > freeze the thread. You should never Sleep a UI thread (and if you
            > should sleep at all, call System.Threadin g.Thread.Sleep rather than
            > using PInvoke).
            >
            >
            > Mattias
            >
            > --
            > Mattias Sjögren [C# MVP] mattias @ mvps.org
            > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
            > Please reply only to the newsgroup.
            >[/color]

            Comment

            • Mattias Sjögren

              #7
              Re: SetForegroundWi ndow in Visual C# Express

              [color=blue]
              >Okay, I totaly understand what you mean by NOT sleeping at the UI Level. My
              >only thing is I need to send that key at that time. Is there any other way
              >to do this? Get rid of sleep and make it more efficent?[/color]

              How about using a Timer (the System.Windows. Forms kind)?


              Mattias

              --
              Mattias Sjögren [C# MVP] mattias @ mvps.org
              http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
              Please reply only to the newsgroup.

              Comment

              Working...