H7 Multithreading and Asynchronous Processing Flashcards
Name at least 3 threadpool methodes:
QueueUserWorkItem GetAvailableThreads GetMaxThreads GetMinThreads SetMaxThreads SetMinThreads
What is the most importend methode of the Threadpool.
public static bool QueueUserWorkItem(WaitCallback callBack)
public static bool QueueUserWorkItem(WaitCallback callBack, Object state)
public delegate void WaitCallback(Object state)
ThreadPool.QueueUserWorkItem((x) => result += ReadDataFromIO());
Is there a join methode in a threadpool?
No, you have to wait until it is finished.
Which steps do you take for a backgroundworker thread?
DoWork += DoWorkEventHandler methode
when work is done assign the result to : DoWorkEventArgs.Result
RunWorkerCompleted += RunWorkerCompletedEventHandler methode
Check RunWorkerCompletedEventArgs.Error
Optionaly :
WorkerReportsProgress = true
ProgressChanged += ProgressChangedEventHandler methode
start : BackgroundWorkerInstance.RunWorkerAsync()
Give an example of Invoke.
Invoke means : put this methode pointer + (optional argument) in the queue of the thread of the e.g. UI.
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
if (this.InvokeRequired) { this.Invoke( new Action(UpdateLabel), e.Result.ToString()); //this.Invoke(Delegate methode, arguments that are passed to the delegate methode.)
}
else {
UpdateLabel(e.Result.ToString());
}
}
private void UpdateLabel(string text) {
lblResult.Text = text;
}
How do you tell that you are done with the thread?
Thread.Sleep(0);
What is the difference between a foreground and a background thread and how do you make a background thread?
A forground thread blocks the main application thread for exiting until it is finished. A Background thread gets killed if the main application finishes.
MyThreadInstance.IsBackground = true;
What delegate do you use for an instance of a Thread?
Thread myThread = new Thread(NewTreadStart(MethodeName));
How can we give thread controll up for e.g. 200 ms in a loop?
while(true)
{
Task.Sleep(200)
…
}
What delegate do you use for an instance of a Thread with parameters?
private void MytestMethode(object myValue)
{
} private void button1_Click(object sender, EventArgs e) { Thread t = new Thread(new ParameterizedThreadStart(MytestMethode)); t.Start("Peter"); }
How do you make a static field per thread? So 5 threads each having an individual static (non shared) field?
[ThreadStatic]
public static int mystaticvar;
What is actualy a task?
It is a managed threadpool thread.
How can you start a task?
Task.Run(delegate);
How do you tell a task that it uses a action delegate when giving the methode name?
Task t = Task.Run(action:MyMethodeName);
What is a schedular?
The component that places a task in the thread queue.