ThreadStart是一个无参数的代理。 但是你可以使用一个包装类,来达到同样的目的。Public Class Form1 Inherits System.Windows.Forms.Form Public Sub New() MyBase.New() InitializeComponent() End Sub Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Private components As System.ComponentModel.IContainer Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Label1 As System.Windows.Forms.Label <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button Me.Label1 = New System.Windows.Forms.Label Me.SuspendLayout() ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(112, 224) Me.Button1.Name = "Button1" Me.Button1.TabIndex = 0 Me.Button1.Text = "Button1" ' 'Label1 ' Me.Label1.Location = New System.Drawing.Point(104, 96) Me.Label1.Name = "Label1" Me.Label1.TabIndex = 1 Me.Label1.Text = "Label1" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.Button1) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub#End Region Public Sub ShowMsg() MessageBox.Show("The thread is over!") End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim testThread As MyThreadWrapper = New MyThreadWrapper(AddressOf ShowMsg) Try testThread.Start() Catch ex As Exception Me.Label1.Text = ex.Message End Try End SubEnd ClassPublic Delegate Sub CallbackFunction()Public Class MyThreadWrapper Private mCallbackFunction As CallbackFunction Public ReadOnly Thread As System.Threading.Thread Public Done As Boolean Sub New(ByVal callback As CallbackFunction) Me.mCallbackFunction = callback Me.Thread = New System.Threading.Thread(AddressOf Me.RunThread) End Sub Public Overridable Sub Start() Me.Thread.Start() End Sub Private Sub RunThread() Done = False OnStart() Done = True Me.mCallbackFunction() End Sub Protected Sub OnStart() Dim i As Integer For i = 0 To 9 Console.WriteLine("Thread is running {0}", i.ToString()) Thread.CurrentThread.Sleep(200) Next End SubEnd Class
可以这样用。 class CountI { private int i = 0; public CountI(int i) { this.i = i; } public int Count() { for(int j = 0; j < i; ++j) { ++j; } } }public static void Main() { CountI ci = new CountI(1000); Thread t = new Thread(new ThreadStart(ci.Count())); t.Start(); }使用一个类,给参数传递给类,然后调用类的方法。 这是其中一种解决办法,还有别的方法。
不晓得能不能继承 ThreadStart, 你可以试一试。
写一个类吧,把你要用线程的方法写到这个类里,然后在实例化这个类的时候把参数据传给这个类…… 大概如下吧:MyClass myclass = new MyClass(parameters) Thread td = new Thread(ThreadStart(myclass.ThreadModth))public MyClass { private parameter = string.Empty ; public MyClass( string parameters) { parameter = parameters ; } public void ThreadModth() { MessageBox.Show(parameter); } }
很简单呀 用一个没有返回值,没有参数的函数来封装这个function(string path) 如: private void fun() { function(string path); } Thread a = new Thread(ThreadStart(fun)); a.start();
但是你可以使用一个包装类,来达到同样的目的。Public Class Form1
Inherits System.Windows.Forms.Form Public Sub New()
MyBase.New() InitializeComponent() End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ComponentModel.IContainer
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(112, 224)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(104, 96)
Me.Label1.Name = "Label1"
Me.Label1.TabIndex = 1
Me.Label1.Text = "Label1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False) End Sub#End Region Public Sub ShowMsg()
MessageBox.Show("The thread is over!")
End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim testThread As MyThreadWrapper = New MyThreadWrapper(AddressOf ShowMsg)
Try
testThread.Start()
Catch ex As Exception
Me.Label1.Text = ex.Message
End Try
End SubEnd ClassPublic Delegate Sub CallbackFunction()Public Class MyThreadWrapper Private mCallbackFunction As CallbackFunction Public ReadOnly Thread As System.Threading.Thread Public Done As Boolean Sub New(ByVal callback As CallbackFunction)
Me.mCallbackFunction = callback
Me.Thread = New System.Threading.Thread(AddressOf Me.RunThread)
End Sub Public Overridable Sub Start()
Me.Thread.Start()
End Sub Private Sub RunThread()
Done = False
OnStart()
Done = True
Me.mCallbackFunction()
End Sub Protected Sub OnStart()
Dim i As Integer
For i = 0 To 9
Console.WriteLine("Thread is running {0}", i.ToString())
Thread.CurrentThread.Sleep(200)
Next
End SubEnd Class
class CountI
{
private int i = 0;
public CountI(int i)
{
this.i = i;
}
public int Count()
{
for(int j = 0; j < i; ++j)
{
++j;
}
}
}public static void Main()
{
CountI ci = new CountI(1000);
Thread t = new Thread(new ThreadStart(ci.Count()));
t.Start();
}使用一个类,给参数传递给类,然后调用类的方法。
这是其中一种解决办法,还有别的方法。
大概如下吧:MyClass myclass = new MyClass(parameters)
Thread td = new Thread(ThreadStart(myclass.ThreadModth))public MyClass
{
private parameter = string.Empty ;
public MyClass( string parameters)
{
parameter = parameters ;
}
public void ThreadModth()
{
MessageBox.Show(parameter);
}
}
用一个没有返回值,没有参数的函数来封装这个function(string path)
如:
private void fun()
{
function(string path);
}
Thread a = new Thread(ThreadStart(fun));
a.start();