当前位置:编程学习 > C#/ASP.NET >>

Timer刷新问题

功能:
用定时器,每隔多少秒刷新path下文件内容到Textbox
VS2010的Windows Froms Application
这是在button1_Click中的方法
 			 //调用刷新方法来定时分析数据
 refrushClass^ re = gcnew refrushClass;
 path = "c:\\SceneServer.log5696";
 re->Main(path);

refrushClass类(与public ref class Form1 : public System::Windows::Forms::Form在同一源码中):

public ref class refrushClass
{
private:
   static System::Windows::Forms::Timer^ myTimer = gcnew System::Windows::Forms::Timer;
   static int alarmCounter = 1;
   static String^ pathRefrush;
   static String^ strTotal;
   // This is the method to run when the timer is raised.
   static void TimerEventProcessor( Object^ /*myObject*/, EventArgs^ /*myEventArgs*/ )
   {
  myTimer->Stop();
  myTimer->Enabled = true;
  //读取path文件内容
  StreamReader^ sr = File::OpenText(pathRefrush);
  try
  {
  //临时存放每次读取到的文件
  String^ s = "";
  while (s = sr->ReadLine())
  {
  strTotal += s;
  }
  }
  finally
  {
  if (sr)
  {
  //释放缓存文件
  delete (IDisposable^)(sr);
  }
  }
  /*不知道怎么把strTotal传递到指定的textbox中,textbox无法直接在此引用*/
   }


public:
   static void Main(String^ path)
   {
   pathRefrush = path;
  /* Adds the event and the event handler for the method that will 
process the timer event to the timer. */
  myTimer->Tick += gcnew EventHandler( TimerEventProcessor );

  // Sets the timer interval to 5 seconds.
  myTimer->Interval = 5000;
  myTimer->Start();

  // 调用TimerEventProcessor()方法
  while(true)
  {
  Application::DoEvents();
  }
   }
};

设计思路:
通过TimerEventProcessor方法来定时读取Path中的内容,但不知道如何传递到textbox控件中 --------------------编程问答-------------------- textbox 怎么定义的。。。 --------------------编程问答-------------------- 理论上是在哪都能引用你的textbox,除非出于设计考虑,你不想再那里用 --------------------编程问答-------------------- 这是托管C++吗? 和C#差不多 --------------------编程问答-------------------- --------------------编程问答-------------------- refrushClass类(与public ref class Form1 : public System::Windows::Forms::Form在同一源码中):所以无法访问Form1的textbox
引用 2 楼 inzaghi97 的回复:
理论上是在哪都能引用你的textbox,除非出于设计考虑,你不想再那里用
--------------------编程问答-------------------- 在Form1里定义的textbox
引用 1 楼 inzaghi97 的回复:
textbox 怎么定义的。。。
--------------------编程问答-------------------- 1, 把你的textbox的访问修饰符扩大一下,到你在此能访问
2, 导入windows的SendMessage函数,向textBox发送消息去修改 --------------------编程问答-------------------- 可以使用SetWindowText这个API函数,无论在哪里,你只需要知道句柄就可以设置。 --------------------编程问答-------------------- 另外的线程更新界面,有个textbox1.invoke这样的方法可以使用。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,