我知道只能在Thread中调用Exitthread,只要你可以访问Thread对象,就可以随时随地使用Abort.但是当你需要强制关闭一个线程时,这两个方法(操作方式)之间是否存在其他显着差异?
我有以下代码,我开始Thread
使用一个ParameterizedThreadStart
对象作为构造函数参数:
static object obj = new object();
static void Main(string[] args)
{
ParameterizedThreadStart start = (o) =>
{
ThreadTest(o);
};
var t = new Thread(() => start(obj));
t.Name = "t";
t.Start();
Thread.Sleep(3000);
obj = null;
// Why the Thread (t) continue here??
Console.ReadKey();
}
private static void ThreadTest(object o)
{
while (o != null)
{
Console.WriteLine(Thread.CurrentThread.Name);
Thread.Sleep(1000);
}
}
Run Code Online (Sandbox Code Playgroud)
之后,我设置obj
到null
在ThreadTest
方法的参数o
仍然是一个有效的对象,为什么呢?
如何设置参数o
来null
使用obj
?
我想知道是否有可能使用线程名称Abort(脏路)一个线程?这是一些示例代码:
public void blah() {
int TableID = 4; //set by the using at runtime
Thread myThread = new Thread(() => beginUser(picture));
myThread.Name = Convert.ToString(TableID);
myThread.Start();
}
Run Code Online (Sandbox Code Playgroud)
所以现在我创建了一个线程.在程序的后面,用户可以结束一个线程,我的问题就在哪里.如何通过它的名字结束一个线程?或许是另一种结束它的方式?我不想使用背景工作者.
例: myThead[4].Abort();
谢谢
我有一个关于多线程应用程序的问题.我使用TaskFactory来启动一个cpu +时间密集型方法.此方法是对SAP的调用,需要很长时间才能完成.用户应该有一个取消任务的选项.目前我正在使用thread.Abort(),但我知道这种方法不是取消它的最佳解决方案.有没有人有替代方案的建议?
代码示例:
Form_LoadAction loadbox = new Form_LoadAction();
Thread threadsapquery = null;
Task.Factory.StartNew<>(() =>
{
t = Thread.CurrentThread;
Thread.sleep(10000000000); //represents time + cpu intensive method
}
loadbox.ShowDialog();
if (loadbox.DialogResult == DialogResult.Abort)
{
t.Abort();
}
Run Code Online (Sandbox Code Playgroud) 使用ADO.Net Entity框架读取数据时,我遇到了一个奇怪的问题.
我有两张桌子,"Surveys"和"PatientVists"."VisitId"是"PatientVists"中的主键,它是"调查"表中的外键.
我正在使用以下查询:
foreach (var survey in db.Surveys.Include(p => p.PatientVisit).Where(p => p.FacilityId == f.Id && p.IsCompleted == true && p.IsImaged == false).OrderBy(p => p.PatientVisit.MrnId).ThenBy(p => p.DateUpdated).ToList())
{
// reminign code
}
Run Code Online (Sandbox Code Playgroud)
上面的查询似乎很正常.但是在执行查询时我得到线程中止异常.我可以理解,由于数据量很大,我是否会收到超时异常.但我不知道为什么我得到线程中止异常.我没有明确地产生一个线程.此代码段位于Web服务中,但我认为它与此无关.
以下是堆栈跟踪:
System.Threading.ThreadAbortException: Thread was being aborted.
at SNIReadSyncOverAsync(SNI_ConnWrapper* , SNI_Packet** , Int32 )
at SNINativeMethodWrapper.SNIReadSyncOverAsync(SafeHandle pConn, IntPtr& packet, Int32 timeout)
at System.Data.SqlClient.TdsParserStateObject.ReadSniSyncOverAsync()
at System.Data.SqlClient.TdsParserStateObject.TryReadNetworkPacket()
at System.Data.SqlClient.TdsParserStateObject.TryPrepareBuffer()
at System.Data.SqlClient.TdsParserStateObject.TryReadByte(Byte& value)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at …
Run Code Online (Sandbox Code Playgroud) 如果它需要太多时间我必须Timer
取消Thread
它.
System.Timers.Timer timer_timers = new System.Timers.Timer();
Thread thread = new Thread(startJob);
thread.Name = "VICTIM_THREAD";
Run Code Online (Sandbox Code Playgroud)
在启动Thread
方法时,我启动Timer
并将当前Thread作为参数传递给事件.
public void startJob()
{
Debug.WriteLine("Name: " + Thread.CurrentThread.Name);
timer_timers.Elapsed += (sender, e) => T_Elapsed(sender, e, Thread.CurrentThread);
timer_timers.Interval = 5000;
// Start simulation process
while (true)
{
Thread.Sleep(700);
Debug.WriteLine("Thread: " + Thread.CurrentThread.Name + " ALIVE: " + thread.IsAlive);
}
}
Run Code Online (Sandbox Code Playgroud)
定时器事件:
private void T_Elapsed(object sender, ElapsedEventArgs e, Thread currentThread)
{
// EDIT: show the correct NAME! of the …
Run Code Online (Sandbox Code Playgroud) 如果用户需要,我只是尝试通过单击按钮停止进程而不是退出应用程序来中止线程。
这是原始代码:
private void Abort_Click(object sender, EventArgs e)
{
// thread1.Abort();
}
/// Runs method 'Copy' in a new thread with passed arguments - on this way we separate it from UI thread otherwise it would freeze
private void backgroundCopy_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e )
{
List<object> genericlist = e.Argument as List<object>;
Thread thread1 = new Thread(() => Copy(genericlist[0].ToString(), genericlist[1].ToString()));
thread1.Start();
thread1.Join(); //Waiting for thread to finish
}
Run Code Online (Sandbox Code Playgroud)
我尝试过的:
我通过将线程字段移出方法来尝试从按钮单击事件中 Abort() 线程,这样您就可以从按钮单击事件中获得访问权限,但它会引发许多错误:
object sender;
System.ComponentModel.DoWorkEventArgs e;
List<object> genericlist = e.Argument as …
Run Code Online (Sandbox Code Playgroud)