mohasad

hi all ,
i am working in WM2003 . in that i am not finding any option to stop the thread . in my case , the issue is as follows:
1. i am showing some animation in one thread .
2. i am fetching the information from network in another thread .
3. at the same time i am having a softkey ,when i press that softkey the network fetching should be stopped and return to the main menu.
but its not happening so . can anyone help me in solving this issue .
thanks
sadiq



Re: Smart Devices VB and C# Projects stopping the thread

Ilya Tumanov

1. Animation on the thread is probably useless because you have to marshal to UI thread using Control.Invoke() for actual work. Consider using Form's timer for animation.

2. Add a flag which you can set from UI which thread checks from time to time. As soon as flag is set, thread should stop doing whatever it was doing and terminate:

while (!terminate)

{

// Fetch portion of data from network

}

// Close network connection and exit

...

terminate = true; // Stop thread.






Re: Smart Devices VB and C# Projects stopping the thread

mohasad

hi Ilya ,
i am having a problem in invoking Control.Invoke , as i said earlier i don know how to use Control.Invoke , because in my application when i press soft key , the animation should start in one thread, and the information should be fetched from network at the sametime in the main thread. how can i use control.Invoke in this scenario . at the same time i am having a right soft key "CANCEL" , when it pressed all the thread should be stopped and it should returns to mainscreen .

thanks
sadiq





Re: Smart Devices VB and C# Projects stopping the thread

gtamas

Hi,

a small example how to use the invoke. It is a part of a SignatureBox control

public class SignatureBox : PictureBox, IDisposable
{
private class DrawLineEventArgs : EventArgs
{
public readonly Pen Pen;
public readonly Point CurrentPoint;
public readonly Point NextPoint;

public DrawLineEventArgs( Pen oPen, Point oCurrentPoint, Point oNextPoint ) : base ()
{
this.Pen = oPen;
this.CurrentPoint = oCurrentPoint;
this.NextPoint = oNextPoint;
}
}


private delegate void DrawLineEvent( Object oSender, DrawLineEventArgs oEventArgs );
private delegate void OnUpdateEvent ();
...
// In this method will be the thread started
private void prepareWriting ()
{
if ( this.oWritingThread != null )
return;

this.OnDrawLine += new DrawLineEvent ( this.onDrawLineEventHandler );
this.OnUpdate += new OnUpdateEvent ( this.onUpdateEventHandler );

this.oWritingThread = new Thread ( new ThreadStart ( this.writeFromBuffer ) );
this.oWritingThread.Start ();
}

// This method runs in the thread
private void writeFromBuffer ()
{
while ( !this.bDisposed )
{
if ( this.qBuffer.Count == 0 )
Thread.Sleep ( this.iSleepPeriod );

// Hier will be the GUI updated
this.drawToGraphics ( );
}
this.bDisposed = false;
this.oWritingThread = null;
}

private void drawToGraphics ()
{
if ( this.qBuffer.Count == 0 )
return;

if ( this.oCurrentPoint == Point.Empty )
this.oCurrentPoint = ( Point ) this.qBuffer.Dequeue ();

Point oNextPoint;
int i = 0;

object[] aParams = new object[ 2 ];
aParams[ 0 ] = this;

while ( this.qBuffer.Count != 0 )
{
oNextPoint = ( Point ) this.qBuffer.Dequeue ();

// this.AdjusClipRectangle ( oNextPoint );

if ( oNextPoint == Point.Empty )
{
this.oCurrentPoint = oNextPoint;
break;
}

aParams[ 1 ] = new DrawLineEventArgs ( this.oPen, this.oCurrentPoint, oNextPoint );
// Here is an invoke with parameters
this.Invoke ( this.OnDrawLine, aParams );

this.oCurrentPoint = oNextPoint;

if ( i++ == this.iWaveSize )
break;
}
// Here is another without parameters
this.Invoke ( this.OnUpdate );
}

...
// *****************************
// Here comes the invoked methods
// Actually nothing special
private void onDrawLineEventHandler ( Object oSender, DrawLineEventArgs oEventArgs )
{
#region Drawing the line

this.oSignatureGraphics.DrawLine ( oEventArgs.Pen,
oEventArgs.CurrentPoint.X,
oEventArgs.CurrentPoint.Y,
oEventArgs.NextPoint.X,
oEventArgs.NextPoint.Y );

this.oSignatureGraphics.DrawLine ( oEventArgs.Pen,
oEventArgs.CurrentPoint.X + 1,
oEventArgs.CurrentPoint.Y,
oEventArgs.NextPoint.X + 1,
oEventArgs.NextPoint.Y );

this.oSignatureGraphics.DrawLine ( oEventArgs.Pen,
oEventArgs.CurrentPoint.X,
oEventArgs.CurrentPoint.Y + 1,
oEventArgs.NextPoint.X,
oEventArgs.NextPoint.Y + 1 );

#endregion

}

private void onUpdateEventHandler ()
{
base.Invalidate ();
base.Update ();
}
...
}



Have fun

Tamas




Re: Smart Devices VB and C# Projects stopping the thread

mohasad

hi tamas ,
the reply that you posted was much helpful . but still i am confused in stopping the thread . how to stop a thread .

thnxs
sadiq





Re: Smart Devices VB and C# Projects stopping the thread

Zero Dai - MSFT

Dear mohasad,

If your application does need to terminate a thread manually, here are three solutions:

  1. Thread.Abort() Method. It will terminate a thread through raising an exception inside this thread. But, this method is only supported in .net cf 2.0.(Recommended)
  2. Native API TerminateThread() function is used to cause a thread to exit. But, this is a dangerous function. It is suppose to be in the most extreme cases.

[DllImport("coredll.dll")]
public static extern int TerminateThread(int hThread);

3. Native API ExitThread() function can be used to end a thread.

Regards,

Zero Dai - MSFT






Re: Smart Devices VB and C# Projects stopping the thread

mohasad

hi Zero dai ,
so far i am stopping the thread by manually setting the object of the thread to null .but in my case the thread is not stopped .. its still running . is there any other way to stop the thread .

thanks
sadiq





Re: Smart Devices VB and C# Projects stopping the thread

mohasad

hi ,
moreover i been involved in developing application using c# . can u help me in solving the issue using c# .

thanks
sadiq





Re: Smart Devices VB and C# Projects stopping the thread

Michael Koster

Hi Sadiq

In the sample posted by Tamas you'd stop the thread as followed:

Code Snippet

public class SignatureBox : PictureBox, IDisposable
{
...
// In this method will be the thread started
private void prepareWriting ()
{
if ( this.oWritingThread != null )
return;

...
this.oWritingThread = new Thread ( new ThreadStart ( this.writeFromBuffer ) );
this.oWritingThread.Start ();
}

// this methods will stop the thread
private void stopThread ()
{
this.bDisposed = true;
}

// This method runs in the thread
private void writeFromBuffer ()
{
while ( !this.bDisposed )
{
if ( this.qBuffer.Count == 0 )
Thread.Sleep ( this.iSleepPeriod );

// Hier will be the GUI updated
this.drawToGraphics ( );
}
this.bDisposed = false;
this.oWritingThread = null;
}
...
}

I marked the important parts in bold

hope this helps

Michael






Re: Smart Devices VB and C# Projects stopping the thread

mohasad

hi michael ,

thanks . i got the solution from your example .

thanks
sadiq






Re: Smart Devices VB and C# Projects stopping the thread

mohasad

hi michael ,
still the problem exists .
i am fetching the information from network in a separate thread . At the same time i am running the glass animation using forms.timer in main thread . in this thing if i press cancel - softkey , the network fetch i.e. the thread should terminate and the application should return mainscreen . in my case its not happening so . even after i had made the thread object to null reference still the thread is running . how to solve this issue .
thanks
sadiq





Re: Smart Devices VB and C# Projects stopping the thread

Michael Koster

Hi Sadiq

I can't do much without seeing your code. Can you please post the relevant code fragments.

Thanks
Michael






Re: Smart Devices VB and C# Projects stopping the thread

mohasad

hi michael ,
my code is as follows :
public delegate byte[] httpRequest();
puzzlefetching = new Thread(new ThreadStart(docancel));
puzzlefetching.Start();

public void docancel()
{
httpRequest ht = null;
ht += new httpRequest(dorequestHttp);
byte[] data = null;
try
{

data = (byte[])this.Invoke(ht);
}
catch (WebException w)
{



}

}

public byte[] dorequestHttp()
{
// i am fetching the information from network
}

- This code is showing Arguement Exception .. what is the right way of coding ...
moreover if i have to pass String as parameter to dorequestHttp() .

thanks
sadiq











Re: Smart Devices VB and C# Projects stopping the thread

mohasad

hi Ilya ,
i am also confused on battery low conditions . in the conditions when the battery is low , i have to save my applications . at the same time i also want to suspend my application when some incoming call come . how to do this in wm 2003 . this is the main two issues .. could you help me in resolving this issues .
thanks
sadiq





Re: Smart Devices VB and C# Projects stopping the thread

mohasad

hi tilak ,
i am also confused on battery low conditions . in the conditions when the battery is low , i have to save my applications . at the same time i also want to suspend my application when some incoming call come . how to do this in wm 2003 . this is the main two issues .. could you help me in resolving this issues .
thanks
sadiq