请教一个线程访问的问题

likegod 2008-12-16 10:12:50

private void button1_Click(object sender, EventArgs e)
{
Thread td = new Thread(new ThreadStart(_model.Start));
td.Start();
}

public class Model
{
.......
private static System.Windows.Forms.Timer _timer;

public void Start()
{
_timer.Enabled = true;
}

void _timer_Tick(object sender, EventArgs e)
{
_timer.Enabled = false;

IDataProvider data = BizConfig.CreateDataProvider();
data.GetData(_stockCode);

..........
}


现在的问题是,如果_model.Start这个方法不放在线程里,可以触发_timer_Tick事件。
当把_model.Start这个方法放在线程里时,不会触发_timer_Tick事件(打断点根本没反应)

请问这个问题如何解决?谢谢!
...全文
136 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
fkuk2 2009-01-04
  • 打赏
  • 举报
回复
实例化一个线程,然后开启
  • 打赏
  • 举报
回复
mark
much0726 2009-01-04
  • 打赏
  • 举报
回复
在线程中Sleep一个时间段,相当于使用Timer了.
quanhuang98bisha 2009-01-04
  • 打赏
  • 举报
回复
mark
sunnyhuang2008 2009-01-03
  • 打赏
  • 举报
回复
mark
woodboy23 2009-01-03
  • 打赏
  • 举报
回复
留个记号
zlb789 2008-12-16
  • 打赏
  • 举报
回复
up
zgke 2008-12-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 WITPIDAN 的回复:]
System.Threading.Timer,对的,线程里的方法触发不了控件TIMER
[/Quote]
换个TIMER把
GTX280 2008-12-16
  • 打赏
  • 举报
回复
帮顶
zhu4139365 2008-12-16
  • 打赏
  • 举报
回复
up[Quote=引用 2 楼 WITPIDAN 的回复:]
System.Threading.Timer,对的,线程里的方法触发不了控件TIMER
[/Quote]
松花皮蛋 2008-12-16
  • 打赏
  • 举报
回复
System.Threading.Timer,对的,线程里的方法触发不了控件TIMER
heyu1000 2008-12-16
  • 打赏
  • 举报
回复
用System.Threading.Timer
周公 2008-12-16
  • 打赏
  • 举报
回复
一个进程可以创建一个或多个线程以执行与该进程关联的部分程序代码。使用 ThreadStart 委托或 ParameterizedThreadStart 委托指定由线程执行的程序代码。使用 ParameterizedThreadStart 委托可以将数据传递到线程过程。

在线程存在期间,它总是处于由 ThreadState 定义的一个或多个状态中。可以为线程请求由 ThreadPriority 定义的调度优先级,但不能保证操作系统会接受该优先级。

GetHashCode 提供托管线程的标识。在线程的生存期内,无论获取该值的应用程序域如何,它都不会和任何来自其他线程的值冲突。

注意
操作系统 ThreadId 和托管线程没有固定关系,这是因为非托管宿主能控制托管与非托管线程之间的关系。特别是,复杂的宿主可以使用 CLR Hosting API 针对相同的操作系统线程调度很多托管线程,或者在不同的操作系统线程之间移动托管线程。

下面的代码示例说明简单的线程处理功能。

using System;
using System.Threading;

// Simple threading scenario: Start a static method running
// on a second thread.
public class ThreadExample {
// 在线程中执行的方法.
public static void ThreadProc() {
for (int i = 0; i < 10; i++) {
Console.WriteLine("ThreadProc: {0}", i);
// Yield the rest of the time slice.
Thread.Sleep(0);
}
}

public static void Main() {
Console.WriteLine("Main thread: Start a second thread.");
//实例化一个线程,
Thread t = new Thread(new ThreadStart(ThreadProc));//ThreadProc是在线程中要执行的方法
//启动线程.
t.Start();
//Thread.Sleep(0);

for (int i = 0; i < 4; i++) {
Console.WriteLine("Main thread: Do some work.");
Thread.Sleep(0);
}

Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
t.Join();
Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.");
Console.ReadLine();
}
}
周公 2008-12-16
  • 打赏
  • 举报
回复
Timer 用于以用户定义的事件间隔触发事件。Windows 计时器是为单线程环境设计的,其中,UI 线程用于执行处理。它要求用户代码有一个可用的 UI 消息泵,而且总是在同一个线程中操作,或者将调用封送到另一个线程。

使用此计时器时,请使用控件的 Tick 事件执行轮询操作,或在指定的时间内显示启动画面。每当 Enabled 属性设置为 true 且 Interval 属性大于 0 时,将引发 Tick 事件,引发的时间间隔基于 Interval 属性设置。

此类提供用于设置时间间隔以及启动和停止计时器的方法。

注意
Windows 窗体 Timer 组件是单线程组件,精度限定为 55 毫秒。如果您需要更高精度的多线程计时器,请使用 System.Timers 命名空间中的 Timer 类。

下面的示例实现简单的间隔计时器,该计时器每五秒钟发一次警报。当发生警报时,MessageBox 显示该警报已激活的次数合计并询问用户计时器是否应继续运行。

public class Class1 {
static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
static int alarmCounter = 1;
static bool exitFlag = false;

// Timer执行的方法.
private static void TimerEventProcessor(Object myObject,
EventArgs myEventArgs) {
myTimer.Stop();

// Displays a message box asking whether to continue running the timer.
if(MessageBox.Show("Continue running?", "Count is: " + alarmCounter,
MessageBoxButtons.YesNo) == DialogResult.Yes) {
// Restarts the timer and increments the counter.
alarmCounter +=1;
myTimer.Enabled = true;
}
else {
// Stops the timer.
exitFlag = true;
}
}

public static int Main() {
/* Adds the event and the event handler for the method that will
process the timer event to the timer. */
myTimer.Tick += new EventHandler(TimerEventProcessor);

// 设置5秒钟执行一次.
myTimer.Interval = 5000;
myTimer.Start();

// Runs the timer, and raises the event.
while(exitFlag == false) {
// Processes all the events in the queue.
Application.DoEvents();
}
return 0;
}
}

周公 2008-12-16
  • 打赏
  • 举报
回复
这是干嘛呀?又是Thread又是Timer的,用其中一个就行了。

111,130

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧