[Mono-dev] Thread Abort() sometimes fails in mono
Wade Maxfield
wmaxfield at gmail.com
Tue Apr 4 10:26:05 EDT 2006
Hi,
I've come into another funny.
When going after a web page using code previously posted on the list, if I
call the thread abort code, it will say the web page timed out (through the
console.writeline in the catch{} section below), and will keep going. In
other words, the application quits but the thread stays running and printing
its results.
Sometimes it aborts just fine. It appears I have to catch it just right
for it to fail or succeed. It may be the abort is called during the
Console.Writeline() code and fails, it may be it is called
during the web page fetch and fails with a timeout.
In Microsoft .NET, it always aborts.
I've also noticed a difference between the font computations on MS
Windows and Ubuntu. In Ubuntu, the fonts are huge for label controls, in
Microsoft, they are smaller. don't know why yet, but it is a pain.
----------------------------------------
application quit code
--------------------------------------
if (MessageBox.Show ("Are you sure you want to quit the
application?", "System Status",
MessageBoxButtons.YesNo, MessageBoxIcon.Question)
== DialogResult.Yes)
{
/*doesn't always work ---->*/ Global.WebThread.Abort();
GetWebPageClass.QuitNow = true;// added this so abort would
always work
Application.Exit();
}
I set up a static in the class so that I could write to it and it would
abort. If I leave the QuitNow flag alone, the thread does not always
abort. This is with 1.1.14, downloaded yesterday.
Following is relevant (I hope) sections of code. Note the web page comes
back *immediately*, but the port the web page is on is 2001.
-----------------------------------------------------------------------------------------
I can hear the screams now, but I've created a global object so that
we have certain items with visibility across threads and objecs, and whose
values are *not* on the stack. It makes it easier to write the code to
navigate
between forms for people used to windows. You just call
Global.MainForm.Show(), etc.
--------------------------------------------------------------------------------------------------
public class Global
{
public static GetWebPageClass WP;
public static Thread WebThread ;
public static StatisticsForm StatForm;
public static WinForm MainForm;
public static DebugForm DbgForm;
public static int DebugFlag;
public Global()
{
//
// TODO: Add constructor logic here
//
DebugFlag = 0;
}
}
-----------------------------------------------------------------------------
the class constructor sets the web page and port
-----------------------------------------------------------------------------
//
host url port
Global.WP = new GetWebPageClass("ampro2","shm/gdsp.php",2001);
Global.WebThread = new Thread(new ThreadStart(Global.WP.Run));
Global.WebThread.Start();
while (!Global.WebThread.IsAlive);
Application.Run(Global.MainForm = new WinForm());
-------------------------------------------
The fetch web page object fragment
--------------------------------------------
public string GetWebPage()
{
try
{
UriBuilder uri = new UriBuilder("http",host,port,url);
HttpWebRequest webRequest =
(HttpWebRequest)WebRequest.Create(uri.ToString());
webRequest.Timeout = 1000;
if (Debug)
{
string MyUri = uri.ToString();
Console.WriteLine("Polling: ");
Console.WriteLine(MyUri);
//Console.WriteLine("\n");
}
HttpWebResponse webResponse =
(HttpWebResponse)webRequest.GetResponse();
Stream responseStream = webResponse.GetResponseStream();
string responseEncoding="";
// responseEncoding = webResponse.ContentEncoding.Trim();
if (responseEncoding.Length == 0)
responseEncoding="us-ascii";
StreamReader responseReader = new StreamReader(responseStream,
System.Text.Encoding.GetEncoding(responseEncoding));
return(responseReader.ReadToEnd());
}
catch
{
if (Debug)
Console.WriteLine("Web site timed out\n"); //
return("Web Site Timed Out\nSorry!\n");
}
}
public void Run()
{
string Name="";
int Value=0;
string Page="";
Tokens t ;
int TokenCounter=0;
MyDspPanelInfo = new RemoteInfoClass();
while(true)
{
Page = GetWebPage();
//----------------------------------------------------------
// look at the web page and get the data from it
//----------------------------------------------------------
//Page = WP.GetWebPage("ampro2",url,2001);
t = new Tokens(Page, new char[] {':'});
TokenCounter =0;
foreach (string item in t)
{
if (Debug)
Console.WriteLine("Item = {0}",item);
if (TokenCounter == 0)
{
TokenCounter++;
Name = item;
}
else
{
try {
Value = int.Parse(item);
}
catch
{
continue; // ignore this one
}
// MyDspPanelInfo.Set(Name,Value);
TokenCounter =0;
continue;
}
}
Thread.Sleep(900);
if (QuitNow)
break;// exit this loop
}
}
---------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20060404/8e31431a/attachment.html
More information about the Mono-devel-list
mailing list