Click here to Skip to main content
15,396,078 members
Home / Discussions / C#
   

C#

 
AnswerRe: Async method/task issue Pin
Gerry Schmitz16-Nov-17 4:24
mveGerry Schmitz16-Nov-17 4:24 
GeneralRe: Async method/task issue Pin
primem0ver16-Nov-17 10:57
Memberprimem0ver16-Nov-17 10:57 
GeneralRe: Async method/task issue Pin
Gerry Schmitz16-Nov-17 11:13
mveGerry Schmitz16-Nov-17 11:13 
GeneralRe: Async method/task issue Pin
primem0ver28-Nov-17 13:41
Memberprimem0ver28-Nov-17 13:41 
GeneralRe: Async method/task issue Pin
Gerry Schmitz29-Nov-17 4:29
mveGerry Schmitz29-Nov-17 4:29 
GeneralRe: Async method/task issue Pin
primem0ver2-Dec-17 13:30
Memberprimem0ver2-Dec-17 13:30 
GeneralRe: Async method/task issue Pin
Gerry Schmitz2-Dec-17 13:46
mveGerry Schmitz2-Dec-17 13:46 
GeneralRe: Async method/task issue Pin
primem0ver3-Dec-17 1:13
Memberprimem0ver3-Dec-17 1:13 
Ok. But I have already done all that. The UI class that manages the worker (the "adapter" as you say) is the same custom control class that manages the tasks (represented by the "Task" class) and displays progress. It has a method "AddTask" that sets up the background worker like this:

C#
public void AddTask(Task task) 
{ 
	tasks.Add(task); // tasks is a List<Task> object
	counterText = counterTemplate.Replace("#2", tasks.Count.ToString());
	task.BackgroundWorker.WorkerReportsProgress = true;
	task.BackgroundWorker.ProgressChanged += OnTaskProgressChanged;
	task.BackgroundWorker.RunWorkerCompleted += OnTaskCompleted;
}

(The DoWork event handler is set up in the Task class constructor so it has already been assigned by the time the above method is called). Assuming by "completed" you are referring to the RunWorkerCompleted event, as you can see, I have a handler (shown below). That handler makes it so that the BackgroundWorker method for the task is set to null (by calling the task's "InvalidateWorker" method also shown below). This handler is NOT being called before the Task::End() code I posted above.

The OnTaskCompleted method (part of the custom control with the AddTask method above):
C#
void OnTaskCompleted(object sender, RunWorkerCompletedEventArgs e)
{
	tasks[currentIndex].InvalidateWorker(); // break point on this line
	NextTask();
}

The InvalidateWorker() method in the Task class (I suppose I could dispose the BackgroundWorker here if necessary):
C#
public void InvalidateWorker()
{
	BackgroundWorker = null;
}

I have a break on the line where the OnTaskCompleted method calls the InvalidateWorker method that is not reached before the End method I posted previously is called (from the OnTaskProgressChanged method if the UI detects that the progress bar has reached its last step). So I am having a hard time understanding why the background worker is "completing" without invoking the event handler for it. I am also assuming that because it is "busy" it hasn't officially completed the call.

The only explanation I can think of is that the "completed" message is queued in the message queue/loop behind the "progress" message that detected the work was technically completed and that the background worker is shut down before "completed" event is invoked. If so, that is just bad design on the part of Microsoft. I figured though that the "IsBusy" would indicate that this is the case (see my End() method in the previous post); but apparently that isn't the case either... which makes Microsoft's code even worse.

modified 3-Dec-17 7:28am.

GeneralRe: Async method/task issue Pin
primem0ver3-Dec-17 1:46
Memberprimem0ver3-Dec-17 1:46 
GeneralRe: Async method/task issue Pin
Gerry Schmitz3-Dec-17 3:17
mveGerry Schmitz3-Dec-17 3:17 
GeneralRe: Async method/task issue Pin
primem0ver3-Dec-17 4:03
Memberprimem0ver3-Dec-17 4:03 
GeneralRe: Async method/task issue Pin
Gerry Schmitz3-Dec-17 4:41
mveGerry Schmitz3-Dec-17 4:41 
GeneralRe: Async method/task issue Pin
primem0ver4-Dec-17 2:12
Memberprimem0ver4-Dec-17 2:12 
GeneralRe: Async method/task issue Pin
Gerry Schmitz4-Dec-17 6:50
mveGerry Schmitz4-Dec-17 6:50 
GeneralRe: Async method/task issue Pin
primem0ver3-Feb-18 23:03
Memberprimem0ver3-Feb-18 23:03 
GeneralRe: Async method/task issue Pin
Gerry Schmitz4-Feb-18 7:05
mveGerry Schmitz4-Feb-18 7:05 
Questionin c# school management project Pin
Member 1289859014-Nov-17 19:51
MemberMember 1289859014-Nov-17 19:51 
AnswerRe: in c# school management project Pin
OriginalGriff14-Nov-17 20:11
mveOriginalGriff14-Nov-17 20:11 
AnswerRe: in c# school management project Pin
Gerry Schmitz15-Nov-17 5:41
mveGerry Schmitz15-Nov-17 5:41 
AnswerRe: in c# school management project Pin
jschell15-Nov-17 7:03
Memberjschell15-Nov-17 7:03 
QuestionDetermine Distance between HD Camera and Object Pin
Member 1041074814-Nov-17 4:09
MemberMember 1041074814-Nov-17 4:09 
AnswerRe: Determine Distance between HD Camera and Object Pin
Richard MacCutchan14-Nov-17 4:51
mveRichard MacCutchan14-Nov-17 4:51 
AnswerRe: Determine Distance between HD Camera and Object Pin
Dave Kreskowiak14-Nov-17 5:24
mveDave Kreskowiak14-Nov-17 5:24 
AnswerRe: Determine Distance between HD Camera and Object Pin
Gerry Schmitz15-Nov-17 5:39
mveGerry Schmitz15-Nov-17 5:39 
Questionmy projects link button not work in iphone 6s but desktop work properly why? Pin
Member 1352056914-Nov-17 2:27
MemberMember 1352056914-Nov-17 2:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.