⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 longprocess.cs

📁 100个非常有趣的C语言UNIX程序实例。可以在乐趣中练习C语言编程。
💻 CS
字号:
using System;
using System.Windows.Forms;
using System.Threading;


namespace WorkerThread
{
	/// <summary>
	/// Class emulates long process which runs in worker thread
	/// and makes synchronous user UI operations.
	/// </summary>
	public class LongProcess
	{
		#region Members

		// Main thread sets this event to stop worker thread:
		ManualResetEvent m_EventStop;

		// Worker thread sets this event when it is stopped:
		ManualResetEvent m_EventStopped;

		// Reference to main form used to make syncronous user interface calls:
		MainForm m_form;

		#endregion

		#region Functions

		public LongProcess(ManualResetEvent eventStop, 
			               ManualResetEvent eventStopped,
			               MainForm form)
		{
			m_EventStop = eventStop;
			m_EventStopped = eventStopped;
			m_form = form;
		}

		// Function runs in worker thread and emulates long process.
		public void Run()
		{
			int i;
			String s;

			for (i = 1; i <= 10; i++)
			{
				// make step
				s = "执行第" + i.ToString() + "步";

				Thread.Sleep(400);

				// Make synchronous call to main form.
				// MainForm.AddString function runs in main thread.
				// To make asynchronous call use BeginInvoke
				m_form.Invoke(m_form.m_DelegateAddString, new Object[] {s});


				// check if thread is cancelled
				if ( m_EventStop.WaitOne(0, true) )
				{
					// clean-up operations may be placed here
					// ...

					// inform main thread that this thread stopped
					m_EventStopped.Set();

					return;
				}
			}

			// Make asynchronous call to main form
			// to inform it that thread finished
			m_form.Invoke(m_form.m_DelegateThreadFinished, null);
		}

		#endregion
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -