在我的C#项目中,我使用以下代码在自己的类之外访问我的表单.
public static FormMain singletonInstance { get; private set; }
public static void PFA(Action<FormMain> action) //PFA = PerForm Action
{
var form = FormMain.singletonInstance;
if (form != null)
{
form.PerformAction(action);
}
}
public void PerformAction(Action<FormMain> action)
{
if (InvokeRequired)
Invoke(action, this);
else
action(this);
}
Run Code Online (Sandbox Code Playgroud)
"FormMain"中的构造函数是:
public FormMain()
{
InitializeComponent();
singletonInstance = this;
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何在Delphi中做到这一点.
问题(Delphi):当Im在其类之外时,我如何在表单上更改某些内容(例如Edit1.Text),Invoke?
提前致谢!
项目是C#.
所以我有一堆多线程代码,旨在作为库运行.这是UI的一个单独项目.
我的库有一个中心对象,需要在创建触发事件的任何内容之前创建.
是否可以在某些对象中传递此主对象,以便我的事件可以确定何时需要调用它们以返回到主UI线程?
我真的很想让UI不必进行大量的调用,因为他的事件处理程序几乎总是从一些随机的后台线程调用.
任何人都可以解释在调用具有列表集合作为参数的方法时如何完成内存分配.由于下面的代码片段显然看起来似乎相同,但它不会产生相同的结果.所以我想知道方法调用在内存分配方面的区别.
using System;
using System.Collections.Generic;
namespace ListSample
{
class ListSampleClass
{
static void Main(string[] args)
{
List<int> i = new List<int>();
i.Add(10);
i.Add(15);
SampleMethod1(i);
Console.WriteLine("Result of SampleMethod1:"+i[0]);
SampleMethod2(i);
Console.WriteLine("Result of SampleMethod2:" + i[0]);
Console.ReadKey();
}
public static void SampleMethod1(List<int> i)
{
List<int> j = new List<int>();
j.Insert(0,20);
i = j;
}
public static void SampleMethod2(List<int> i)
{
List<int> j = new List<int>();
j = i;
j.Insert(0, 20);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我试图理解Windows Forms中的某个长期概念:UI编程; 以下代码来自Chris Sells的Windows窗体编程书(第2版,2006):
void ShowProgress(string pi, int totalDigits, int digitsSoFar) {
// Display progress in UI
this.resultsTextBox.Text = pi;
this.calcToolStripProgressBar.Maximum = totalDigits;
this.calcToolStripProgressBar.Value = digitsSoFar;
if( digitsSoFar == totalDigits ) {
// Reset UI
this.calcToolStripStatusLabel.Text = "Ready";
this.calcToolStripProgressBar.Visible = false;
}
// Force UI update to reflect calculation progress
this.Refresh();
}
Run Code Online (Sandbox Code Playgroud)
此方法是小样本应用程序的一部分,它具有另一个计算Pi的长期运行方法.每次计算一组数字时,都会调用ShowProgress()来更新UI.正如书中所解释的,这段代码是"错误"的处理方式,并且当应用程序最小化时会导致UI冻结,然后再次进入前台,导致系统要求应用程序重新绘制自己.
我不明白:由于this.Refresh()被重复调用,为什么它不处理任何等待注意的系统重绘事件?
还有一个后续问题:当我在this.Refresh()之后立即添加Application.DoEvents()时,冻结问题就会消失. 这是不必诉诸于Invoke/BeginInvoke等.任何评论?
也许我被困了......但事实是我对线程有点n00b ...
我正在使用类中的串行端口.我正在从该课程中提出一个事件来调用我的课程.事件包含收到的数据......
我希望简单地从引发的事件中填充一个文本框.
现在我不是专门创建一个单独的线程,但是当我尝试在UI上更新我的文本框时,我得到了正常的交叉线程错误,所以我的假设是串口和它的内部方法可能会创建自己的线程......
无论如何,我对如何从我的主窗体正确实现一个调用有点困惑,指向实例化类中的线程...
我希望这是有道理的...
Dim WithEvents tmpRS232 As New clsRS232
Private Sub but_txt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles but_txt.Click
tmpRS232.Set_com_port("COM8", 38400)
tmpRS232.Transmit_data(txt_tx.Text)
End Sub
Private Sub tmprs232_rx_data_returned(ByVal str_data As String) Handles tmpRS232.rx_data_returned
txt_rx.Text = str_data 'Cross threading error
MsgBox(str_data) 'Fires without errors
End Sub
Run Code Online (Sandbox Code Playgroud)
Dim WithEvents tmpRS232 As New clsRS232
Private Sub but_txt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles but_txt.Click
tmpRS232.Set_com_port("COM8", 38400)
tmpRS232.Transmit_data(txt_tx.Text)
End Sub
Private Sub tmprs232_rx_data_returned(ByVal str_data As String) Handles …
Run Code Online (Sandbox Code Playgroud) 在调用int main()之后是否有可能运行任何其他指令?
int main(){cout<<"a";}
Run Code Online (Sandbox Code Playgroud)
在main()之后调用cout <<"b"; 在某个地方之后.没有任何改变main().
嘿伙计们,当我的申请结束时,我得到了这个例外.CustomerReadySub是我订阅的活动.
错误发生在这一行
fTypeLabel.Invoke(new MethodInvoker(fuelTypeChosen));
public void CustomerReadySub(object sender, CustomerReadyEventArgs fuel)
{
// code to handle the event
string CustReady = null;
//checks what fuel is chosen and then activates the pump
fuelType = fuel.SelectedFuel.ToString();
if (!String.IsNullOrEmpty(fuelType))
{
fTypeLabel.Invoke(new MethodInvoker(fuelTypeChosen));
if (fuelType == "Unleaded") //checks fuel type and displays price accordingly
{
pplText.Invoke(new MethodInvoker(petrol));
}
else
{
pplText.Invoke(new MethodInvoker(diesel));
}
CustReady = "READY";
WCFPump.sendReady(CustReady);
}
while (WCFPump.getOK() == 0) { /*do nothing*/} //used to loop around until OK is retrieved
if …Run Code Online (Sandbox Code Playgroud) 我试图通过Java反射调用一个方法,它没有任何参数:
Method getSelectedCriteriaMethod = multipleSorting.getClass().getDeclaredMethod("getSelectedCriteria", null);
EstimateSortField selectedCriteria = (EstimateSortField)getSelectedCriteriaMethod.invoke(multipleSorting, null);
Run Code Online (Sandbox Code Playgroud)
这是有效的,但我收到编译器警告:
warning: non-varargs call of varargs method with inexact argument type for last parameter;
cast to java.lang.Class<?> for a varargs call
non-varargs call of varargs method with inexact argument type for last parameter;
cast to java.lang.Object for a varargs call
Run Code Online (Sandbox Code Playgroud)
调用没有任何参数的方法的正确方法是什么?
你究竟如何从课外访问私有静态方法.说,我有一节课
Class ABC {
private:
static void print(string str) {
cout << "It works!!!" << endl;
}
};
Run Code Online (Sandbox Code Playgroud)
现在,我只是调用print()函数说来自另一个函数:
void doSomething() {
string str = "1776a0";
// Call to print() here
}
Run Code Online (Sandbox Code Playgroud)
我已经搜索了互联网和stackoverflow这样的问题,但我找不到多少.那么,请指出我是否正确的方向,如果可能的话,如果是这样的话.
我目前正在使用GCC.
谢谢大家.
在我的应用程序中我使用backgroundWorker,在一些TextBox中设置文本,我首先需要调用TextBox.
首先我使用:
if (someTextBox.InvokeRequired)
{
someTextBox.Invoke((MethodInvoker)delegate
{
someTextBox.Text = "some_text";
});
}
else
{
someTextBox.Text = "some_text";
}
Run Code Online (Sandbox Code Playgroud)
这个方法对我很好,但因为我有多个TextBox-es我写道:
private void invComp(TextBox txtBox, String str)
{
if (txtBox.InvokeRequired)
{
txtBox.Invoke((MethodInvoker)delegate
{
txtBox.Text = str;
});
}
else
{
txtBox.Text = str;
}
}
Run Code Online (Sandbox Code Playgroud)
最好以这种方式调用它?(invComp(someTextBox,"some_text");或者我可能有第三种,更好的方式?
我调用了一些按钮,我想在按钮上写这样的东西,如果这样可以吗?
TNX