|
There are 5 C# articles on this subject at CP. Type "notify icon" in the search box to display a list.
/ravi
|
|
|
|
|
Basically What I want
In my program, two thread running.
Both thread taking method Name from Array and and executing.
Array containning Method Name List.
Only method name from the Array.
problem is when i m assigning like this i m getting error.
Error "strMN is a Variable but it's used like Method"
strMN getting value from the Array.
strMN is Variable Name.
Code :-
string[] strMName = strMethodName.ToString().Split(' ');
Console.WriteLine("Thread running");
for (int i = 0; i < strMName.Length; i++)
{
string strMN = strMName[i].ToString();
// create a thread for the Incrementer
Thread trdIncrementer = new Thread(new ThreadStart(strMN));
trdIncrementer.Start();
// create a thread for the Decrementer.
Thread trdDecrementer = new Thread(new ThreadStart(strMN));
trdDecrementer.Start();
}
Error "strMN is a Variable but it's used like Method"
Methods:-
///
/// demo function for Incrementer, counts up to 10.
///
public void Incrementer()
{
lock (trdIncrementer )
{
for (int counter = 0; counter < 10; counter++)
{
// assign the incrementer value and display the results
Console.WriteLine("Incrementer: {0}", counter);
}
}
}
///
/// demo function for Decrementer, counts down from 10.
///
public void Decrementer()
{
lock (trdDecrementer)
{
for (int counter = 10; counter >= 0; counter--)
{
// assign the decremented value and display the results
Console.WriteLine("Decrementer: {0}", counter);
}
}
}
help me out .
It's arrgent.
Thanks
|
|
|
|
|
ThreadStart does not take string as an input, it takes method name as an input which is delegate. you can do this if you know the methods at compile time.
ThreadStart(Incrementer);
instead of
ThreadStart(strMN);
-- modified at 4:07 Monday 8th January, 2007
or you can do this
new Thread((ThreadStart)ThreadStart.CreateDelegate(typeof(ThreadStart), typeof(ClassName), strMN));
Regards
Shajeel
|
|
|
|
|
thanks for reply
i am using this
new Thread((ThreadStart)ThreadStart.CreateDelegate(typeof(ThreadStart), typeof(ClassName), strMN));
but iam getting "error to bind the target method"
thanks
|
|
|
|
|
first of all check if class name is correct. second check if the function are static.
Regards
Shajeel
|
|
|
|
|
Thanks
Now code working fine.
could you tell me.
I m storing all method name in Array.
In my application having two Thread.
First thread take method name from array and execute.
second thread take method name from array and execute.
if first thread completed execution then it take 3rd method name from the array Otherwise
if second thread completed execution then it take 3rd method name from array.
like that i want to do.
it's arrgent.
Thanks
|
|
|
|
|
The code that you have provided has only one thread. can you show where are you starting two thread.
The object of threads is to do work simultaneously, you should start all methods simultaneously and if they are dependent on each other then you should add some checking mechanism in your functions so that if they cannot proceed then they should sleep for some times.
you can write like this
for(int i=0; i<count; i++)
{
="" start="" all="" thread.
}
a()
{
="" do="" something
}
b()
{
while(a="" not="" concluded)
{
sleep
}
="" something
}
hope="" this="" help=""
<div="" class="ForumSig">Regards
Shajeel
|
|
|
|
|
I m sending my code .
class ThreadTest
{
static void Main(string[] args)
{
StringBuilder strMethodName = new StringBuilder();
XmlDocument xmlDoc = new XmlDocument();
XmlTextReader readerXml = new XmlTextReader(@"D:\Asif\DotNetProjects\ThreadTest\ThreadTest\XSPMethod.xml");
while (readerXml.Read())
{
switch (readerXml.NodeType)
{
case XmlNodeType.Element: // The node is an element.
//Console.Write("<" + readerXml.Name);
//Console.WriteLine(">");
break;
case XmlNodeType.Text: //Display the text in each element.
//Console.WriteLine(readerXml.Value);
strMethodName.Append(readerXml.Value.ToString() + " ");
break;
case XmlNodeType.EndElement: //Display the end of the element.
//Console.Write("");
break;
}
}
ThreadTest t = new ThreadTest();
t.getData(strMethodName);
}
public void getData(StringBuilder strMethodName)
{
string[] strMName = strMethodName.ToString().Split(' ');
Console.WriteLine("Thread running");
for ( int i = 0; i < strMName.Length - 1; i++)
{
string strMN = strMName[i].ToString();
// create a thread for the Incrementer
try
{
Thread trdIncrementer = new Thread((ThreadStart)ThreadStart.CreateDelegate
(typeof(ThreadStart), new ThreadTest(), strMN));
// create a thread for the Decrementer.
Thread trdDecrementer = new Thread((ThreadStart)ThreadStart.CreateDelegate
(typeof(ThreadStart), new ThreadTest(), strMN));
trdIncrementer.Start();
trdDecrementer.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Console.ReadLine();
}
///
/// demo function for Incrementer, counts up to 10.
///
public void Incrementer()
{
lock (this)
{
for (int counter = 0; counter < 10; counter++)
{
// assign the incrementer value and display the results
Console.WriteLine("Incrementer: {0}", counter);
}
}
}
///
/// demo function for Decrementer, counts down from 10.
///
public void Decrementer()
{
lock (this)
{
for (int counter = 10; counter >= 0; counter--)
{
// assign the decremented value and display the results
Console.WriteLine("Decrementer: {0}", counter);
}
}
}
public void Add()
{
lock (this)
{
for (int counter = 0; counter < 10; counter++)
{
// assign the decremented value and display the results
Console.WriteLine("Addition: {0}", counter + 1);
}
}
}
}
|
|
|
|
|
what are the nodes in XML file, it they are Incrementer, Decrementer, and Add
then replace
asif_aslam wrote: for ( int i = 0; i < strMName.Length - 1; i++)
{
string strMN = strMName[i].ToString();
// create a thread for the Incrementer
try
{
Thread trdIncrementer = new Thread((ThreadStart)ThreadStart.CreateDelegate
(typeof(ThreadStart), new ThreadTest(), strMN));
// create a thread for the Decrementer.
Thread trdDecrementer = new Thread((ThreadStart)ThreadStart.CreateDelegate
(typeof(ThreadStart), new ThreadTest(), strMN));
trdIncrementer.Start();
trdDecrementer.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
by
for ( int i = 0; i < strMName.Length - 1; i++)
{
string strMN = strMName[i].ToString();
// create a thread for the Incrementer
try
{
Thread thrd = new Thread((ThreadStart)ThreadStart.CreateDelegate
(typeof(ThreadStart), new ThreadTest(), strMN));
thrd.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
and all methods will run.
Regards
Shajeel
|
|
|
|
|
Thanks for reply.
But can we do same with the help of two thread.
Thanks.
|
|
|
|
|
in your thread there were no two threads, what you were doing was starting all the functions twice and there were six threads. naming thread to increment and decrement does not means you are starting two functions. can you specify what you want as a result exactly.
Regards
Shajeel
|
|
|
|
|
Thanks for reply
I have two thread.
I have more than one method in the array.
I want Two thread should run side by side.
and execute methods.
Suppopse array contains four method name:
1.A
2.B
3.C
4.D
So,
First thread take A method name from the array.
Second thread take B method name from the array.
Once Any of these two thread comleted their task then it has to take next C
method from the array....
Example:-
if suppose 1st thread complete before 2nd thread. it' should take 3rd method name then 2nd thread should not take 3rd method name from the array. it should take 4th method name from the array.
Thanks
|
|
|
|
|
according to this specification i think your code is incorrect. you should do something like that, i am just providing a pseudo code.
static string [] methodlist;
static int currentmethod;
main()
{
}
function X()
{
while(!allmethodscompleted)
{
for calling function from name check out this
<a href = "http://www.codeproject.com/csharp/delegates_and_reflection.asp" rel="nofollow">http:
}
}
Regards
Shajeel
|
|
|
|
|
I want to access child forms save method through mdi toolbar save button.
how can i acesss that method runtime.
because when i call active from's save method it gives me error method or member not define.
Hallo
|
|
|
|
|
you can do parent form's method as public and then
on your chid form
<br />
frmParent frm = new frmParent();<br />
frm.MypublicMethod();<br />
i hope that it is ok for u!
|
|
|
|
|
I think he wants to access a child's method from a parent.
he should do the same as u said but with the child form

|
|
|
|
|
ooppps but the result is same
|
|
|
|
|
yea sure it is the same.
i just posted that for the thread poster to take attention to it.
good job anyways
|
|
|
|
|
Hi,
Its Ok!But i want to decide at runtime which child forms method will run.
Suppose
Form1 is mdi and Form2/from3.... is child;
Form2/form3..... contains save() mehod;
i want active forms save() method
like
Form f=new Form2(); ///Form2 is Currently Active..
f.save();
like
Hallo
|
|
|
|
|
vaibhavnvag wrote: Form f=new Form2(); ///Form2 is Currently Active..
Unless Form is not the base Form class, but one that you defined to use polymorphically, the problem here is that although f is a Form2, you only *know* it's a Form. You need to use reflection, or cast the variable to the right type, before you can see methods that are only on the derived class.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Hello,
First you should build a base Form (BasicForm) from where your Form2 and Form3 inherit.
If save() method in Form2 and Form3 are different:
This BasicForm has than a virtual method save() which is overriden in Form2 and Form3.
If its the same functionality in both forms, you just have to make the method once in BasicForm.
Then in Form1 code you could do:
BasicForm activeForm= this.ActiveMdiChild as BasicForm;
if(activeForm!=null)
{
activeForm.save();
}
Hope that helps!
All the best,
Martin
|
|
|
|
|
Ok! Thank!!!!
My problem is solved.....
Hallo
|
|
|
|
|
I want this statement valid in C#:
Integer i = 0;
In C/C++ you can easily do it by define an alias name for it, for example
#define Integer Int32
But you can not do it in C#. So what should I do?
Thank you for your reading.
|
|
|
|
|
using Integer = System.Int32;
...
Integer i = 0;
Keep in mind that the C# int keyword is an alias for System.Int32.
|
|
|
|
|