8 Replies - 1398 Views - Last Post: 29 December 2011 - 01:58 PM Rate Topic: -----

#1 Webslave  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-December 11

How to handle unresponsive thread?

Posted 26 December 2011 - 06:13 AM

Hi! First time poster so I hope you'll be paitent with me ;)

I am working on an application that uses screen scraping to extract information from a IBM 3270 telnet emulator. This is working great BUT I'm having some issues with the communication between my application and the emulator.

Every now and then the emulator just stops communicating back towards my application and that makes the thread that is performing the screen scraping unresponsive. There are built in methods for checking if the emulator is ready and all that but sometimes it just stops returning anything.

An example:
if (!status.WaitForInputReady(30000))

This says to the emulator: If you are not ready for input within 30 seconds then do blablabla. This works 99,99% of the time I use it but sometimes it just hangs and does not return neither true or false... massive problem.

How should I handle this? I have a ticker running on my main thread that checks the last time of activity and if it is over 30 seconds then it goes into an error handler but if I try to handle the thread that is unresponsive (eg. thread.Abort()) my main thread also becomes unresponsive, I guess it's waithing for the emulator thread to abort but since it's unresponsive it isn't possible.

I'm very new to multithreaded applications so all tips and tricks are welcome! I'm using a backgroundworker for my emulator thread.

I hope I have explained my problem enough, if not I'll try to ellaborate!

Is This A Good Question/Topic? 0
  • +

Replies To: How to handle unresponsive thread?

#2 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 783
  • Joined: 27-October 11

Re: How to handle unresponsive thread?

Posted 26 December 2011 - 11:36 AM

Does it throw an exception? If it does you can catch and handle it:

try
{
//code that will throw the exception here
}
catch (Exception ex)
{
//code that will handle the exception here
}



Maybe implement throwing your own exception if it does not throw it's own.

Take a look at here for a tutorial on exception handling:

Exceptions and Exception Handling (C# Programming Guide)

This post has been edited by RexGrammer: 26 December 2011 - 11:37 AM

Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,479
  • Joined: 29-May 08

Re: How to handle unresponsive thread?

Posted 26 December 2011 - 01:23 PM

How are creating the threads?
Was This Post Helpful? 0
  • +
  • -

#4 Webslave  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-December 11

Re: How to handle unresponsive thread?

Posted 26 December 2011 - 11:39 PM

Hi all, thanks for your time.

RexGrammer: That is the main problem... it doesn't throw anything, it just stops responding. I think the problem is that sometimes my application loses connection with the emulator (it's an old system from mid 90's) and when I then ask the emulator if it's ready for input it cannot return either true or false since the connection is gone so my application just waits for a response.
Base.LatestActivity = DateTime.Now;
if (!status.WaitForInputReady(30000)) //Wait for 30 seconds and then handle error. This works if the system is responsive and maybe is just slow to reply, but my issue is when the connection is lost and it never returns anything from .WaitForInputReady() and just hangs.
    Helper.ExceptionHandler(new Exception("Timeout"), "Wait");

And since I don't know if the connection is gone until I ask the afforementioned question I cannot forsee this problem.

On my main thread I'm using this in a ticker to check if my other thread (with the error handler above) is unresponsive:
if (this.bwImport.IsBusy && (DateTime.Now - Base.LatestActivity).TotalSeconds > 30)
{
    Base.StatusMessages.Add(new StatusMessage(Base.StatusMessageType.ImportError, Convert.ToDateTime(DateTime.Now.ToShortTimeString()), "Import abortet, timeout."), true);
    if (bwThread != null)
        bwThread.Abort();
    LSImport.ForceCloseInstance();
    Base.CurrentStatus = Base.Status.Off;
}


I've read enough to know I shouldn't be using .Abort() on another thread then from the one that is calling the .Abort() so I need some help with how to handle my unresponsive thread.

Hope I am making myself understood :)

AdamSpeight2008: I am using a backgroundworker for my emulator thread but since it's in an unresponsive state I cannot use .CancelAsync() 'cause the thread will never be able to cancel itself...
Was This Post Helpful? 0
  • +
  • -

#5 Webslave  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-December 11

Re: How to handle unresponsive thread?

Posted 27 December 2011 - 12:58 AM

I have to add (I can't seem to find a way to edit my post?) that the function .WaitForInputReady() in my emulator thread is from the emulator (I am referencing it) so I cannot control what's happening inside of it.
Was This Post Helpful? 0
  • +
  • -

#6 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 783
  • Joined: 27-October 11

Re: How to handle unresponsive thread?

Posted 27 December 2011 - 03:15 AM

You don't have enough posts to edit your posts.

Why don't you throw an exception when you check if the thread is not responsive:

throw new Exception("This is an exception");



Of course you would find the correct type of exception that suits your needs and pass all the relevant parameters, and then catch that exception in a catch statement. Then you could make a log file to see if your checking logic does work (meaning it is executed every time your thread stops). If it does keep it that way and implement the logic of stopping the thread in the catch statement. Maybe take a look at delegates on how to stop the thread.
Was This Post Helpful? 0
  • +
  • -

#7 Webslave  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-December 11

Re: How to handle unresponsive thread?

Posted 28 December 2011 - 12:23 AM

View PostRexGrammer, on 27 December 2011 - 11:15 AM, said:

You don't have enough posts to edit your posts.

Ah, thanks for clearing that up :)

View PostRexGrammer, on 27 December 2011 - 11:15 AM, said:

Why don't you throw an exception when you check if the thread is not responsive:

throw new Exception("This is an exception");


Well since I can't get the thread that is unresponsive to throw an exception (if I could this wouldn't be an issue) I assumed that it wouldn't make any difference if the main thread throws an exception or just handles it in another way. I may be totally wrong on this one :)

View PostRexGrammer, on 27 December 2011 - 11:15 AM, said:

Of course you would find the correct type of exception that suits your needs and pass all the relevant parameters, and then catch that exception in a catch statement. Then you could make a log file to see if your checking logic does work (meaning it is executed every time your thread stops). If it does keep it that way and implement the logic of stopping the thread in the catch statement. Maybe take a look at delegates on how to stop the thread.

So, just to clearify: You are saying that by using delegates I could somehow be able to, from the main thread that is handling that the emulator thread is not responding, abort the emulator thread?

I've been reading a little on delegates and I can't figure out how to use them in this kind of scenario, I just can't see the use of it. Do you have an example of how it could be used or maybe point to an article that describes your idea? It's, obviously, the first time I've been working with delegates so I'm totally new :)

Thanks for your time anyhow, much appreciated.
Was This Post Helpful? 0
  • +
  • -

#8 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 783
  • Joined: 27-October 11

Re: How to handle unresponsive thread?

Posted 28 December 2011 - 04:08 AM

delegate (C# Reference)(MSDN Article)
Delegates (C# Programming Guide) (MSDN Article)
Delegates, Lambdas and Events (DIC Tutorial)

Example:
public delegate void TestDelegate(string message);
TestDelegate myDelegate;
public int MyMethod1 (string message)
{
//Do stuff here
}
public int MyMethod2 (string message)
{
//Do stuff here
}

public void EventHandler (object sendr, EventArgs e)
{
myDelegate += MyMethod1;
myDelegate += MyMethod2;
myDelegate("Message");
//This calls both methods and passes the "Message" string as a parameter
}



A few things to note:

1. Methods that are added 'into' the delegate must have the same signature as the delegate. Signature = the parameters that can be passed into the method + the return type of the method
2. Methods are added without the '()'
3. The delegate is called as any other method

Delegates are just methods that can have methods passed as parameters and can return methods.

Put it in the most ugliest and untrue definition: They are methods that call other methods

Delegates are not the same as events:

Events and Delegates (MSDN Article)

This post has been edited by RexGrammer: 28 December 2011 - 04:23 AM

Was This Post Helpful? 0
  • +
  • -

#9 Webslave  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-December 11

Re: How to handle unresponsive thread?

Posted 29 December 2011 - 01:58 PM

I just want to say thanks for your time. I seem to have managed to resolve the problem. By NOT using the function .WaitForInputRead() and instead using a way around (that I never thought would work) so now the thread hasn't become unresponsive for quite some time :)

I'll probably be back with other questions :) Thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1