Liste kutusu görüntüleme yöntemleriyle (öznitelikle) bir win form uygulamasına sahibim. Bir iş parçacığı içinde yöntemleri dinamik olarak çağırmaya çalışıyorum, liste kutusunun seçilen değerinden yöntem bilgisi almak için yansıma kullanarak. Ancak, Methodinfo.Invoke çağıran bu iç istisna alıyorum "Statik olmayan bir yöntem, bir hedef C# gerektirir". İşte Statik olmayan bir yöntem için bir hedef C gerektirir. #
kodum var (hala C# ve genel olarak programlama yeniyim unutmayın.)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");
}
}
Teşekkür: Ayrı parçacığı üzerinde usulünün
Örnek Hızlı yanıt için. Bu kodu denedikten sonra, UIthread değil ana form iplik üzerinde seçilen yöntemi çağırır. (Bu iş parçacığı adları belirsiz, bu konuda üzgünüm). –
Açıkça bu yöntemi mainform iş parçacığı üzerinde 'testListBox.BeginInvoke() 'kullanarak çağırıyorsunuz. 'MethodInfo.Invoke() ', çağrıldığı iş parçacığı üzerinde gerçekleştirir. – max
Ah görüyorum. Görünüşe göre kodumu yeniden gözden geçirmem gerekecek. Seçtiğiniz yöntemin ana formdan farklı bir iş parçacığı içinde çağrılması hakkında nasıl gidebileceğime dair herhangi bir fikriniz var mı? –