关于线程启动的问题,急!!在线等。
本人新手,请教各位。private void OnShown(object sender, EventArgs e)
{
Thread m_Thread = new Thread(new ThreadStart(this.BeginImport));
m_Thread.Name = "UpImport_Thread";
m_Thread.Start();
}
本意是,当前窗体显示的时候,启动线程,但是,BeginImport这个方法是个带多个参数的方法,所以会出现异常。
请问,这个应该怎么处理!?
最好写下具体的代码片段,谢谢啦
问题解决 马上散分。。急!!
--------------------编程问答-------------------- Thread m_Thread = new Thread(new ThreadStart(this.BeginImport));
中
ThreadStart是个委托,你看看
Thread得构造函数吧 --------------------编程问答-------------------- http://www.skymean.com/blog/post/261.html
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
http://feiyun0112.cnblogs.com/ --------------------编程问答-------------------- To yfcomeon:Thread的构造函数倒是可以这样用
Thread m_Thread = new Thread(new ThreadStart(this.BeginImport), new object[] { });
但是后面的参数根本是获取不到的啊。传不过来 --------------------编程问答-------------------- http://www.skymean.com/blog/post/261.html
你看看吧
不知道对你有用不 --------------------编程问答-------------------- 还是搞不定。。各位帮帮忙哦。。。
--------------------编程问答-------------------- 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(42);
// 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("The answer.");
}
public static void DoWork(object data)
{
Console.WriteLine("Static thread procedure. Data='{0}'",
data);
}
public void DoMoreWork(object data)
{
Console.WriteLine("Instance thread procedure. Data='{0}'",
data);
}
}
/* This code example produces the following output (the order
of the lines might vary):
Static thread procedure. Data='42'
Instance thread procedure. Data='The answer'
*/ --------------------编程问答-------------------- 以上方法是2.0的,另一个方法:
using System;
using System.Threading;
public class ThreadWork
{
private int time;//需传入的参数
public ThreadWork(int _time)
{
time = _time;
}
public void DoWork()
{
for(int i = 0; i<time;i++)
{
Console.WriteLine("Working thread...");
Thread.Sleep(100);
}
}
}
class ThreadTest
{
public static void Main()
{
ThreadWork _ThreadWork = new ThreadWork(3);
ThreadStart myThreadDelegate = new ThreadStart(_ThreadWork.DoWork);
Thread myThread = new Thread(myThreadDelegate);
myThread.Start();
for(int i = 0; i<3; i++)
{
Console.WriteLine("In main.");
Thread.Sleep(100);
}
}
} --------------------编程问答-------------------- 变通一下。
class Class1
{
int a;
int b;
void Main()
{
//
.........
}
}
Class1 obj = new Class1();
obj.a = 1;
obj.b = 2;
Thread m_Thread = new Thread(new ThreadStart(obj.Main));
m_Thread.Name = "UpImport_Thread";
m_Thread.Start();
补充:.NET技术 , C#