When I need to invoke some code in the specified thread, i am using something like this:
Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
delegate void MethodToInvokeDelegate(string foo, int bar);
void MethodToInvoke(string foo, int bar)
{
DoSomeWork(foo);
DoMoreWork(bar);
}
void SomeMethod()
{
string S = "Some text";
int I = 1;
dispatcher.BeginInvoke(new MethodToInvokeDelegate(MethodToInvoke), new object[] {S, I});
}
This code works fine, but it's quite heavy. I'd like to make it without declaring MethodToInvoke and MethodToInvokeDelegate - using an anonymous method. But I can't figure out how to pass parameters to it.
I can't write this like:
dispatcher.BeginInvoke((Action)delegate() { DoSomeWork(S); DoMoreWork(I); });
I need to actually pass parameters to method.
Is it any way to write it short and simple?
Example:
Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
int[] ArrayToFill = new int[3];
void SomeMethod()
{
for (int i = 0; i < 3; i++)
dispatcher.BeginInvoke( { ArrayToFill[i] = 10; } );
}
This code will not work: method will be called with i = 1, 2, 3 and will raise IndexOutOfRange exception. i will be incremented before method begins to execute. So we need to rewrite it like:
Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
int[] ArrayToFill = new int[3];
delegate void MethodToInvokeDelegate(int i);
void MethodToInvoke(int i)
{
ArrayToFill[i] = 10;
}
void SomeMethod()
{
for (int i = 0; i < 3; i++)
dispatcher.BeginInvoke(new MethodToInvokeDelegate(MethodToInvoke), new object[] {i});
}
if you wish to avoid creating a delegate type for each call, make use of Action<>, Action<,>, etc
Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
string S = "Some text";
int I = 1;
dispatcher.BeginInvoke(
(Action<string, int>)((foo, bar) =>
{
MessageBox.Show(bar.ToString(), foo);
//DoSomeWork(foo);
//DoMoreWork(bar);
}),
new object[] { S, I }
);
an example with ArrayToFill[j] = 10; action can be fixed very simple:
for (int i = 0; i < 3; i++)
{
int j = i;
// lambda captures int variable
// use a new variable j for each lambda to avoid exceptions
dispatcher.BeginInvoke(new Action(() =>
{
ArrayToFill[j] = 10;
}));
}
Try this:
string S = "Some text";
int I = 1;
dispatcher.BeginInvoke(new Action(() =>
{
DoSomeWork(S);
DoMoreWork(I);
}));
[EDIT]
In response to your modified question:
The issue you are seeing there is modified closure problem.
To fix it, you merely need to copy the argument before invoking the method:
Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
int[] ArrayToFill = new int[3];
for (int i = 0; i < 3; i++)
{
int index = i;
dispatcher.BeginInvoke(new Action(() => { ArrayToFill[index] = 10; } ));
}
yourUIcontrol.BeginInvoke(new MethodInvoker(delegate {
//your code goes here
}));
Related
I wrote the following code in c# and after a lot of tries I still face the same error in the commented line
error message: "method name expected"
so please can you tell me the appropriate way to send the "tmp" variable for my LoadSpeceficSontrol function.
any help is appreciated in advance.
private void AddBox()
{
Thread thread= new Thread(() => LoadControls());
thread.Start();
}
delegate void LoadControlsCallback();
private void LoadControls()
{
int Nu = int.Parse(PageNumber.Text);
for (int i = (Nu - 1) * 100; i < Nu * 200 && i < MoviesList.Count; i++)
{
MovieControl tmp = new MovieControl(MoviesList[i]);
if (tmp.InvokeRequired || MoviesFlowPanel.InvokeRequired)
{
LoadControlsCallback d = new LoadControlsCallback(LoadSpeceficControl, new object[] {tmp}); // error here
this.Invoke(d);
}
else
MoviesFlowPanel.Controls.Add(tmp);
}
}
private void LoadSpeceficControl(MovieControl tmp)
{
MoviesFlowPanel.Controls.Add(tmp);
}
Code Version 2.0
private void AddBox()
{
Thread thread= new Thread(() => LoadControls());
thread.Start();
}
private void LoadControls()
{
int Nu = int.Parse(PageNumber.Text);
for (int i = (Nu - 1) * 100; i < Nu * 200 && i < MoviesList.Count; i++)
{
MovieControl tmp = new MovieControl(MoviesList[i]);
if (tmp.InvokeRequired || MoviesFlowPanel.InvokeRequired)
{
this.Invoke((MethodInvoker)delegate { MoviesFlowPanel.Controls.Add(tmp); });
}
else
MoviesFlowPanel.Controls.Add(tmp);
tmp = null;
}
}
Should be something like this:
delegate void LoadControlsCallback(MovieControl tmp);
LoadControlsCallback d = LoadSpeceficControl;
d(tmp);
Your delegate definition of LoadControlsCallback should match signature. Like from how you using, it should look like:
delegate void LoadControlsCallback(Action<MovieControl> action, object[] objarray);
I have the following code:
void Selection()
{
bool solutionFound = false;
int generation = 0;
int distance = int.MaxValue;
while(!solutionFound)
{
generation++;
for (int i = 0; i < population.Length; i++)
{
population[i].CalcFitness(target);
}
matingPool = new List<DNA>();
for (int i = 0; i < population.Length; i++)
{
int n = (int)(population[i].fitness * 100);
for (int j = 0; j < n; j++)
{
matingPool.Add(population[i]);
}
}
for (int i = 0; i < population.Length; i++)
{
int a = StaticRandom.Instance.Next(matingPool.Count);
int b = StaticRandom.Instance.Next(matingPool.Count);
DNA partnerA = matingPool[a];
DNA partnerB = matingPool[b];
if(partnerA.GetPhrase().Equals(partnerB.GetPhrase()))
{
while(partnerA.GetPhrase().Equals(partnerB.GetPhrase()))
{
a = StaticRandom.Instance.Next(matingPool.Count);
b = StaticRandom.Instance.Next(matingPool.Count);
partnerA = matingPool[a];
partnerB = matingPool[b];
}
}
DNA child = partnerA.Crossover(partnerB);
child.Mutate(mutationRate);
population[i] = child;
ThreadHelperClass.SetText(this, display_lbl, i + ": " + child.GetPhrase());
ThreadHelperClass.SetText(this, gen_lbl, "Generations: " + generation);
int compute = LevenshteinDistance.Compute(target, child.GetPhrase());
if(compute < distance)
{
distance = compute;
ThreadHelperClass.SetText(this, bestPhrase_lbl, "Best Phrase: " + child.GetPhrase());
}
if(child.GetPhrase().Equals(target))
{
solutionFound = true;
break;
}
}
}
}
and the following code
public static class ThreadHelperClass
{
delegate void SetTextCallback(Form f, Control ctrl, string t);
public static void SetText(Form form, Control control, string text)
{
//Invoke requires compares the thread ID of the calling thread
//to the thread ID of the creating thread
//If they are different InvokeRequired sets to true
if (control.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
form.Invoke(d, new Object[] { form, control, text });
}
else
control.Text = text;
}
}
I send Selection to another thread using:
Task.Factory.StartNew(() => Selection(), cToken.Token);
I've already tried to set the ThreadHelper class to use BeginInvoke instead of Invoke and it locks up my UI on the first iteration of the Selection method.
I've also tried to end the task using cToken.Cancel(). This still locks up my UI when the user clicks the stop_btn.
Is there a way I can stop this from happening?
I have a for loop and when the loop is being processed, I cant access any other function or event like clicking button it doesn't work till the for loop ends. Is there any way to overcome this Issue and hope I can get answer soon.
for (int i = 0; i < sizes - 2; i++)
{
if (pictureBox1.Image != null)
{
trackBar1.Value = trackBar1.Value + 1;
DisplayImage(_image);
}
}
Thanks in advance.
hi if you using framework 4.5
you can to the next :
Task.Run(() =>
{
for (int i = 0; i < sizes - 2; i++)
{
if (pictureBox1.Image != null)
{
trackBar1.Value = trackBar1.Value + 1;
DisplayImage(_image);
}
}
});
if not you can try this using thread :
Thread thread = new Thread(NewMethod);
thread.Start();
private void NewMethod()
{
for (int i = 0; i < sizes - 2; i++)
{
if (pictureBox1.Image != null)
{
trackBar1.Value = trackBar1.Value + 1;
DisplayImage(_image);
}
}
}
you can upgrade but you need to do it with delegate try this if you have cross thread operation error when update ui :
create delegate void function
delegate void Function();
then in your for make this :
Invoke(new Function(delegate()
{
label.text = "some text" ;
}));
This example shows how to create a new thread in .NET Framework. First, create a new ThreadStart delegate. The delegate points to a method that will be executed by the new thread. Pass this delegate as a parameter when creating a new Thread instance. Finally, call the Thread.Start method to run your method (in this case WorkThreadFunction) on background.
using System.Threading;
Thread thread = new Thread(new ThreadStart(WorkThreadFunction));
thread.Start();
The WorkThreadFunction could be defined as follows.
public void WorkThreadFunction()
{
try
{
// do any background work
}
catch (Exception ex)
{
// log errors
}
}
I am trying to learn Threading in .Net.
Many of you must have seen this:
private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(loop));
t.Start();
}
private void loop()
{
for (int i = 0; i < 100000; i++)
{
textBox1.Text = i.ToString();
}
}
It works fine, but what if my loop method has parameters in it, like:
private void loop(string str)
{
for (int i = 0; i < 100000; i++)
{
textBox1.Text = i + str;
}
}
Then how to call this method in my ThreadStart as ThreadStart accepts just the method name. Then how to call loop method in a different thread?
Thread t = new Thread(new ParameterizedThreadStart(loop));
t.Start("Hello world");
private void loop(object obj)
{
string str = (string)obj;
for (int i = 0; i < 100000; i++)
{
// Don't do this: you can't change a control from another thread. Danger Will Robinson!
textBox1.Text = i + str;
}
}
Note that the loop method must accept an object parameter, so you'll have to upcast the object to your type. If you don't want, you can use a closure and an anonymous method:
string str = "Hello world";
Thread t = new Thread(() => {
for (int i = 0; i < 100000; i++)
{
// Don't do this: you can't change a control from another thread. Danger Will Robinson!
textBox1.Text = i + str;
}
});
t.Start();
In this way the anonymous method will "close" around str and it will be similar as if you had passed the parameter. Similar because there are differences/problems on closing variables. In reality I would write something similar to:
string str = "Hello world";
{
string str2 = str;
Thread t = new Thread(() => {
for (int i = 0; i < 100000; i++)
{
// Don't do this: you can't change a control from another thread. Danger Will Robinson!
textBox1.Text = i + str2;
}
});
t.Start();
}
so that no one else can "touch" str2.
If you need I can find some answer on SO that explain this "problem"
You'd use ParameterizedThreadStart instead: http://msdn.microsoft.com/en-us/library/system.threading.parameterizedthreadstart.aspx
Thread t = new Thread(new ParameterizedThreadStart(loop));
t.Start("Foo");
// Note the use of Object here to match the delegate signature
private void loop(Object state)
{
var str = state as String;
for (int i = 0; i < 100000; i++)
{
// For what it is worth, this is illegal:
// textBox1.Text = i + str;
// You need to Invoke back to the UI thread to access a control's properties:
textBox1.Invoke(()=> { textBox1.Text = i + str; });
}
}
There is a ParameterizedThreadStart class that Delegates with a single parameter can be cast to when instantiating a Thread:
private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ParameterizedThreadStart(loop));
t.Start(str);
}
private void loop(string str)
{
for (int i = 0; i < 100000; i++)
{
//the code you had is a no-no when you are multithreading;
//all UI updates must occur on the main thread
//textBox1.Text = i + str;
UpdateTextBoxText(textBox1, i+str);
}
}
private void UpdateTextBoxText(TextBox textBox, string text)
{
//the method will invoke itself on the main thread if it isn't already running there
if(InvokeRequired)
{
this.Invoke((MethodInvoker)(()=>UpdateTextBoxText(TextBox textBox, string text)));
return;
}
textBox.Text = text;
}
If you don't need very fine-grained control over when the thread starts and stops, you can leave it to the ThreadPool and use Delegate.BeginInvoke:
private void button1_Click(object sender, EventArgs e)
{
Action<string> method = loop;
method.BeginInvoke(str, null, null);
}
private void loop(string str)
{
for (int i = 0; i < 100000; i++)
{
//textBox1.Text = i + str;
UpdateTextBoxText(textBox1, i+str);
}
}
private void UpdateTextBoxText(TextBox textBox, string text)
{
//the method will invoke itself on the main thread if it isn't already running there
if(InvokeRequired)
{
this.Invoke((MethodInvoker)(()=>UpdateTextBoxText(textBox, text)));
return;
}
textBox.Text = text;
}
Like this:
new Thread(() => loop("MyString")).Start();
You don't even have to mess with ThreadStart/ParameterizedThreadStart.
Take a look at ParameterizedThreadStart, it allows you to pass parameters into your thread start function.
Basic Goal is that i have four progress bar and want to run them at once as button is pressed and i donot have to use background worker have to do by this.
var t = new Thread(() =>
{
try
{
}
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
I tried and codded
for (i = 0; i < 4; i++)
{
var t = new Thread(() =>
{
for (double x = 0; x < 10000; x = x + 0.5)
{
progressVal=(int)x;
this.Invoke(new EventHandler(ProgressBar));
Thread.Sleep(2);
}
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
private void ProgressBar(object sender, EventArgs e)
{
progressBar1.Value=progressVal;
}
but cannot think of idea how to manuplate other progress bars
I would put the progress bars into an array:
var pBars = new[] { progressBar1, progressBar2, progressBar3, progressBar4 };
foreach (var pBar in pBars)
{
new Thread(currentPBar =>
{
for (double x = 0; x < 10000; x = x + 0.5)
{
var progress = (int)x;
Action<ProgressBar, int> del = UpdateProgress;
Invoke(
del,
new object[] { (ProgressBar)currentPBar, progress }
);
Thread.Sleep(2);
}
}).Start(pBar);
}
and the UpdateProgress method:
private void UpdateProgress(ProgressBar pBar, int progress)
{
pBar.Value = progress;
}
This being said, using a BackgroundWorker is far more adapted to your scenario.