本文整理汇总了C#中Thread.Abort方法的典型用法代码示例。如果您正苦于以下问题:C# Thread.Abort方法的具体用法?C# Thread.Abort怎么用?C# Thread.Abort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Thread
的用法示例。
在下文中一共展示了Thread.Abort方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
public static int Main()
{
Thread
t1
=
new
Thread(new
ThreadStart
(MultiThreadExceptionTest.ThreadStart1));
t1.Name
=
"Thread 1";
Thread.Sleep
(100);
t1.Start();
Thread.Sleep
(200);
t1.Abort
("STATETEST");
t1.Join
();
Console.WriteLine
("Result: "
+
result);
if
(result
!=
27)
return
1;
return
0;
}
示例2: Main
static void Main(string[] args)
{
//Declarations
ConsoleKey ck;
Thread thSniffer;
//Create the sniffer thread
thSniffer = new Thread(new ThreadStart(Sniffer.GetSniffer().Start));
//Start sniffing
thSniffer.Start();
Console.WriteLine("Press Enter key to quit anytime...");
Console.WriteLine();
//Read the console
ck = Console.ReadKey().Key;
//Shutdown the sniffer if the user opted to
if (ck == ConsoleKey.Enter)
{
Sniffer.GetSniffer().ShutDown();
thSniffer.Abort();
}
}
示例3: Start
public void Start()
{
try
{
//Starting the TCP Listener thread.
sampleTcpThread = new Thread(new ThreadStart(StartListen2));
sampleTcpThread.Start();
Console.Message = ("Started SampleTcpUdpServer's TCP Listener Thread!\n"); OnChanged(EventArgs.Empty);
}
catch (Exception e)
{
Console.Message = ("An TCP Exception has occurred!" + e); OnChanged(EventArgs.Empty);
sampleTcpThread.Abort();
}
try
{
//Starting the UDP Server thread.
sampleUdpThread = new Thread(new ThreadStart(StartReceiveFrom2));
sampleUdpThread.Start();
Console.Message = ("Started SampleTcpUdpServer's UDP Receiver Thread!\n"); OnChanged(EventArgs.Empty);
}
catch (Exception e)
{
Console.Message = ("An UDP Exception has occurred!" + e); OnChanged(EventArgs.Empty);
sampleUdpThread.Abort();
}
}
示例4: Connect
public TcpClient Connect()
{
// kick off the thread that tries to connect
connected = false;
exception = null;
Thread thread = new Thread(new ThreadStart(BeginConnect));
thread.IsBackground = true; // So that a failed connection attempt
// wont prevent the process from terminating while it does the long timeout
thread.Start();
// wait for either the timeout or the thread to finish
thread.Join(_timeout_milliseconds);
if (connected == true)
{
// it succeeded, so return the connection
thread.Abort();
return connection;
}
if (exception != null)
{
// it crashed, so return the exception to the caller
thread.Abort();
throw exception;
}
else
{
// if it gets here, it timed out, so abort the thread and throw an exception
thread.Abort();
string message = string.Format("TcpClient connection to {0}:{1} timed out",
_hostname, _port);
throw new TimeoutException(message);
}
}
示例5: Connect
public TcpClient Connect()
{
connected = false;
exception = null;
Thread thread = new Thread(new ThreadStart(BeginConnect));
thread.IsBackground = true;
thread.Start();
thread.Join(_timeout_milliseconds);
if (connected == true)
{
thread.Abort();
return connection;
}
if (exception != null)
{
// it crashed, so return the exception to the caller
thread.Abort();
return connection;
//throw exception;
}
else
{
// if it gets here, it timed out, so abort the thread and throw an exception
thread.Abort();
string message = string.Format("TcpClient connection to {0}:{1} timed out",
_hostname, _port);
//throw new TimeoutException(message);
return connection;
}
}
示例6: Connect
public static bool Connect(Socket s, IPEndPoint ep, int timeOutmiliSeconds)
{
_connected = false;
var registerThread = new Thread(() => SockConnect(s, ep)) { Priority = ThreadPriority.Normal };
registerThread.Start();
int timeout = 0;
while (registerThread.IsAlive)
{
if (_connected)
{
registerThread.Abort();
return true;
}
Thread.Sleep(100);
if (++timeout == timeOutmiliSeconds / 100)
{
while (_killwait)
{
;
}
registerThread.Abort();
return false;
}
}
return _connected;
}
示例7: Kill
/// <summary>
/// Do our best to kill a thread, passing state info
/// </summary>
/// <param name="thread">The thread to kill</param>
/// <param name="stateInfo">Info for the ThreadAbortException handler</param>
public static void Kill(Thread thread, object stateInfo)
{
try
{
if (stateInfo == null)
thread.Abort();
else
thread.Abort(stateInfo);
}
catch (ThreadStateException)
{
#if !NETCF
// Although obsolete, this use of Resume() takes care of
// the odd case where a ThreadStateException is received.
#pragma warning disable 0618,0612 // Thread.Resume has been deprecated
thread.Resume();
#pragma warning restore 0618,0612 // Thread.Resume has been deprecated
#endif
}
#if !NETCF
if ( (thread.ThreadState & ThreadState.WaitSleepJoin) != 0 )
thread.Interrupt();
#endif
}
示例8: GetFilesLockedBy
/// <summary>
/// Return a list of file locks held by the process.
/// </summary>
public static List<string> GetFilesLockedBy(Process process)
{
var outp = new List<string>();
ThreadStart ts = delegate
{
try
{
outp = UnsafeGetFilesLockedBy(process);
}
catch { Ignore(); }
};
var t = new Thread(ts);
t.IsBackground = true;
try
{
t.Start();
if (!t.Join(250))
{
try
{
t.Interrupt();
t.Abort();
}
catch { t.Abort(); }
}
}
catch { t.Abort(); }
return outp;
}
示例9: HttpConnect
/// <summary>
/// HTTP通信
/// </summary>
/// <param name="JSONstring"></param>
/// <returns></returns>
public static bool HttpConnect(string JSONstring)
{
JSON = JSONstring;
HttpWebRequest request = null;
HttpWebResponse response = null;
int lastTime = 0;
//网络请求线程
Thread requestThread = new Thread(delegate()
{
WaitForConnection(out request);
});
requestThread.Start();
//通过持续时间判断网络是否异常
lastTime = System.DateTime.Now.Millisecond + System.DateTime.Now.Second * 1000;
while(requestThread .IsAlive)
{
if(System.DateTime.Now.Millisecond+System.DateTime.Now.Second*1000-lastTime<0)
{
lastTime = System.DateTime.Now.Millisecond + System.DateTime.Now.Second * 1000;
}
else if(System.DateTime.Now.Millisecond+System.DateTime.Now.Second*1000-lastTime>2000)
{
Debug.Log("Request Lost!");
requestThread.Abort();
return false;
}
}
//网络回应线程
Thread responseThread = new Thread(delegate()
{
WaitForResponse(request, response);
});
responseThread.Start();
//通过持续时间判断网络是否异常
lastTime = System.DateTime.Now.Millisecond + System.DateTime.Now.Second * 1000 - lastTime;
while(responseThread .IsAlive )
{
if (System.DateTime.Now.Millisecond + System.DateTime.Now.Second * 1000 - lastTime < 0)
{
lastTime = System.DateTime.Now.Millisecond + System.DateTime.Now.Second * 1000;
}
else if (System.DateTime.Now.Millisecond + System.DateTime.Now.Second * 1000 - lastTime > 2000)
{
Debug.Log("Request Lost!");
requestThread.Abort();
return false;
}
}
return true;
}
示例10: ForceStop
public static void ForceStop(ref Thread thread, bool setNull)
{
try
{
if (thread != null && thread.IsAlive)
{
//force stop
try
{
thread.Interrupt();
}
catch (ThreadInterruptedException ex)
{
Log.WarnFormat("thread {0} Interrupt error : {1}", thread.Name, ex.Message);
try
{
thread.Abort();
}
catch (System.Exception ex1)
{
Log.WarnFormat("thread {0} Abort error : {1}", thread.Name, ex1.Message);
}
}
catch (System.Exception ex)
{
Log.WarnFormat("thread {0} Interrupt error : {1}", thread.Name, ex.Message);
try
{
thread.Abort();
}
catch (System.Exception ex1)
{
Log.WarnFormat("thread {0} Abort error : {1}", thread.Name, ex1.Message);
}
}
finally
{
if (setNull)
{
thread = null;
}
}
}
else
{
if (setNull)
{
thread = null;
}
}
}
catch (System.Exception ex)
{
Log.Error("Not expectant exception : ", ex);
throw ex;
}
}
示例11: Thread_wont_die
public void Thread_wont_die()
{
var t = new Thread(IWillNotDie);
t.Start();
Thread.Sleep(1000);
t.Abort();
Thread.Sleep(1000);
t.Abort();
Thread.Sleep(1000);
t.Abort();
Assert.That(t.ThreadState == ThreadState.Running);
}
示例12: SendData
///<summary></summary>
public static void SendData(Program ProgramCur,Patient pat) {
_path=Programs.GetProgramPath(Programs.GetCur(ProgramName.DemandForce));
if(!File.Exists(_path)) {
MessageBox.Show(_path+" could not be found.");
return;
}
if(MessageBox.Show(Lan.g("DemandForce","This may take 20 minutes or longer")+". "+Lan.g("DemandForce","Continue")+"?","",MessageBoxButtons.OKCancel)!=DialogResult.OK) {
return;
}
_formProg=new FormProgress();
_formProg.MaxVal=100;
_formProg.NumberMultiplication=1;
_formProg.DisplayText="";
_formProg.NumberFormat="F";//Show whole percentages, not fractional percentages.
Thread workerThread=new Thread(new ThreadStart(InstanceBridgeExport));
workerThread.Start();
if(_formProg.ShowDialog()==DialogResult.Cancel) {
workerThread.Abort();
MessageBox.Show(Lan.g("DemandForce","Export cancelled")+". "+Lan.g("DemandForce","Partially created file has been deleted")+".");
CheckCreatedFile(CodeBase.ODFileUtils.CombinePaths(Path.GetDirectoryName(_path),"extract.xml"));
_formProg.Dispose();
return;
}
MessageBox.Show(Lan.g("DemandForce","Export complete")+". "+Lan.g("DemandForce","Press OK to launch DemandForce")+".");
try {
Process.Start(_path);//We might have to add extract.xml to launch command in the future.
}
catch {
MessageBox.Show(_path+" is not available.");
}
_formProg.Dispose();
}
示例13: Start
public static void Start()
{
Log.Info("Starting RconHandler.");
try
{
Console.WriteLine(string.Format("RconHandler is running at UDP port {0} and password is {1}",
ListenPort,
Password));
listen = new Thread(Listener);
listen.Start();
while (true)
{
if (listen.ThreadState != ThreadState.Running)
{
listen.Abort();
while (listen.ThreadState != ThreadState.Stopped)
continue;
listen.Start();
}
Thread.Sleep(3000);
}
}
catch (Exception e)
{
Log.Error(e.ToString());
}
}
示例14: CaptureWebPageBytes
public static byte[] CaptureWebPageBytes(string body, int width, int height)
{
bool bDone = false;
byte[] data = null;
DateTime startDate = DateTime.Now;
DateTime endDate = DateTime.Now;
//sta thread to allow intiate WebBrowser
var staThread = new Thread(delegate()
{
data = CaptureWebPageBytesP(body, width, height);
bDone = true;
});
staThread.SetApartmentState(ApartmentState.STA);
staThread.Start();
while (!bDone)
{
endDate = DateTime.Now;
TimeSpan tsp = endDate.Subtract(startDate);
System.Windows.Forms.Application.DoEvents();
if (tsp.Seconds > 50)
{
break;
}
}
staThread.Abort();
return data;
}
示例15: BasicConsole
public BasicConsole(bool colorful) {
_output = System.Console.Out;
_errorOutput = System.Console.Error;
SetupColors(colorful);
_creatingThread = Thread.CurrentThread;
#if !SILVERLIGHT // ConsoleCancelEventHandler
// Create the default handler
this.ConsoleCancelEventHandler = delegate(object sender, ConsoleCancelEventArgs e) {
if (e.SpecialKey == ConsoleSpecialKey.ControlC) {
e.Cancel = true;
_ctrlCEvent.Set();
_creatingThread.Abort(new KeyboardInterruptException(""));
}
};
Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) {
// Dispatch the registered handler
ConsoleCancelEventHandler handler = this.ConsoleCancelEventHandler;
if (handler != null) {
this.ConsoleCancelEventHandler(sender, e);
}
};
#endif
_ctrlCEvent = new AutoResetEvent(false);
}