Threading Question. Passing parameters to a function.

  • Thread starter Thread starter rollasoc
  • Start date Start date
R

rollasoc

Hi,

Having a slight problem with how to pass parameters to functions between
threads.

I have a form with a progress bar and a description label on it. It has a
public function
IncrementProgressBar() that just calls performstep on the progress bar.


On my main form I create a thread which creates the progress bar form

Then using

MethodInvoker mi = new MethodInvoker(m_ProgressBar.IncrementProgressBar);

this.BeginInvoke(mi);

I can get this function called wherever is appropriate.

All is fine. But I want a function that takes a string as a parameter so I
can dynamically update the description.

Any idea how to do this? I've searched google and found nothing so far.

I've tried

MethodInvoker mi = new MethodInvoker(m_ProgressBar.UpdateDescription);

object[] args = {m_txtUpdateDescription.Text};

this.BeginInvoke(m_ProgressBar.UpdateTitle, args);

but the first line doesn't compile.



Rollasoc
 
Sorry cut and pasted the wrong bit of code. (which obviously doesn't work)

It shoul have been
I've tried

MethodInvoker mi = new MethodInvoker(m_ProgressBar.UpdateDescription);

object[] args = {m_txtUpdateDescription.Text};

this.BeginInvoke(m_ProgressBar.UpdateDescription, args);

but the first line doesn't compile.
 
I don't understand why you need to use MethodInvoke in order to call your
function (It seems you are trying a combination of threading and
reflection). You could use something like the next sample, where I am using
the member variable "someMessage" as a way of passing the string to the new
thread. Perhaps you will have to add some synchronization to the
someMessage variable.


public class MyClass
{
private string someMessage;

public void DoSomething()
{
//....
this.someMessage = "Some Message";
new Thread(new ThreadStart(this.MyThreadFunction())).Start();

}

public void MyThreadFunction()
{
DoSomething(this.someMessage);
}
}

You could also consider sending some notifications to update the
slider/message.
Sorry cut and pasted the wrong bit of code. (which obviously doesn't work)

It shoul have been
I've tried

MethodInvoker mi = new MethodInvoker(m_ProgressBar.UpdateDescription);

object[] args = {m_txtUpdateDescription.Text};

this.BeginInvoke(m_ProgressBar.UpdateDescription, args);

but the first line doesn't compile.


rollasoc said:
Hi,

Having a slight problem with how to pass parameters to functions between
threads.

I have a form with a progress bar and a description label on it. It has a
public function
IncrementProgressBar() that just calls performstep on the progress bar.


On my main form I create a thread which creates the progress bar form

Then using

MethodInvoker mi = new MethodInvoker(m_ProgressBar.IncrementProgressBar);

this.BeginInvoke(mi);

I can get this function called wherever is appropriate.

All is fine. But I want a function that takes a string as a parameter so I
can dynamically update the description.

Any idea how to do this? I've searched google and found nothing so far.

I've tried

MethodInvoker mi = new MethodInvoker(m_ProgressBar.UpdateDescription);

object[] args = {m_txtUpdateDescription.Text};

this.BeginInvoke(m_ProgressBar.UpdateTitle, args);

but the first line doesn't compile.



Rollasoc

Adrian Vinca [MSFT], Developer Division
--------------------------------------------------------------------
This reply is provided "AS IS", without warranty (express or implied).

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 
Your right. There was no need really. I was going on what few articles I
could find. I have since found some very useful articles on the microsoft
site.

Completely sorted the whole thing with delegates and using BeginInvoke on
them.

Works great now.

Rollasoc


Adrian Vinca said:
I don't understand why you need to use MethodInvoke in order to call your
function (It seems you are trying a combination of threading and
reflection). You could use something like the next sample, where I am using
the member variable "someMessage" as a way of passing the string to the new
thread. Perhaps you will have to add some synchronization to the
someMessage variable.


public class MyClass
{
private string someMessage;

public void DoSomething()
{
//....
this.someMessage = "Some Message";
new Thread(new ThreadStart(this.MyThreadFunction())).Start();

}

public void MyThreadFunction()
{
DoSomething(this.someMessage);
}
}

You could also consider sending some notifications to update the
slider/message.
Sorry cut and pasted the wrong bit of code. (which obviously doesn't work)

It shoul have been
I've tried

MethodInvoker mi = new MethodInvoker(m_ProgressBar.UpdateDescription);

object[] args = {m_txtUpdateDescription.Text};

this.BeginInvoke(m_ProgressBar.UpdateDescription, args);

but the first line doesn't compile.


rollasoc said:
Hi,

Having a slight problem with how to pass parameters to functions between
threads.

I have a form with a progress bar and a description label on it. It
has
a
public function
IncrementProgressBar() that just calls performstep on the progress bar.


On my main form I create a thread which creates the progress bar form

Then using

MethodInvoker mi = new MethodInvoker(m_ProgressBar.IncrementProgressBar);

this.BeginInvoke(mi);

I can get this function called wherever is appropriate.

All is fine. But I want a function that takes a string as a parameter
so
I
can dynamically update the description.

Any idea how to do this? I've searched google and found nothing so far.

I've tried

MethodInvoker mi = new MethodInvoker(m_ProgressBar.UpdateDescription);

object[] args = {m_txtUpdateDescription.Text};

this.BeginInvoke(m_ProgressBar.UpdateTitle, args);

but the first line doesn't compile.



Rollasoc

Adrian Vinca [MSFT], Developer Division
--------------------------------------------------------------------
This reply is provided "AS IS", without warranty (express or implied).

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 
Back
Top