THE RAZI

Hi i have a scenario where we i have piece of chunck that requires time consuming processing , so i put that chunck into seperate thread (see code below) now the problem is I got UI reponsive as soon as seperate thread starts so whole aplciation takes very little time around 6 seconds to load (prviously it was 5 mins) now the issue i close the Form and then open it again this time it takes bit more time like 8 sec and this continues as i close the form and reopen it it take some extra time. The only ways out is to close the entire application and then re-open it

I am now looking where we can put Abort method or some thing else to free the memoery taken by thread. please let me know what can be done , every one is encourged to email me at razi_rais@yahoo.com

Following is the code

public void Frm_Load()

{

System.Threading.ParameterizedThreadStart startMethod = new System.Threading.ParameterizedThreadStart(HeavyIntializationWork);

System.Threading.Thread thread = new System.Threading.Thread(startMethod);

thread.IsBackground = true;

thread.Start(dtTable);

}

void HeavyIntializationWork(Object objDataTable)

{

try

{

// MessageBox.Show(System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());

DataTable dtTable = objDataTable as DataTable;

InitalizeDataCells(dtTable); // this method do some heavy time consuming task

//Should Abort be placed here

//System.Threading.Thread.CurrentThread.Abort();

}

catch (System.Exception exp)

{

// MessageBox.Show(exp.Message);

}

}




Re: Visual C# Language Regarding Threading in C#

micvos

Do you want the thread to stop when you close the form Then terminate the thread in the close event. If you want the thread to start only once when you start the application, then place the startup code for the thread somewhere else. In the Main method for example.




Re: Visual C# Language Regarding Threading in C#

THE RAZI

I need to kill the thread once form is closed , can you give me code snippset for how to do that






Re: Visual C# Language Regarding Threading in C#

Yazan D

Hi

Placing the Abort where your comments indicate will not work because the thread will be aborted only after InitializeDataCells has finished. A couple of ways to accomplish what you need:

1. Modify your InitializeDataCells to monitor a flag that indicates whether it should stop or continue initializing data cells. This is usually done via a while(bYourFlag) loop

2. If you code logic does not support having a while loop to monitor a flag, then you might need to explicitly abort your thread by calling thread.Abort from your Closing form event. When your thread is aborted, it will raise the ThreadAbortException. Make sure to handle this exception to do any clean up that InitializeDataCells requires.

public void DoSomething()
{
try
{
// Do something lengthy
}
catch( System.Threading.ThreadAbortException ex )
{
// Print time this thread acutally aborted
Trace.WriteLine( "Time worker thread was aborted: " + DateTime.Now.ToString() );
}
}






Re: Visual C# Language Regarding Threading in C#

V.Tortola

Don't lose the threads references when you make multithreading operations.

Make a list with the launch threads and delete it from list whet it ends.

For example:



List<Thread> lauchThreads = new List<Thread>();

Thread t = new Thread(ThreadStart(MyMethod));
t.Start();
launchThreads.Add(t);

 


When the thread finish, you must raise a event or something to delete the item in the list.

When you want close the app, you can iterate over the list and abort all of pending threads, for example:



foreach(Thread t in launchThreads) t.Abort();

 


I do somethin like this, but with asyncrhonous operations, always cache the IAsyncResult to always have localized and know how many backgroud threads i have.

Is a option, i hope that it be usefull for you.

Regards.





Re: Visual C# Language Regarding Threading in C#

THE RAZI

I able to done it in different way. Thanks anyways.




Re: Visual C# Language Regarding Threading in C#

V.Tortola

Put the way





Re: Visual C# Language Regarding Threading in C#

Matthew Watson

Thread.Abort() is the work of the devil!

Seriously, avoid it like the plague. Calling it can leave things in the most hideous of states!