让应用程序睡眠 [英] Make application sleep

查看:85
本文介绍了让应用程序睡眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打开一个文件进行输入。每条线都是单独处理的。每次

一条线已被处理,我希望等待?在阅读

之前的第二个。我这样做是对还是错? (我认为这是错的):

Dim thSleep As New Thread(AddressOf AppSleep)


Sub AppSleep()



Thread.Sleep(500)

循环

结束子


私人子按钮1_点击(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理Button1.Click


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

Dim s As String =""


Do sr.Peek()> = 0

s = sr.ReadLine()

Console.WriteLine(s)


如果s.Substring(0,1)=" {"然后

s = s.Substring(1,s.Length - 2)

Dim myArray()As String = Split(s,",",,

CompareMethod.Text)


ClickPosition()

AppSleep()


Else

TextInput = s

WriteText()

AppSleep()

结束如果


循环


sr.Close()

sr.Dispose()


End Sub


对于每一行,只需鼠标单击og textline i

即可发送到另一个应用程序。对于每个点击/文本,我希望

应用程序吸收它。


问候/ Snedker

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(AddressOf AppSleep)

Sub AppSleep()
Do
Thread.Sleep(500)
Loop
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) 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.WriteLine(s)

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

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

推荐答案

Ummmmmmmm ......这是非常错的。


第一次调用AppSleep时,你的UI线程将进入休眠状态,因为Do ... Loop而不会再次唤醒



" Morten Snedker" < morten_spammenot_ATdbconsult.dkwrote in message

news:7a ******************************** @ 4ax.com ...
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_spammenot_ATdbconsult.dkwrote in message
news:7a********************************@4ax.com...

>我打开一个文件进行输入。每条线都是单独处理的。每次

一条线已被处理,我希望等待?在阅读

之前的第二个。我这样做是对还是错? (我认为这是错的):


Dim thSleep As New Thread(AddressOf AppSleep)


Sub AppSleep()



Thread.Sleep(500)

循环

结束子


Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理Button1.Click


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

Dim s As String =""


Do sr.Peek()> = 0

s = sr.ReadLine()

Console.WriteLine(s)


如果s.Substring(0,1 )=" {"然后

s = s.Substring(1,s.Length - 2)

Dim myArray()As String = Split(s,",",,

CompareMethod.Text)


ClickPosition()

AppSleep()


Else

TextInput = s

WriteText()

AppSleep()

结束如果


循环


sr.Close()

sr.Dispose()


End Sub


对于每一行,只需鼠标单击og textline i

即可发送到另一个应用程序。对于每个点击/文本,我希望

应用程序吸收它。


问候/ Snedker
>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(AddressOf AppSleep)

Sub AppSleep()
Do
Thread.Sleep(500)
Loop
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) 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.WriteLine(s)

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

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



亲爱的Snedker先生,


我看到你的子AppSleep是一个永恒的循环,因为你没有设置

如何结束它的任何条件。所以它会一次又一次地睡觉并且再次点击
...


所以,让AppSleep看起来像这样:


Sub AppSleep()

Thread.Sleep(500)

结束子


除此之外,还有发送应用程序

(发送者)不知道字符串是否被处理的问题。取决于

情况(可能是Windows交换或非常繁忙)500毫秒可能不会

就足够了。如果您已经发送了下一个字符串,处理顺序

可能是第一个处理第二个字符串。如果字符串的顺序很重要,这可能是一个

问题。


也许另一种方法可能会更好(但只有你这样做才有效) />
可以访问这两个应用程序的来源):


在两个程序中实现状态消息。这可以是例如通过创建状态文件(或命名管道)来完成




接收者创建一个状态文件(如果你喜欢,则返回状态

消息,例如OK或错误消息)。发件人然后检查文件

是否存在。


a)如果文件存在并且您实现了OK /错误消息它将打开

it并检查交易是否正常或是否发生错误。如果确定,

然后发件人删除文件并发送下一个字符串否则

处理错误。


OR


b)如果文件存在,发件人删除文件并发送下一个字符串。


最好的问候,


HKSHK

Morten Snedker写道:
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(500)
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:

我打开一个文件进行输入。每条线都是单独处理的。每次

一条线已被处理,我希望等待?在阅读

之前的第二个。我这样做是对还是错? (我认为这是错的):


Dim thSleep As New Thread(AddressOf AppSleep)


Sub AppSleep()



Thread.Sleep(500)

循环

结束子


Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理Button1.Click


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

Dim s As String =""


Do sr.Peek()> = 0

s = sr.ReadLine()

Console.WriteLine(s)


如果s.Substring(0,1 )=" {"然后

s = s.Substring(1,s.Length - 2)

Dim myArray()As String = Split(s,",",,

CompareMethod.Text)


ClickPosition()

AppSleep()


Else

TextInput = s

WriteText()

AppSleep()

结束如果


循环


sr.Close()

sr.Dispose()


End Sub


对于每一行,只需鼠标单击og textline i

即可发送到另一个应用程序。对于每个点击/文本,我希望

应用程序吸收它。


问候/ Snedker
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(AddressOf AppSleep)

Sub AppSleep()
Do
Thread.Sleep(500)
Loop
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) 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.WriteLine(s)

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

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


正如其他人所说,并且:

如果您正在使用我们最近在您的

ClickPosition()中讨论的PostMessage,您可能希望使用SendMessage - 它等待

直到app在返回之前处理消息。可能会帮助

减少/消除睡眠需求并加快速度。

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.


这篇关于让应用程序睡眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆