窗体中放置一个标签,formload中开启一个线程使得标签移动,使用了委托。运行时,主线程失去响应,整个进程占用了很多CPU,窗体的别的操作都不响应
解决方案 »
- 如何填写串口"写入"代码?
- 一个关于线程挂起的问题
- C#客户端,如何判断webserver服务是否启动
- 站点localhost可以访问,而127.0.0.1却无法访问?
- visual studio .net 安装不上了
- 郁闷啊,如何设置IDE自动显示鼠标所指向的类/变量/方法的智能快速信息啊
- 笨人正在学做Controls,down了一个源代码。其中一段不明白。会做自定义控件的请进(windows)
- 关于安装后程序的路径问题(WINFORM的)!
- Graphics.MeasureString无法处理字体的Style
- C#给短信猫发送AT指令,短信猫回复异常
- DataGridViewCheckBoxColumn列怎么让checkbox禁用掉啊
- C#中如何在数字中插入逗号,如130000,变成130,000
Dim WithEvents T2 As Timer Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If T1 IsNot Nothing Then
T1.Stop()
T1.Dispose()
End If If T2 IsNot Nothing Then
T2.Stop()
T2.Dispose()
End If
End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
T1 = New Timer
T1.Interval = 200
T1.Start() T2 = New Timer
T2.Interval = 200
T2.Start()
End Sub Private Sub T1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles T1.Tick
If Label1.Left + Label1.Width > Me.Width Then
Label1.Left = 0
Else
Label1.Left += 10
End If
End Sub Private Sub T2_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles T2.Tick
If Label2.Left > 0 Then
Label2.Left -= 10
Else
Label2.Left = Me.Width
End If
End SubEnd Class
{
if (lblMsg.Left >= this.Width)
{
lblMsg.Left = 0;
}
lblMsg.Left += 1;
Thread.Sleep(100);
}
private void MoveMsg()
{
startMove start = delegate()
{
if (lblMsg.InvokeRequired)
{
lblMsg.Invoke(new MethodInvoker(MoveMsg));
}
else
{
while (true)
{
if (lblMsg.Left >= this.Width)
{
lblMsg.Left = 0;
}
lblMsg.Left += 1;
Thread.Sleep(100);
}
}
};
lblMsg.Invoke(start);
}
或则用.waitone这个方法.
private void MoveMsg()
{
startMove start = delegate()
{
if (lblMsg.InvokeRequired)
{
lblMsg.Invoke(new MethodInvoker(MoveMsg));
}
else
{
if (lblMsg.Left >= this.Width)
{
lblMsg.Left = 0;
}
lblMsg.Left += 1;
Thread.Sleep(100);
}
}
};
while(true)
{
lblMsg.Invoke(start);
}
}
Private Delegate Sub Run2(ByVal Reset As Boolean) Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
t1.Abort()
t2.Abort()
End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
t1 = New Threading.Thread(AddressOf Sub1) : t1.Start()
t2 = New Threading.Thread(AddressOf Sub2) : t2.Start()
End Sub Private Sub Move1(ByVal Reset As Boolean)
If Reset Then Label1.Left = 0 Else Label1.Left += 1
End Sub Private Sub Move2(ByVal Reset As Boolean)
If Reset Then Label2.Left = Me.Width Else Label2.Left -= 1
End Sub Private Sub Sub1()
Do
If Label1.Left < Me.Width Then
Me.Invoke(New Run1(AddressOf Move1), False)
Else
Me.Invoke(New Run1(AddressOf Move1), True)
End If
Threading.Thread.Sleep(10)
Loop
End Sub Private Sub Sub2()
Do
If Label2.Left > 0 Then
Me.Invoke(New Run2(AddressOf Move2), False)
Else
Me.Invoke(New Run2(AddressOf Move2), True)
End If
Threading.Thread.Sleep(10)
Loop
End SubEnd Class
但是调试代码的时候发现标签的left值变化是正确的,执行时移出边界后却不出现了
头疼中……
照你的意思是不是这样,在move方法中判断标签是否超过了边界Public Class Form1 Dim t1, t2 As Threading.Thread Private Delegate Sub Run1()
Private Delegate Sub Run2()
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
t1.Abort()
t2.Abort()
End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
t1 = New Threading.Thread(AddressOf Sub1) : t1.Start()
t2 = New Threading.Thread(AddressOf Sub2) : t2.Start()
End Sub Private Sub Move1()
If Label1.Left < Me.Width Then
Label1.Left += 1
Else
Label1.Left = 0
End If
End Sub Private Sub Move2()
If Label2.Left > 0 Then
Label2.Left -= 1
Else
Label2.Left = Me.Width
End If
End Sub Private Sub Sub1()
Do
Me.Invoke(New Run1(AddressOf Move1))
Threading.Thread.Sleep(10)
Loop
End Sub Private Sub Sub2()
Do
Me.Invoke(New Run2(AddressOf Move2))
Threading.Thread.Sleep(10)
Loop
End Sub
End Class我试了试,在vb中依然没有问题。可以拖动窗体,可以点击按钮。
{
public delegate void run1();
public delegate void run2(); Thread threadCn;
Thread threadEn;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{ } private void moveCn()
{
while (true)
{
this.Invoke(new run1(startCn));
}
} private void startCn()
{
//while (true)
//{
if (lblCn.Left >= this.Width)
{
lblCn.Left = 0;
}
lblCn.Left += 50;
Thread.Sleep(50);
//}
} private void moveEn()
{
while (true)
{
this.Invoke(new run2(startEn));
}
} private void startEn()
{
//while (true)
//{
if (lblEn.Left <= -lblEn.Width)
{
lblEn.Left = this.Width;
}
lblEn.Left -= 50;
Thread.Sleep(50);
//}
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
threadCn.Abort();
threadEn.Abort();
} private void button1_Click(object sender, EventArgs e)
{
threadCn = new Thread(new ThreadStart(moveCn));
threadCn.Start();
threadEn = new Thread(new ThreadStart(moveEn));
threadEn.Start();
}
}
我给你放到while循环里了。using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace WindowsApplication2
{
public partial class Form1 : Form
{
public delegate void run1();
public delegate void run2(); Thread threadCn;
Thread threadEn;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void moveCn()
{ while (true)
{
this.Invoke(new run1(startCn));
Thread.Sleep(50);
}
} private void startCn()
{
//while (true)
//{
if (lblCn.Left >= this.Width)
{
lblCn.Left = 0;
}
lblCn.Left += 50;
//}
} private void moveEn()
{ while (true)
{
this.Invoke(new run2(startEn));
Thread.Sleep(50);
}
} private void startEn()
{
//while (true)
//{
if (lblEn.Left <= -lblEn.Width)
{
lblEn.Left = this.Width;
}
lblEn.Left -= 50;
//}
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
threadCn.Abort();
threadEn.Abort();
} private void button1_Click(object sender, EventArgs e)
{
threadCn = new Thread(new ThreadStart(moveCn));
threadCn.Start();
threadEn = new Thread(new ThreadStart(moveEn)); threadEn.Start();
}
}
}