 |
|
การส่ง parameter มากกว่า 1 ตัว อยากทราบว่า ParameterizedThreadStart สามารถรับ parameter มากกว่า 1 ตัวได้หรือไม่ครับ |
|
 |
|
|
 |
 |
|
Code (C#)
Thread standardTCPServerThread = new Thread(startSocketServerAsThread);
standardServerThread.Start( orchestrator, initializeMemberBalance, arg, 60000);
}
static void startSocketServerAsThread(ServiceOrchestrator orchestrator, List<int> memberBalances, string arg, int port)
{
startSocketServer(orchestrator, memberBalances, arg, port);
}
แก้เป็น
Code (C#)
Thread standardTCPServerThread =
new Thread(
unused => startSocketServerAsThread(initializeMemberBalance, arg, 60000)
);
http://stackoverflow.com/questions/831009/thread-with-multiple-parameters
|
 |
 |
 |
 |
Date :
2011-12-07 12:46:49 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (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. C# infers the
// appropriate delegate creation syntax:
// new ParameterizedThreadStart(Work.DoWork)
//
Thread newThread = new Thread(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("My Data1","My Data2");
// 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. C# infers
// the appropriate delegate creation syntax:
// new ParameterizedThreadStart(w.DoMoreWork)
//
Work w = new Work();
newThread = new Thread(w.DoMoreWork);
// Pass an object containing data for the thread.
//
newThread.Start("My Data1","My Data2");
}
public static void DoWork(object data1,object data2)
{
Console.WriteLine("Static thread procedure. Data1='{0}'",
data1);
Console.WriteLine("Static thread procedure. Data2='{0}'",
data2);
}
public void DoMoreWork(object data1,object data2)
{
Console.WriteLine("Instance thread procedure. Data1='{0}'",
data1);
Console.WriteLine("Instance thread procedure. Data2='{0}'",
data2);
}
}
|
 |
 |
 |
 |
Date :
2011-12-07 12:50:42 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อันนี้ for VB.NET
Code (VB.NET)
Imports System.Threading
Public Class Work
Public 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. C# infers the
' appropriate delegate creation syntax:
' new ParameterizedThreadStart(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
' reference type or value type. The following code passes
' the integer value 42.
'
newThread.Start("My Data1", "My Data2")
' 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. C# infers
' the appropriate delegate creation syntax:
' new ParameterizedThreadStart(w.DoMoreWork)
'
Dim w As New Work()
newThread = New Thread(AddressOf w.DoMoreWork)
' Pass an object containing data for the thread.
'
newThread.Start("My Data1", "My Data2")
End Sub
Public Shared Sub DoWork(data1 As Object, data2 As Object)
Console.WriteLine("Static thread procedure. Data1='{0}'", data1)
Console.WriteLine("Static thread procedure. Data2='{0}'", data2)
End Sub
Public Sub DoMoreWork(data1 As Object, data2 As Object)
Console.WriteLine("Instance thread procedure. Data1='{0}'", data1)
Console.WriteLine("Instance thread procedure. Data2='{0}'", data2)
End Sub
End Class
|
 |
 |
 |
 |
Date :
2011-12-07 12:51:33 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มีวีธิที ทำงานร่วมกับ ui-thread ไม่ครับ แต่ก็ขอบคุณมากครับ คุณ win
|
 |
 |
 |
 |
Date :
2011-12-07 13:27:29 |
By :
kanchen |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|