2011-01-02 10 views
13

की आवश्यकता है मेरे पास एक सूची बॉक्स प्रदर्शित करने वाले तरीकों (विशेषता द्वारा) के साथ एक जीत फ़ॉर्म ऐप है। मैं सूची बॉक्स के चयनित मूल्य से विधि जानकारी प्राप्त करने के लिए प्रतिबिंब का उपयोग करके, धागे में विधियों को गतिशील रूप से आमंत्रित करने का प्रयास कर रहा हूं। हालांकि, पर कॉल करते समय Methodinfo.Invoke मुझे यह आंतरिक अपवाद मिल रहा है "गैर स्थैतिक विधि को लक्ष्य C# की आवश्यकता है"।गैर स्थैतिक विधि के लिए एक लक्ष्य C#

यहाँ मेरी कोड है (ध्यान में रखना मैं अभी भी ग # और सामान्य रूप में प्रोग्रामिंग करने के लिए नया हूँ।)

private void PopulateComboBox() 
{//Populates list box by getting methods with declared attributes 
    MethodInfo[] methods = typeof(MainForm).GetMethods(); 

    MyToken token = null; 
    List<KeyValuePair<String, MethodInfo>> items = 
     new List<KeyValuePair<string, MethodInfo>>(); 

    foreach (MethodInfo method in methods) 
    { 
     token = Attribute.GetCustomAttribute(method, 
      typeof(MyToken), false) as MyToken; 
     if (token == null) 
      continue; 

     items.Add(new KeyValuePair<String, MethodInfo>(
      token.DisplayName, method)); 

    } 

    testListBox.DataSource = items; 
    testListBox.DisplayMember = "Key"; 
    testListBox.ValueMember = "Value"; 
} 

public void GetTest() 
{//The next two methods handle selected value of the listbox and invoke the method. 

    if (testListBox.InvokeRequired) 
     testListBox.BeginInvoke(new DelegateForTest(functionForTestListBox)); 
    else 
     functionForTestListBox(); 

} 

public void functionForTestListBox() 
{ 
    _t = testListBox.SelectedIndex; 

    if (_t < 0) 
     return; 

    _v = testListBox.SelectedValue; 

    method = _v as MethodInfo; 


    if (method == null) 
     return; 

    _selectedMethod = method.Name; 

    MessageBox.Show(_selectedMethod.ToString()); 

    method.Invoke(null, null);//<----Not sure about this. it runs fine when I dont invoke in a thread. 

    counter++; 

} 
private void runTestButton_Click(object sender, EventArgs e) 
{// Click event that calls the selected method in the thread 
    if (_serverStatus == "Running") 
    { 

     if (_testStatus == "Not Running") 
     { 

      // create Instance new Thread and add function 
      // which will do some work 
      try 
      { 
       SetupTestEnv(); 
       //functionForOutputTextBox(); 
       Thread UIthread = new Thread(new ThreadStart(GetTest)); 
       UIthread.Name = "UIThread"; 
       UIthread.Start(); 
       // Update test status 
       _testStatus = "Running"; 
       //Make thread global 
       _UIthread = UIthread; 
      } 
      catch 
      { 
        MessageBox.Show("There was an error at during the test setup(Note: You must install each web browser on your local machine before attempting to test on them)."); 
      } 

     } 
     else 
     { 
      MessageBox.Show("Please stop the current test before attempt to start a new one"); 
     } 
    } 
    else 
    { 
     MessageBox.Show("Please make sure the server is running"); 
    } 
} 

उत्तर

19

आप वस्तु दृष्टान्त संदर्भ प्रदान किए बिना गैर स्थैतिक विधि को लागू करने की कोशिश कर रहे हैं जो इस विधि के लिए बुलाया जाना चाहिए। आप MainForm वर्ग के तरीकों के साथ काम कर रहे हैं के बाद से, आप MethodInfo.Invoke(Object, Object[]) के पहले पैरामीटर में MainForm प्रकार की वस्तु प्रदान करना चाहिए, आपके मामले में:

public MethodInfo GetSelectedMethod() 
{ 
    var index = testListBox.SelectedIndex; 
    if (index < 0) return; 
    var value = testListBox.SelectedValue; 
    return value as MethodInfo; 
} 

private void ThreadProc(object arg) 
{ 
    var method = (MethodInfo)arg; 
    if(method.IsStatic) 
     method.Invoke(null, null) 
    else 
     method.Invoke(this, null); 
} 

private void RunThread() 
{ 
    var method = GetSelectedMethod(); 
    if(method == null) return; 
    var thread = new Thread(ThreadProc) 
    { 
     Name = "UIThread", 
    }; 
    thread.Start(method); 
} 
+0

धन्यवाद:

if(method.IsStatic) method.Invoke(null, null); else method.Invoke(this, null); 

अलग थ्रेड पर विधि को क्रियान्वित करने का उदाहरण त्वरित प्रतिक्रिया के लिए। यद्यपि इस कोड को आजमाने के बाद, यह मुख्य विधि धागे पर चयनित विधि को यूथ्रेड नहीं करता है। (उन थ्रेड नाम संदिग्ध हैं, इसके बारे में खेद है)। –

+0

आप 'testListBox.BeginInvoke()' का उपयोग कर मुख्य रूप से इस विधि को मुख्य प्रारूप धागे पर कॉल कर रहे हैं। 'MethodInfo.Invoke() 'उस थ्रेड पर निष्पादित करता है जहां से इसे कहा जाता था। – max

+0

आह मैं देखता हूं। वैसे ऐसा लगता है कि मुझे अपने कोड पर पुनर्विचार करना होगा। क्या आपके पास कोई विचार है कि मैं मुख्य रूप से अलग थ्रेड में चुने जाने के लिए चयनित विधि कैसे प्राप्त कर सकता हूं? –

संबंधित मुद्दे