473,883 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Make application sleep

I open a file for input. Each line is handled individually. Every time
a line has been handled I wish to wait for ½ second before reading the
next. Am I doing it right or wrong ? (I think it's wrong):
Dim thSleep As New Thread(AddressO f AppSleep)

Sub AppSleep()
Do
Thread.Sleep(50 0)
Loop
End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click

Dim sr As StreamReader = New StreamReader("d :\test.txt")
Dim s As String = ""

Do While sr.Peek() >= 0
s = sr.ReadLine()
Console.WriteLi ne(s)

If s.Substring(0, 1) = "{" Then
s = s.Substring(1, s.Length - 2)
Dim myArray() As String = Split(s, ",", ,
CompareMethod.T ext)

ClickPosition()
AppSleep()

Else
TextInput = s
WriteText()
AppSleep()
End If

Loop

sr.Close()
sr.Dispose()

End Sub

The essense is that for each line either a mouse click og textline i
send to another application. For each click/text I wish for the
application to "absorb" it.

Regards /Snedker
Oct 12 '06 #1
12 2148
Ummmmmmmm ... It's very wrong.

The first time you call AppSleep, your UI thread will go to sleep and never
wake up again because of the Do ... Loop.

"Morten Snedker" <morten_spammen ot_ATdbconsult. dkwrote in message
news:7a******** *************** *********@4ax.c om...
>I open a file for input. Each line is handled individually. Every time
a line has been handled I wish to wait for ½ second before reading the
next. Am I doing it right or wrong ? (I think it's wrong):
Dim thSleep As New Thread(AddressO f AppSleep)

Sub AppSleep()
Do
Thread.Sleep(50 0)
Loop
End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click

Dim sr As StreamReader = New StreamReader("d :\test.txt")
Dim s As String = ""

Do While sr.Peek() >= 0
s = sr.ReadLine()
Console.WriteLi ne(s)

If s.Substring(0, 1) = "{" Then
s = s.Substring(1, s.Length - 2)
Dim myArray() As String = Split(s, ",", ,
CompareMethod.T ext)

ClickPosition()
AppSleep()

Else
TextInput = s
WriteText()
AppSleep()
End If

Loop

sr.Close()
sr.Dispose()

End Sub

The essense is that for each line either a mouse click og textline i
send to another application. For each click/text I wish for the
application to "absorb" it.

Regards /Snedker

Oct 12 '06 #2
Dear Mr. Snedker,

As I see it your sub AppSleep is an eternal loop, because you didn't set
any condition on how to end it. So it will sleep again and again and
again and ...

So, make AppSleep look like this:

Sub AppSleep()
Thread.Sleep(50 0)
End Sub

Apart from that, there is the problem that the sending application
(sender) does not know if the string was processed or not. Depending on
the situation (maybe Windows is swapping or very busy) 500 ms might not
be enough. If you send the next string already, the processing order
might be that the second string is processed first. This might be a
problem if the order of the strings is important.

Perhaps another method might be better (but it would only work if you
have access to the sources of both applications):

Implement a status message into both programs. This could e.g. be done
by creating a status file (or a named pipe).

The receiver creates a status file (and if you like returns a status
message e.g. OK or error message). The sender then checks if the file
exists.

a) If the file exists and you implemented the OK/error message it opens
it and checks if the transaction was OK or an error occurred. If OK,
then the sender deletes the file and sends the next string otherwise
handle the error.

OR

b) If the file exists the sender deletes the file and sends the next string.

Best Regards,

HKSHK
Morten Snedker wrote:
I open a file for input. Each line is handled individually. Every time
a line has been handled I wish to wait for ½ second before reading the
next. Am I doing it right or wrong ? (I think it's wrong):
Dim thSleep As New Thread(AddressO f AppSleep)

Sub AppSleep()
Do
Thread.Sleep(50 0)
Loop
End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click

Dim sr As StreamReader = New StreamReader("d :\test.txt")
Dim s As String = ""

Do While sr.Peek() >= 0
s = sr.ReadLine()
Console.WriteLi ne(s)

If s.Substring(0, 1) = "{" Then
s = s.Substring(1, s.Length - 2)
Dim myArray() As String = Split(s, ",", ,
CompareMethod.T ext)

ClickPosition()
AppSleep()

Else
TextInput = s
WriteText()
AppSleep()
End If

Loop

sr.Close()
sr.Dispose()

End Sub

The essense is that for each line either a mouse click og textline i
send to another application. For each click/text I wish for the
application to "absorb" it.

Regards /Snedker
Oct 12 '06 #3
As the others said, and:

If you're using PostMessage as we recently discussed in your
ClickPosition() , you might want to use SendMessage instead - it waits
until the app processes the message before returning. Might help
reduce/eliminate the need for Sleep and speed things up.

Oct 12 '06 #4
On 12 Oct 2006 03:11:54 -0700, te******@hotmai l.com wrote:
Thanks to all of you guys for your help and efforts. I've given
SendMessage a go, but it still won't do (entirely).

My test.txt contains:
{1221,12}
{50,980}
{70,867}

The idea is to move to each position and perform a click. In my case
the first position should minimize any foremost underlying
application.

Second click hits the Win-XP Start-button.

The third click should click somewhat above the the now unfolded list
above the the Start-button and hit Notepad.

In all three cases the mouse moves to the proper position.

Though, the first click doesn't minimize the underlying application
(that could be any maximized app).

The second click performs well and unfolds the Start-button.

The thirc click doesn't open the Notepad (even at the right position).

Here is my full code:

'--code begin
Imports System.Windows. Forms
Imports System.Threadin g
Imports System.IO
Public Class Form1

Public TextInput As String = ""
Public PosX As Integer, PosY As Integer

Dim thSleep As New Thread(AddressO f ReadFile)

#Region "Get window-name, Declares"
Dim myVal1 As Integer
Dim myVal2 As Integer

Dim intHandle As IntPtr = FindWindow("[Class Name Here]",
vbNullString)
Dim intHandle2 As IntPtr = FindWindow(vbNu llString, "Lommeregne r")

Private Declare Function GetForegroundWi ndow _
Lib "user32" () As IntPtr
Private Declare Function SetForegroundWi ndow _
Lib "user32" (ByVal hWnd As IntPtr) As Boolean
Private Declare Function GetWindowThread ProcessId _
Lib "user32" (ByVal hWnd As IntPtr, _
ByVal lpdwProcessId As IntPtr) As Integer
Private Declare Function AttachThreadInp ut _
Lib "user32" (ByVal idAttach As Integer, _
ByVal idAttachTo As Integer, ByVal fAttach As Boolean _
) As Boolean
Private Declare Function FindWindow _
Lib "user32" Alias "FindWindow A" (ByVal lpClassName As String,
_
ByVal lpWindowName As String) As IntPtr
#End Region

Declare Auto Function WindowFromPoint Lib "user32" (ByVal xPoint
As Integer, ByVal yPoint As Integer) As IntPtr
Declare Function SendMessage Lib "user32" Alias "SendMessag eA" _
(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal
wParam As Integer, ByVal lParam As Integer) As Integer

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
As System.EventArg s) Handles Button1.Click
Me.WindowState = FormWindowState .Minimized
ReadFile()
End Sub
Sub ReadFile()

Dim sr As StreamReader = New StreamReader("d :\test.txt")
Dim s As String = ""

Do While sr.Peek() >= 0
s = sr.ReadLine()
Console.WriteLi ne(s)

If s.Substring(0, 1) = "{" Then
s = s.Substring(1, s.Length - 2)
Dim myArray() As String = Split(s, ",", ,
CompareMethod.T ext)
PosX = myArray(0)
PosY = myArray(1)

ClickPosition()
Thread.Sleep(50 0)
Else
'TextInput = s
'WriteText()
'Thread.Sleep(5 00)
End If
Loop

sr.Close()
sr.Dispose()

End Sub
Sub ClickPosition()
Dim wnd As IntPtr
Dim pt As Point

pt.X = PosX
pt.Y = PosY
Windows.Forms.C ursor.Position = pt

Const WM_ACTIVATEAPP = &H1C
Const WM_LBUTTONUP = &H202 '//LButton up
Const WM_LBUTTONDOWN = &H201 '//LButton down

wnd = WindowFromPoint (PosX, PosY)
Call SendMessage(wnd , WM_ACTIVATEAPP, 0, 0)
Call SendMessage(wnd , WM_LBUTTONDOWN, 0, 0)
Call SendMessage(wnd , WM_LBUTTONUP, 0, 0)
End Sub

Sub WriteText()
SendKeys.SendWa it(TextInput)
End Sub

End Class
'--code end

Regards /Snedker
Oct 12 '06 #5
Morten Snedker wrote:
The third click should click somewhat above the the now unfolded list
above the the Start-button and hit Notepad.
What if the position of Notepad in the start menu changes? What if the
machine the program is running on does not have Notepad in the start
menu?

What are you trying to accomplish? Just starting Notepad (or some
other app?) Why not use the Process class to start notepad directly?

Oct 12 '06 #6
Hi Morten,

This won't really help with your app but I can see a few strange things
going on there..

When you call a method to operate on a variable it's best to send that
variable to the method as a parameter, i.e.

Sub WriteText(Byval iTextInput As String)
SendKeys.SendWa it(iTextInput)
End Sub

That way SendKeys will always send each value. If the value of
TextInput were to change after calling the method it would not act as
expected.

Same goes for your click method, I would create a parameter for the
position to click, but this would be relative to the screen coordinates.
You have to bare in mind that if you are obtaining the handle to a window
and sending the click message to it then X, and Y will be relative to it's
current location, so it might not even be clicking inside of it.

BTW, what are you trying to achieve? Scripting clicking a few buttons I
presume to remove something annoying from the screen? ;-)

Nick.

"Morten Snedker" <morten_spammen ot_ATdbconsult. dkwrote in message
news:dk******** *************** *********@4ax.c om...
On 12 Oct 2006 03:11:54 -0700, te******@hotmai l.com wrote:
Thanks to all of you guys for your help and efforts. I've given
SendMessage a go, but it still won't do (entirely).

My test.txt contains:
{1221,12}
{50,980}
{70,867}

The idea is to move to each position and perform a click. In my case
the first position should minimize any foremost underlying
application.

Second click hits the Win-XP Start-button.

The third click should click somewhat above the the now unfolded list
above the the Start-button and hit Notepad.

In all three cases the mouse moves to the proper position.

Though, the first click doesn't minimize the underlying application
(that could be any maximized app).

The second click performs well and unfolds the Start-button.

The thirc click doesn't open the Notepad (even at the right position).

Here is my full code:

'--code begin
Imports System.Windows. Forms
Imports System.Threadin g
Imports System.IO
Public Class Form1

Public TextInput As String = ""
Public PosX As Integer, PosY As Integer

Dim thSleep As New Thread(AddressO f ReadFile)

#Region "Get window-name, Declares"
Dim myVal1 As Integer
Dim myVal2 As Integer

Dim intHandle As IntPtr = FindWindow("[Class Name Here]",
vbNullString)
Dim intHandle2 As IntPtr = FindWindow(vbNu llString, "Lommeregne r")

Private Declare Function GetForegroundWi ndow _
Lib "user32" () As IntPtr
Private Declare Function SetForegroundWi ndow _
Lib "user32" (ByVal hWnd As IntPtr) As Boolean
Private Declare Function GetWindowThread ProcessId _
Lib "user32" (ByVal hWnd As IntPtr, _
ByVal lpdwProcessId As IntPtr) As Integer
Private Declare Function AttachThreadInp ut _
Lib "user32" (ByVal idAttach As Integer, _
ByVal idAttachTo As Integer, ByVal fAttach As Boolean _
) As Boolean
Private Declare Function FindWindow _
Lib "user32" Alias "FindWindow A" (ByVal lpClassName As String,
_
ByVal lpWindowName As String) As IntPtr
#End Region

Declare Auto Function WindowFromPoint Lib "user32" (ByVal xPoint
As Integer, ByVal yPoint As Integer) As IntPtr
Declare Function SendMessage Lib "user32" Alias "SendMessag eA" _
(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal
wParam As Integer, ByVal lParam As Integer) As Integer

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
As System.EventArg s) Handles Button1.Click
Me.WindowState = FormWindowState .Minimized
ReadFile()
End Sub
Sub ReadFile()

Dim sr As StreamReader = New StreamReader("d :\test.txt")
Dim s As String = ""

Do While sr.Peek() >= 0
s = sr.ReadLine()
Console.WriteLi ne(s)

If s.Substring(0, 1) = "{" Then
s = s.Substring(1, s.Length - 2)
Dim myArray() As String = Split(s, ",", ,
CompareMethod.T ext)
PosX = myArray(0)
PosY = myArray(1)

ClickPosition()
Thread.Sleep(50 0)
Else
'TextInput = s
'WriteText()
'Thread.Sleep(5 00)
End If
Loop

sr.Close()
sr.Dispose()

End Sub
Sub ClickPosition()
Dim wnd As IntPtr
Dim pt As Point

pt.X = PosX
pt.Y = PosY
Windows.Forms.C ursor.Position = pt

Const WM_ACTIVATEAPP = &H1C
Const WM_LBUTTONUP = &H202 '//LButton up
Const WM_LBUTTONDOWN = &H201 '//LButton down

wnd = WindowFromPoint (PosX, PosY)
Call SendMessage(wnd , WM_ACTIVATEAPP, 0, 0)
Call SendMessage(wnd , WM_LBUTTONDOWN, 0, 0)
Call SendMessage(wnd , WM_LBUTTONUP, 0, 0)
End Sub

Sub WriteText()
SendKeys.SendWa it(TextInput)
End Sub

End Class
'--code end

Regards /Snedker

Oct 12 '06 #7
On 12 Oct 2006 06:27:09 -0700, "Chris Dunaway" <du******@gmail .com>
wrote:
>What if the position of Notepad in the start menu changes? What if the
machine the program is running on does not have Notepad in the start
menu?
Not an issue - the described situation is just for test. Just to see
it work.
>What are you trying to accomplish? Just starting Notepad (or some
other app?) Why not use the Process class to start notepad directly?
Notepad is just an example. The end-purpose is to make flexible
navigation in any given program.

The kickstart is my collegue who has a special application to make
complicated calculations. That takes several clicks and input of data.
Then it calculates for 5-6 minutes. That he sometimes does a hundred
times - and it's a kiling job.

So we wish for this application to take care of itself.

Regards /Snedker
Oct 12 '06 #8
On Thu, 12 Oct 2006 14:48:07 +0100, "NickP" <a@a.comwrote :
>Hi Morten,

This won't really help with your app but I can see a few strange things
going on there..

When you call a method to operate on a variable it's best to send that
variable to the method as a parameter, i.e.

Sub WriteText(Byval iTextInput As String)
SendKeys.SendWa it(iTextInput)
End Sub
Initially that's what I had. Just found it more convenient to have
just one variable...but I agree.
Same goes for your click method, I would create a parameter for the
position to click, but this would be relative to the screen coordinates.
You have to bare in mind that if you are obtaining the handle to a window
and sending the click message to it then X, and Y will be relative to it's
current location, so it might not even be clicking inside of it.
Aha. That indeed may be where I'm going wrong. But if I send a
doubleclick to an Excel-shortcut on the desktop, still Excel doesn't
open...?

Thanks for input.

Regards /Snedker
Oct 12 '06 #9
Sigh.

In your thread "How to find my information", you inquired into a means
of simulating a mouse click *without* moving the mouse, which I
provided with a warning that not all controls respond uniformly. You
said you'd follow up in that thread if you had problems.

Instead, you started a new thread, "Trouble with mouse click", where
you posted code where you're *moving* the mouse, then using the
technique I provided for simulating a click *without moving* the mouse,
thus combining the disadvantages of both. There I advised you use the
more conventional, reliable method of simulating clicks if you were
going to move the mouse anyway, and provided code.

When you started this thread I wasn't sure if you'd seen my previous
response, but now that you've posted ClickPosition code, it appears
that you did not; if you had, it would be working already.

Seriously. One problem, one thread, makes things so much easier. :)

This will work just fine on your minimize button, start menu, shortcut,
and whatever else you throw at it:

Const MOUSEEVENTF_LEF TDOWN = &H2
Const MOUSEEVENTF_LEF TUP = &H4
Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Int32, ByVal dX
As Int32, ByVal dy As Int32, ByVal cButtons As Int32, ByVal dwExtraInfo
As Int32)

Sub ClickPosition()
Dim wnd As IntPtr
Dim pt As Point

pt.X = PosX
pt.Y = PosY
Windows.Forms.C ursor.Position = pt

mouse_event(MOU SEEVENTF_LEFTDO WN + MOUSEEVENTF_LEF TUP, 0, 0, 0, 0)
End Sub

Want a double-click? Just issue that mouse_event twice, back to back,
no delay necessary.

Oct 12 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2240
by: Eric Marvets | last post by:
I want to make the main thread in my app sleep while I have a worker do some arbitrary task. The problem is waking the main thread back up. Can anyone make this code sample work? private void button1_Click(object sender, System.EventArgs e) { //save ref to main thread th = Thread.CurrentThread; Thread worker = new Thread(new ThreadStart(GetString));
3
2502
by: Chris Tanger | last post by:
I am creating a class that has a method "Write" that I wish to make threadsafe. The method must block calling threads until the task performed in write is complete. Only 1 thread at a time can perform the task within "Write". 1-10 different threads may call "Write" simultaneously and continuously, some in a greedy manner. That is to say that some of the threads calling "Write" will take all they can get, while other threads may only call...
2
2299
by: Richard Siderits | last post by:
Greetings. I am trying to write a small application for controlling CRYDOM AC and DC switches from the parallel port using pyparallel. The project is described in the latest issue of MAKE magazine Vol.3 pg 86. All of the examples are in C, VB, Linux, Unix but not a thing in Python. Seems like a perfect application for a Python program or even a simple windowed app. Problem is I'm stuck. How, for example, would I format the setData() to...
2
1210
by: Ford Prefect alias Armin | last post by:
Hello I have a Problem of understanding how IIS/ASPX Works..... Why can't to request run at the "same" time ?? I have a Simple ASPX Web Application. It has two Button.
8
109495
by: Roger Solano | last post by:
How can I make my VB .net program sleep or pause for say 5 minutes? Any help would be much appreciated. Roger
17
1622
by: > Adrian | last post by:
I have converted a number of applications to enable them to work together on a network. I have been led to believe that I can do this as follows: FileStream fs = new FileStream(some code); while (!fs.CanRead){allow the processor to handle another thread;} Hoverer, doing some tests I find out that the solution isn't working. Is there a simple way out?
2
5259
by: BLUE | last post by:
I would like to pause an application while the GUI display a Label saying "Logging in...". System.Timers System.Windows.Forms.Timer System.Threading.Timer System.Threading ==Thread.Sleep Which one? In the last case Sleep must be applied to what(Application, this or what)?
11
5189
by: Don | last post by:
I have a WPF application in VB in VSTS 2008 RTM. I am trying to "blink" (momentarily clear) a field of data if the data is reloaded from the database to give the user some visual indication of the LOAD operation. So on the LOAD button I clear the text fields, do mainCanvas.UpdateLayout( ), and the reload the text field from the database. But the text fields are not cleared long enough to see them blink.
4
3369
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, We use Payflow Pro from Verisign(Now it calls paypal Payflow Pro) as a gateway software to process credit card payment. Most of time, Paypal server is OK and we don't have problems for credit card payment. But we do experience some issues sometimes. i.e. Occasionally, after credit card information was submitted to payPal server, we didn't get any response from their server, no error code, it is just an empty string.
0
9944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9796
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11152
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10859
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10420
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7134
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5997
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4620
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3239
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.