本文整理汇总了C#中ParameterizedThreadStart类的典型用法代码示例。如果您正苦于以下问题:C# ParameterizedThreadStart类的具体用法?C# ParameterizedThreadStart怎么用?C# ParameterizedThreadStart使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ParameterizedThreadStart类属于命名空间,在下文中一共展示了ParameterizedThreadStart类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExecAsync
public static Thread ExecAsync(ParameterizedThreadStart start, object state)
{
Thread _th = new Thread(start);
_th.IsBackground = true;
_th.Start(state);
return _th;
}
示例2: CreateThread
/// <summary>
/// Creates a thread.
/// </summary>
/// <param name="name">The name of the thread.</param>
/// <param name="proc">The procedure to run in the thread.</param>
/// <returns>
/// The thread
/// </returns>
public Thread CreateThread(string name, ParameterizedThreadStart proc)
{
var t = new Thread(proc);
t.Name = name;
return t;
}
示例3: _StartACountingThread
private static void _StartACountingThread(string threadName)
{
var parameterizedThreadStart = new ParameterizedThreadStart(_SharedCount);
var threadOne = new Thread(parameterizedThreadStart);
threadOne.IsBackground = false;
threadOne.Start(threadName);
}
示例4: MultipleThreads
public void MultipleThreads()
{
GenericListener myListener = new GenericListener();
ManualResetEvent ev = new ManualResetEvent(false);
ArrayList threads = new ArrayList();
System.Diagnostics.Trace.Listeners.Add(myListener);
for (int i = 0; i < 20; i++)
{
ParameterizedThreadStart ts = new ParameterizedThreadStart(MultipleThreadsWorker);
Thread t = new Thread(ts);
threads.Add(t);
t.Start(ev);
}
// now let the threads go
ev.Set();
// wait for the threads to end
int x = 0;
while (x < threads.Count)
{
while ((threads[x] as Thread).IsAlive)
Thread.Sleep(50);
x++;
}
}
示例5: PasswordDismisser
public PasswordDismisser(string password)
{
ParameterizedThreadStart workerStart = new ParameterizedThreadStart(DismissPasswordDialog);
Thread WorkerThread = new Thread(workerStart);
WorkerThread.Name = MethodBase.GetCurrentMethod().DeclaringType.Name + "." + MethodBase.GetCurrentMethod().Name;
WorkerThread.Start(password);
}
示例6: btn_start_Click
private void btn_start_Click(object sender, EventArgs e)
{
//线程更新窗体内容
_thread = new Thread(new ThreadStart(set_txt));
_thread.Start();
//线程传递参数
ParameterizedThreadStart start = new ParameterizedThreadStart(set_txt);
Thread thread = new Thread(start);
object obj = "From Thread With Pareameter:Hello ParameterizedThreadStart!!! ";
thread.Start(obj);
Thread thread2 = new Thread(() => set_txt("From Thread With Lambda:Hello Lam!!!"));
thread2.Start();
ThreadPool.SetMaxThreads(100, 50);
ThreadPool.QueueUserWorkItem(set_txt, "From Thread With ThreadPool:Hello ThreadPool!!!");
//传递BsonDocument
BsonDocument doc_input = new BsonDocument();
doc_input.Add("sleep", 700);
doc_input.Add("txt", "From BsonDocument!!!");
ParameterizedThreadStart start3 = new ParameterizedThreadStart(set_txt_with_doc);
Thread thread3 = new Thread(start3);
object obj3 = (object)doc_input;
thread3.Start(obj3);
//异步调用
D_Add handler = new D_Add(f_add);
sb.AppendLine("Start Add!!");
IAsyncResult result = handler.BeginInvoke(1, 2, new AsyncCallback(f_complete), "AsycState:OK");
sb.AppendLine("Start do other work!!");
this.txt_result.Text = sb.ToString();
}
示例7: WaysToStartThreads
public static void WaysToStartThreads()
{
// 1
// Passing the name of the method name to be executed in the ctor directly without using ThreadStart
var thread1 = new Thread(DoSomeWork);
thread1.Start();
// 2
// Passing the ThreadStart delegate which points to a method name to be executed
var threadStart = new ThreadStart(DoSomeWork);
var thread2 = new Thread(threadStart);
thread2.Start();
// 3
// Passing the ParametrizedThreadStart delegate which points to a method to be executed
var parametrizedThreadStart = new ParameterizedThreadStart(DoSomeWorkWithParameter);
var thread3 = new Thread(parametrizedThreadStart);
thread3.Start(2);
// 4
// Passing a Lambda expression in the Thread class constructor and subsequently calling the Start method
var thread4 = new Thread(() =>
{
int x = 5;
for (int i = 0; i < x; i++)
{
Console.WriteLine(i);
}
});
thread4.Start();
// 5
// Leveraging ThreadPools, call ThreadPool.QueueUserWorkItem passing in the method name to be executed
ThreadPool.QueueUserWorkItem(DoSomeWorkWithParameter);
ThreadPool.QueueUserWorkItem(DoSomeWorkWithParameter, 4);
// 6
// Using TPL (Task Parallel Library). Create a Task<T>, where T is the return type of the method to be executed.
Task<string> task = Task.Factory.StartNew<string>(DoSomeStringWork);
var result = task.Result;
// 7
// Using Asynchronous Delegates
Func<string, string> work = DoSomeStringWork;
IAsyncResult res = work.BeginInvoke("Hello", null, null);
string result1 = work.EndInvoke(res);
////TODO: Explicit use of Thread class
//Threadpool
//Task Parallel Library
//Action class with lambda functions
//BeginInvoke
//BackgroundWorker
}
示例8: WatchConnecting
/// <summary>
/// 连接客户端
/// </summary>
private void WatchConnecting()
{
while (true)//持续不断的监听客户端的请求
{
//开始监听 客户端连接请求,注意:Accept方法,会阻断当前的线程
Socket connection = socketWatch.Accept();
if (connection.Connected && !dict.ContainsKey(connection.RemoteEndPoint.ToString().Substring(0, connection.RemoteEndPoint.ToString().IndexOf(":"))))
{
//向列表控件中添加一个客户端的Ip和端口,作为发送时客户的唯一标识
// listbOnline.Items.Add(connection.RemoteEndPoint.ToString());
//将与客户端通信的套接字对象connection添加到键值对集合中,并以客户端Ip做为健
//list.Items.Add("IP地址", connection.RemoteEndPoint.ToString());
string[] str = new string[]{
"客户端在线",
connection.RemoteEndPoint.ToString().Substring(0, connection.RemoteEndPoint.ToString().IndexOf(":"))
};
uishow.ShwMsgforView(list, str);
dict.Add( connection.RemoteEndPoint.ToString().Substring(0, connection.RemoteEndPoint.ToString().IndexOf(":")), connection);
//创建通信线程
ParameterizedThreadStart pts = new ParameterizedThreadStart(RecMsg);
Thread thradRecMsg = new Thread(pts);
thradRecMsg.IsBackground = true;
thradRecMsg.Start(connection);
}
}
}
示例9: ShowHTML
/// <summary>
/// creates a new browser instance and displays a given html code
/// </summary>
/// <param name="html">the html code to display</param>
public static void ShowHTML(string html)
{
ParameterizedThreadStart threadStart = new ParameterizedThreadStart(CreateBrowserAndShowIt);
Thread thread = new Thread(threadStart);
thread.SetApartmentState(ApartmentState.STA);
thread.Start(html);
}
示例10: recordToolStripMenuItem_Click
private void recordToolStripMenuItem_Click(object sender, EventArgs e)
{
Select s =r== null?new Select():new Select(r);
recordToolStripMenuItem.Enabled = false;
if (s.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
{
recordToolStripMenuItem.Enabled = true;
return;
}
r = s.recData;
if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
r.path = saveFileDialog1.FileName;
}
else
{
recordToolStripMenuItem.Enabled = true;
return;
}
recordToolStripMenuItem.Enabled = true;
ParameterizedThreadStart pts = null;
recorder = RecorderFactory.CreateRecorder(Path.GetExtension(r.path).ToLower());
pts = new ParameterizedThreadStart(recorder.Record);
Thread t = new Thread(pts);
t.Start(r);
recordToolStripMenuItem.Enabled = false;
stopToolStripMenuItem.Enabled = true;
pauseToolStripMenuItem.Enabled = true;
}
示例11: Run
public static void Run(ParameterizedThreadStart whatToRun, object param)
{
InheritedContextThreadRunner runner = new InheritedContextThreadRunner(whatToRun);
Thread thread = new Thread(new ParameterizedThreadStart(runner.Run));
thread.IsBackground = true;
thread.Start(param);
}
示例12: chkdsk_Click
private void chkdsk_Click(object sender, EventArgs e)
{
//docmd("chkdsk.exe", disk + ":");
ParameterizedThreadStart p = new ParameterizedThreadStart(docmd2);
IAsyncResult i=p.BeginInvoke(new string[] { "chkdsk", disk + ":" }, null, null);
//p.EndInvoke(i);
}
示例13: Add
/// <summary>
/// Adds the specified name.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="threadSt">The thread st.</param>
/// <param name="mLine">The m line.</param>
public void Add(string name, ParameterizedThreadStart threadSt, Graphics mLine)
{
Thread trd = new Thread(threadSt);
trd.Name = name;
threads.Add(trd);
trd.Start(mLine);
}
示例14: Invoke
public void Invoke(ParameterizedThreadStart func)
{
Thread thread = new Thread(func);
thread.IsBackground = true;
thread.Start();
threadList.Add(thread);
}
示例15: Main
static void Main()
{
int[] inputValues = new int[2];
Program p = new Program();
Calculator c = new Calculator();
// Get first numeric value.
inputValues[0] = p.GetNumericValue();
// Get second numeric value.
inputValues[1] = p.GetNumericValue();
// TODO: Create an instance of the ParameterizedThreadStart delegate
// passing in the Add method of the Calculator class.
ParameterizedThreadStart parameterizedThreadStart = new ParameterizedThreadStart(c.Add);
// TODO: Declare and create an instance of the secondary thread
// passing in the delegate instance.
Thread thread = new Thread(parameterizedThreadStart);
//Thread thread = new Thread(c.Add);
//TODO: Start the secondary thread passing in the object data.
thread.Start(inputValues);
System.Threading.Thread.Sleep(2500);
Console.WriteLine("\nTotal values: {0}",
c.TotalValue);
Console.Write("\nPress any key to end.");
Console.ReadLine();
}