Click here to Skip to main content
15,896,606 members

Comments by Martijn Kok (Top 54 by date)

Martijn Kok 21-Jan-14 2:24am View    
Hello Rahul,

It's actual a good question. I'm not sure if my technical knowledge is good enough to explain it clearly, but I hope my explanation will give some indication for you to search for more information on this topic.

A thread is more or less a seperate piece of executing code. Every program has at least one thread in which the program executes. Each thread has exactly one current line of execution (the yellow line when you are debugging your code in Visual studio). A thread does more than that. I also does something with memory management (I don't know the details). A thread owns the objects which are created in that thread.

Your code can run more than 1 thread. When your start a BackgroundWorker a new thread is created. Not Always the threads know of each other existance. Their life time might be different. In this case of the BackgroundWorker the background worker only exists as long as the background worker executes. As soon as the backgroundworker is completed is stops existing.

This might create a problem in memory management. When the backgroundworker dies, the objects it owns die too. If it was allowed that one thread sets object in another thread this would cause a problem.

There are several solutions to this problem, which all involve calling the other thread first an than setting the object. This can be done using an Invoke (in WinForms). WPF uses something called the Dispatcher object. In the background worker you can use the ProgressChanged event.
Martijn Kok 15-Nov-12 4:38am View    
Hi Vasanth,

I'll send it in a view. The playlist part of the code is missing. I've never seen the actual code behind the ListView (I never really used a ListView in programs I wrote).

Due to the missing of part of the code, I have made guesses about parts, but I think you could fit it into your code.

Regards
Martijn
Martijn Kok 13-Nov-12 0:40am View    
Hi Vasanth,

My mistake. Ater calling DoWaiting, I didn't break the execution of the code (which would result in calling the Thread.Sleep(2000) and thus waiting for 2 more seconds). Change the code to

ResetTime();
if (DoWaiting())
{
e.Cancel = true;
break;
}

Regards

Martijn
Martijn Kok 12-Nov-12 11:39am View    
Hi Vasanth,

I think I have a solution. I can't test it though and thus I'm not sure if it will solve issue 1.

First of all add the following code:

private int timeInMilliSecondsRemaining = 0; //Initialize the time at 0.

private bool DoWaiting()
{
while (timeInMilliSecondsRemaining > 0)
{
Thread.Sleep(200); // 200 milliseconds
timeInMilliSecondsRemaining -= 200; // substract waited time from timeremaining
if (backgroundWorker1.CancellationPending)
{
return true;
}
}

timeInMilliSecondsRemaining = 0;
return false;
}

private void ResetTime()
{
// Only reset time if the current time has been finished, otherwise resume
if (timeInMilliSecondsRemaining <= 0)
timeInMilliSecondsRemaining = Convert.ToInt32(listview_playlist.SelectedItems[0].Tag.ToString());
//timeInMilliSecondsRemaining = Convert.ToInt32(txtboxSleep.Text);
}

Secondly change part of the loop into:

backgroundWorker1.ReportProgress(i * 100 / count, j);
//Thread.Sleep(2000);
//if (backgroundWorker1.CancellationPending)
//{
// e.Cancel = true;
// break;
//}
ResetTime();
e.Cancel = DoWaiting();

The code does the following. To pause you need something to pause. In the former code the backgroundworker did wait the required seconds and then checked to see if a cancellation was pending. So it was inpossible to stop halfway. The DoWaiting method checks every 0.2 seconds if a cancellation is pending.

To check the time remaining I added a int with the time remaining in milliseconds. When the time remaining reaches 0, the waiting will be finish. DoWaiting returns true if the pause button has been hit (assuming that your pausebutton will cancel the background worker).

To set the time remaining I added the ResetTime method. It will take the time form the playlist (you might need to mulitply the seconds with 1000, i forget that).

Concerning Issue 1 this also might be solved. When you push the start button the reset time function will check if there was a time remaining and the code will continue where it left. If you don't want this funcionality, then remove the 'if (timeInMilliSeccondsRemaing <= 0)'

I hope it will work

Regards

Martijn
Martijn Kok 12-Nov-12 10:27am View    
Deleted
Hi Vasanth,

I think i have a solution, although I'm not sure that is solves issue 1. Unfortunately I can't test the code.

First of all add the following code

<pre lang="c#">
private static int timeInMilliSecondsRemaining = 0; //Initialize the time at 0.

private bool DoWaiting()
{
while (timeInMilliSecondsRemaining > 0)
{
Thread.Sleep(200); // 200 milliseconds
timeInMilliSecondsRemaining -= 200; // substract waited time from timeremaining
if (backgroundWorker1.CancellationPending)
{
return true;
}
}

timeInMilliSecondsRemaining = 0;
return false;
}

private void ResetTime()
{
// Only reset time if the current time has been finished, otherwise resume
if (timeInMilliSecondsRemaining <= 0)
timeInMilliSecondsRemaining = Convert.ToInt32(listview_playlist.SelectedItems[0].Tag.ToString());
//timeInMilliSecondsRemaining = Convert.ToInt32(txtboxSleep.Text);
}
</pre>