Self Destruct Timer for Thread?

Is there any way i can create a thread which ends no matter what after 1 min?

Currently i make a thread to login to a website code is below:

 private void runBrowserThread()

        specialCheck = 0;
        var th = new Thread(() =>
            var br = new WebBrowser();
            browserCounter = 0;
            br.ScriptErrorsSuppressed = true;
            br.DocumentCompleted += browser_DocumentCompleted;



BUT there are a lot of complications in the process so i do not want to code them all , what i am thinking something like a self destruct i can use when making this timer ,so that no matter what if a code is stuck , it does not reach one of the Application.Exitthread() it will be stuck there forever so a self destruct after 1 min , Exits the threads so the code can move on.

Because currently i have Application.Exitthread() at many places but i sometimes the code is stuck does not reach any, so let me know if there is any way.


As you're already blocking on th.Join you could use that to terminate the thread, like so:

Boolean success = th.Join( 60 * 1000 ); // 60 second timeout
if( !success ) th.Abort();

If you don't want to block on Join, then you could spawn a second watchdog thread that exists only to block on Join and perform the timeout and termination task instead of the instigator thread.

Also, use of Application.ExitThread isn't good design. Threads can terminate by returning from their entrypoint method - it also removes an unnecessary dependence on the Application class and its associated System.Windows.Forms assembly.

Need Your Help

Rules for Iterator Invalidation

c++ stl iterator invalidate

What are the usual rules for Iterator invalidation when operating over STL container classes(Vector,Dequeue,list,map,multimap,set,multiset). Is it possible to categorize and sum up some general

_vscwprintf on Mac OS X/Linux

c++ c linux osx

I am porting an application on Mac OS X which was written for Windows.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.