Click here to Skip to main content
15,395,897 members
Home / Discussions / C#
   

C#

 
GeneralRe: Active Directory Questions Pin
Mycroft Holmes16-Nov-17 11:36
professionalMycroft Holmes16-Nov-17 11:36 
AnswerRe: Active Directory Questions Pin
BillWoodruff16-Nov-17 20:44
mveBillWoodruff16-Nov-17 20:44 
GeneralRe: Active Directory Questions Pin
Richard MacCutchan16-Nov-17 22:54
mveRichard MacCutchan16-Nov-17 22:54 
GeneralRe: Active Directory Questions Pin
Kevin Marois17-Nov-17 4:39
professionalKevin Marois17-Nov-17 4:39 
AnswerRe: Active Directory Questions Pin
Nathan Minier17-Nov-17 1:36
professionalNathan Minier17-Nov-17 1:36 
Generalsession Pin
Ishi Kaushik16-Nov-17 7:04
MemberIshi Kaushik16-Nov-17 7:04 
AnswerRe: Hello , I am working on three tier architecture based project, i am stuck in writting session code. so please tell me how and where i can write the session code Pin
OriginalGriff16-Nov-17 8:03
mveOriginalGriff16-Nov-17 8:03 
QuestionData type mismatch in criteria expression." in c# Pin
sai.201216-Nov-17 5:54
Membersai.201216-Nov-17 5:54 
AnswerRe: Data type mismatch in criteria expression." in c# Pin
Gerry Schmitz16-Nov-17 6:35
mveGerry Schmitz16-Nov-17 6:35 
AnswerRe: Data type mismatch in criteria expression." in c# Pin
Eddy Vluggen16-Nov-17 7:07
professionalEddy Vluggen16-Nov-17 7:07 
PraiseRe: Data type mismatch in criteria expression." in c# Pin
sai.201216-Nov-17 9:56
Membersai.201216-Nov-17 9:56 
GeneralRe: Data type mismatch in criteria expression." in c# Pin
Eddy Vluggen16-Nov-17 10:33
professionalEddy Vluggen16-Nov-17 10:33 
QuestionLicensing a .NET DLL Pin
P.Gnanaraj16-Nov-17 2:17
MemberP.Gnanaraj16-Nov-17 2:17 
AnswerRe: Licensing a .NET DLL Pin
Richard MacCutchan16-Nov-17 3:20
mveRichard MacCutchan16-Nov-17 3:20 
QuestionAsync method/task issue Pin
primem0ver15-Nov-17 7:21
Memberprimem0ver15-Nov-17 7:21 
I am new to using the "async" functionality in .NET. I have created a few GUI classes that are designed to give feedback to a user while time consuming tasks are completed. The core of this functionality is a "Task" class that makes use of the System.Threading.Tasks.Task class. It contains the "description" of the task being completed, the number of steps to be completed in the task, and the System.Threading.Tasks.Task task that is to be called within an async method. This "API task" that is the actual "task" to be performed amounts to calling a function within the class. The problem is that the creation of my "Task" action is causing a TargetInvocationException. I am getting the impression from the inner exception that the System.Threading.Task.Task is effectively a "null" task and I do not have the experience to understand why. Here is my code inside the async method:

C#
async void OnGenerateMapData()
{
	statusPanel = new StatusPanel();
			
	// add tasks to complete to panel - this causes error
	statusPanel.AddTask(new Task("Processing Map Data", 1081 * 1081, true, new System.Threading.Tasks.Task(() => { ProcessPixelData(); })));
	statusPanel.AddTask(new Task("Generating Shoreline", 1079 * 1079 + 50000, true, new System.Threading.Tasks.Task(() => { ProcessShoreline(); })));
	statusPanel.AddTask(new Task("Updating Map", 1081 * 1081, true, new System.Threading.Tasks.Task(() => { UpdateResult(); })));
	// add panel to UI
	statusPanel.Dock = DockStyle.Fill;
	this.Controls.Add(statusPanel);
	//statusPanel.BringToFront();
			
	Task currentTask = statusPanel.BeginTasks();
	while (currentTask != null)
	{
		await currentTask.Action;
		currentTask = statusPanel.AdvanceCurrentTask(0);
	}

	this.Controls.Remove(statusPanel);
	statusPanel = null;
}


It may help to know that all the function calls being put into the "task" are within the same class as the async method. However, perhaps I need more information (such as a this. before the call)? Or is doing this illegal in its entirety?

EDIT: Just in case it helps... here is my Task class:
C#
public class Task
{
	bool displayAsPercent;
	int currentItem;
	int totalItems;
	object result;

	public System.Threading.Tasks.Task Action { get; private set; }
	public string Description { get; private set; }
	public bool IsComplete { get { return currentItem >= ItemCount; } }
	public int TotalItems { get { return totalItems; } }
	public object Result { get { return result; } }
	public object Baggage { get; private set; }
	public bool Started { get { return currentItem > 0; } }

	public int ItemCount 
	{
		get
		{
			if (displayAsPercent)
				return 100;
			else
				return totalItems;
		}
	}

	public int CompletedValue
	{
		get
		{
			if (displayAsPercent)
				return (int)((float)((float)currentItem / (float)ItemCount) * 100.0f + 0.5f);
			else 
				return currentItem;
		}
	}

	public Task(string description, int stepCount, bool displayAsPercent, System.Threading.Tasks.Task task)
	{
		currentItem = 0;
		Description = description;
		totalItems = stepCount;
		this.displayAsPercent = displayAsPercent;
		Action = task;
	}

	public void Begin(Task previous) { if (previous != null) Baggage = previous.Result; ++currentItem; }
	public void Advance() { ++currentItem; }
	public void Advance(int increase) { currentItem += increase; }
	public void Complete() { currentItem = totalItems; }
	public void Finalize(object result) { this.result = result; }
}


modified 15-Nov-17 13:35pm.

AnswerRe: Async method/task issue Pin
Sascha Lefèvre15-Nov-17 8:33
professionalSascha Lefèvre15-Nov-17 8:33 
GeneralRe: Async method/task issue Pin
primem0ver15-Nov-17 9:52
Memberprimem0ver15-Nov-17 9:52 
GeneralRe: Async method/task issue Pin
Sascha Lefèvre15-Nov-17 10:19
professionalSascha Lefèvre15-Nov-17 10:19 
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 

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.