我的程序如下,我用到了委托,为什么程序会在while语句中死锁?使整个界面停滞?
但如果不用委托,直接用线程运行AlarmThreadEvent(),就不会停滞,这是为什么?
因为我要操作窗口,所以得用委托呀,请帮一下,急,在线等;
我的程序如下
private void button1_Click(object sender, EventArgs e)
{
AlarmThread = new Thread(new ThreadStart(AlarmThreadEventA));
AlarmThread.IsBackground = true; } public delegate void AlarmThreadEventdelegate();
private void AlarmThreadEventA()
{
AlarmThreadEventdelegate a = new AlarmThreadEventdelegate(AlarmThreadEvent);
this.BeginInvoke(a, new object[]{ });
}
private void AlarmThreadEvent()
{
while (true)
{
Thread.Sleep(1);
}
}
但如果不用委托,直接用线程运行AlarmThreadEvent(),就不会停滞,这是为什么?
因为我要操作窗口,所以得用委托呀,请帮一下,急,在线等;
我的程序如下
private void button1_Click(object sender, EventArgs e)
{
AlarmThread = new Thread(new ThreadStart(AlarmThreadEventA));
AlarmThread.IsBackground = true; } public delegate void AlarmThreadEventdelegate();
private void AlarmThreadEventA()
{
AlarmThreadEventdelegate a = new AlarmThreadEventdelegate(AlarmThreadEvent);
this.BeginInvoke(a, new object[]{ });
}
private void AlarmThreadEvent()
{
while (true)
{
Thread.Sleep(1);
}
}
private void button1_Click(object sender, EventArgs e)
{
AlarmThread = new Thread(new ThreadStart(AlarmThreadEventA));
AlarmThread.Start() }
否则你 while()一直在运行 CPU就没时间执行别的.你可以考虑在循环里Application.DoEvents();
Imports System
Imports System.ThreadingPublic Class Work <MTAThread> _
Shared Sub Main()
' To start a thread using a shared thread procedure, use
' the class name and method name when you create the
' ParameterizedThreadStart delegate. Visual Basic expands
' the AddressOf expression to the appropriate delegate
' creation syntax:
' New ParameterizedThreadStart(AddressOf Work.DoWork)
'
Dim newThread As New Thread(AddressOf Work.DoWork)
' Use the overload of the Start method that has a
' parameter of type Object. You can create an object that
' contains several pieces of data, or you can pass any
' object or value type. The following code passes the
' integer value 42.
'
newThread.Start(42) ' To start a thread using an instance method for the thread
' procedure, use the instance variable and method name when
' you create the ParameterizedThreadStart delegate. Visual
' Basic expands the AddressOf expression to the appropriate
' delegate creation syntax:
' New ParameterizedThreadStart(AddressOf w.DoMoreWork)
'
Dim w As New Work()
newThread = New Thread(New ParameterizedThreadStart(AddressOf w.DoMoreWork))
'newThread = New Thread(AddressOf w.DoMoreWork)
' Pass an object containing data for the thread.
'
newThread.Start("The answer.")
End Sub
Public Shared Sub DoWork(ByVal data As Object)
Console.WriteLine("Static thread procedure. Data='{0}'", _
data)
End Sub Public Sub DoMoreWork(ByVal data As Object)
Console.WriteLine("Instance thread procedure. Data='{0}'", _
data)
End Sub
End Class' This code example produces the following output (the order
' of the lines might vary):
'
'Static thread procedure. Data='42'
'Instance thread procedure. Data='The answer'
C# 复制代码
using System;
using System.Threading;public class Work
{
public static void Main()
{
// To start a thread using a shared thread procedure, use
// the class name and method name when you create the
// ParameterizedThreadStart delegate.
//
Thread newThread = new Thread(
new ParameterizedThreadStart(Work.DoWork));
// Use the overload of the Start method that has a
// parameter of type Object. You can create an object that
// contains several pieces of data, or you can pass any
// reference type or value type. The following code passes
// the integer value 42.
//
newThread.Start(42); // To start a thread using an instance method for the thread
// procedure, use the instance variable and method name when
// you create the ParameterizedThreadStart delegate.
//
Work w = new Work();
newThread = new Thread(
new ParameterizedThreadStart(w.DoMoreWork));
// Pass an object containing data for the thread.
//
newThread.Start("The answer.");
}
public static void DoWork(object data)
{
Console.WriteLine("Static thread procedure. Data='{0}'",
data);
} public void DoMoreWork(object data)
{
Console.WriteLine("Instance thread procedure. Data='{0}'",
data);
}
}/* This code example produces the following output (the order
of the lines might vary):Static thread procedure. Data='42'
Instance thread procedure. Data='The answer'
*/
while (true)
{
Application.DoEvents();
Thread.Sleep(10);
} 但是我不明白,为什么线程里面Thread.Sleep(10);为什么还会让界面停滞?
哪些朋友实际试一下,帮一下我呀,,就这么点代码
{
Thread.Sleep(1);
}
占用了UI线程,而且一直都不退出,当前会假死啦,Invoke相当于在UI线程里执行,你这个循环不退出,UI线程的其它语句怎么执行。
{
Thread.Sleep(1);
}1太小了,这样这个操作把所有的CPU时间都吃了,让别人吃什么去阿(死机)。
改称1000ms轮训一次把
int count, i = 0;
public void AddData()
{
if (i == count)
i = 0;
this.lblResult.Text = i.ToString() + "/" + count.ToString();
i++;
}
public void UpdataThread()
{
try
{
MethodInvoker mi = new MethodInvoker(this.AddData);
while (true)
{
this.BeginInvoke(mi);
Thread.Sleep(50);
}
}
catch (ThreadInterruptedException)
{
}
finally
{
}
}
public void StartThread()
{
StopThread();
timerThread = new Thread(new ThreadStart(UpdataThread));
timerThread.IsBackground = true;
timerThread.Start();
}
public void StopThread()
{
if (timerThread != null)
{
timerThread.Interrupt();
timerThread = null;
}
}
private void btnStart_Click(object sender, EventArgs e)
{
count = int.Parse(textBox1.Text);
this.StartThread();
}
private void btnStop_Click(object sender, EventArgs e)
{
this.StopThread();
}
}
private void button1_Click(object sender, EventArgs e)
{
AlarmThread = new Thread(new ThreadStart(AlarmThreadEventA));
AlarmThread.IsBackground = true;
AlarmThread.Start();
} private void AlarmThreadEvent()
{
while (true)
{
AlarmThreadEventdelegate a = new AlarmThreadEventdelegate(AlarmThreadEventA);
this.BeginInvoke(a, new object[] { });
Thread.Sleep(30000);
}
} public delegate void AlarmThreadEventdelegate();
private void AlarmThreadEventA()
{
//大量数据处理过程
} 改成这样不会再出现停滞情况,但是按你说的,方法AlarmThreadEventA()还是被调回主线程执行了,对吗?那是不是线程就没起作用了?请帮帮改改,应该怎么样实现比较好?我想实现的效果就是在线程里30S处理一次数据,即执行AlarmThreadEventA()