我很擅长使用线程.我试图设定一个DependencyProperty
值:
public States State
{
get { return (States)GetValue(StateProperty); }
set
{
Dispatcher.BeginInvoke(DispatcherPriority.Background,
//(SendOrPostCallback)delegate { SetValue(StateProperty, value); }, //works
(Action)(()=> SetValue(StateProperty, value)), //doesnt
value);
}
}
public static readonly DependencyProperty StateProperty =
DependencyProperty.Register("State", typeof(States), typeof(FTPDownload), new UIPropertyMetadata(States.Idle));
Run Code Online (Sandbox Code Playgroud)
我意识到在setter中你必须使用SendOrPostCallback(因为它在调用方法时提供了一个参数).它不适用于Action(因为缺少参数.而且,wpf真的是一个关于它的婊子,调试并找到TargetParameterCountException的原因,"没有源可用",根本没有线索.
为什么我必须在那里使用SendOrPostCallback?我怎么知道在这种情况下这是正确的?因为实际上通过以下方式调用setter:
Dispatcher.BeginInvoke((Action)(()=>State=States.Updating), null);
Run Code Online (Sandbox Code Playgroud)
并使用SendOrPostCallback而不是当然导致TargetParameterCountException ..
只是想知道看似不一致的事情是否只是常识?感觉有点迷失在这里,至少自从谷歌搜索SendOrPostCallback,Action和BeginInvoke作为关键字没有有意义的结果.
当我打电话时,为什么Visual Studio 2010告诉我''System.Delegate'不包含'EndInvoke'的定义job.Delegate.EndInvoke()
?我如何解决它?请注意,它BeginInvoke()
很好,并且如果我EndInvoke()
立即添加后不会抱怨BeginInvoke()
(思想帽子没有达到我想要的效果).
我有一个小JobTracker类用于跟踪备份作业:
public class JobTracker
{
private class Job
{
public Account Account { get; set; }
public IAsyncResult Result { get; set; }
public Delegate Delegate { get; set; }
public bool IsCompleted { get { return result.IsCompleted } }
public string Instance { get { return Account.Instance } }
}
public List<Job> Running = new List<Job>;
public void AddJob(Account a, IAsyncResult result, Delegate del)
{
var j = …
Run Code Online (Sandbox Code Playgroud) 是否有解释Control.BeginInvoke()不执行它传递的委托?
我们在Winforms应用程序中采用了以下模式,以便在UI线程上安全地执行UI相关工作:
private Control hiddenControl = new Control();
private void uiMethod()
{
MethodInvoker uiDelegate = new MethodInvoker(delegate()
{
Logging.writeLine("Start of uiDelegate");
//ui releated operations
childDialog = new ChildDialog();
childDialow.show();
Logging.writeLine("End of uiDelegate");
});
if (hiddenControl.InvokeRequired)
{
Logging.writeLine("Start of InvokeRequired block");
hiddenControl.BeginInvoke(uiDelegate);
Logging.writeLine("End of InvokeRequired block");
}
else
{
uiDelegate();
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我们显式创建一个控件"hiddenControl",以便在UI线程上运行委托.我们从不调用endInvoke,因为它显然不是 Control.BeginInvoke所必需的,我们永远不需要返回值,因为我们的方法只是操纵UI,无论如何.
虽然非常冗长,这种格局似乎是一个比较 好 接受的 解决方案.然而,有一些证据表明即使这种模式在所有情况下都不能很好地发挥作用.
我不排除应用程序错误并指责WinForms.毕竟,选择可能不会破碎.然而,我无法解释为什么代表似乎根本没有参与竞选.
在我们的例子中,我们有时会观察到"开始uiDelegate"日志消息从未在某些线程场景中执行,即使"Start of InvokeReqiured block"和"End of InvokeRequired block"成功执行.
复制此行为非常困难,因为我们的应用程序是作为DLL提供的; 我们的客户在自己的应用程序中运行它.因此,我们无法保证可以调用这些方法的方式或方式.
我们排除了UI线程饥饿,因为观察到UI没有锁定.据推测,如果UI正在更新,则消息泵可操作并可用于从消息队列中提取消息并执行其委托. …
我有简单的逻辑(我认为)。
public static void NotifyAboutNewJob(int jobId, bool forceSending = false)
{
Action<int> notifier = SendAppleNotifications;
notifier.BeginInvoke(jobId, null, null);
}
Run Code Online (Sandbox Code Playgroud)
方法SendAppleNotifications有一个参数a,很容易传递给它BeginInvoke
。现在,我添加了第二个参数forceSending
。问题-我不知道如何将其传递给BeginInvoke
。
我应该将其作为第三参数传递object
吗?
private static void SendAppleNotifications(int jobId, bool forceSending = false){...}
Run Code Online (Sandbox Code Playgroud)
或这就是答案:
Action<int, bool> notifier = SendAppleNotifications;
notifier.BeginInvoke(jobId, forceSending, null, null);
Run Code Online (Sandbox Code Playgroud) 我被告知Invoke()类似于普通方法调用...那么为什么人们会选择使用Invoke而不是正常的方法调用?
我尝试在线搜索这个问题,我得到的是使用BeginInvoke()的优点,但使用Invoke()有什么好处?
我想写一个类来简化异步编程,比如string s = mylib.BeginInvoek(test,"1"); 这是我的代码:
public T BeginInvokeExWithReturnValue<T>(Func<T> actionFunction)
{
ExecWithReturnType<T> execWtihReturnValue = new ExecWithReturnType<T>(actionFunction);
IAsyncResult iar = execWtihReturnValue.BeginInvoke(new AsyncCallback(EndInvokeExWithReturnValue<T>), execWtihReturnValue);
// how to code here to return value
}
private void EndInvokeExWithReturnValue<T>(IAsyncResult iar)
{
ExecWithReturnType<T> execWtihReturnValue = (ExecWithReturnType<T>)iar.AsyncState;
execWtihReturnValue.EndInvoke(iar);
}
Run Code Online (Sandbox Code Playgroud)
这个BeginInvokeExWithReturnValue函数没有输入参数,但返回一个值,但我不知道如何从BeginInvokeExWithReturnValue函数返回一个值.知道这一点的人,你能帮助我吗?非常感谢.
我有这个C#代码:
Action action = MyFunction;
action.BeginInvoke(action.EndInvoke, action);
Run Code Online (Sandbox Code Playgroud)
从我所知道的,只是异步运行MyFunction.你能用Java做同样的事情吗?
我正在维护一个包含客户信息的程序.它由许多表单组成,每个表单都显示数据库中的一些相关信息.执行以下操作后,此错误是单一形式
这是失败的代码:
private void FireFileCountChanged() {
if (FileCountChanged != null)
BeginInvoke(new DeferEvent(FireFileCountChangedDeferred), 2); // FAILS
Run Code Online (Sandbox Code Playgroud)
"System.Windows.Forms.dll中发生了'System.InvalidOperationException'类型的未处理异常
附加信息:在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke."
我尝试添加以下内容:
private void FireFileCountChanged() {
if (FileCountChanged != null && this.Handle != null) // CHANGED AND FAILS.
BeginInvoke(new DeferEvent(FireFileCountChangedDeferred), 2);
}
Run Code Online (Sandbox Code Playgroud)
但是this.handle给出了:
'this.Handle'抛出了'System.ObjectDisposedException'类型的异常,并且"无法访问已处置的对象.\ r \n对象名称:'AttachmentsControl'."
然后我添加了10秒的超时作为方法的第一行,但仍然没有创建句柄.当其中一个窗户关闭时,手柄是否已被处理掉?那可以做些什么呢?任何帮助表示赞赏.我有点卡住了.
private void FireFileCountChangedDeferred(int repostCount) {
if (FileCountChanged != null) {
if (repostCount > 0) {
//black magic is somehow involved in getting this event to fire *after* the filewatcher reports the change. …
Run Code Online (Sandbox Code Playgroud) 这是Microsoft的代码片段.我对异步方法调用有疑问.
因为我们正在调用end.Invoke在Begin-invoke之后看起来我们正在进行同步调用.因为我们正在等待异步调用的返回值.
如果异步方法在调用end.invoke时没有完成,会发生什么.我们可以继续下一个声明,或者我们必须等待.
如果在多线程环境中发生这种情况,它们如何处理回调信号以纠正线程.
public void DemoEndInvoke()
{
MethodDelegate dlgt = new MethodDelegate (this.LongRunningMethod) ;
string s ;
int iExecThread;
// Initiate the asynchronous call.
IAsyncResult ar = dlgt.BeginInvoke(3000, out iExecThread, null, null);
// Do some useful work here. This would be work you want to have
// run at the same time as the asynchronous call.
// Retrieve the results of the asynchronous call.
s = dlgt.EndInvoke (out iExecThread, ar) ;
MessageBox.Show (string.Format ("The delegate call returned the string: …
Run Code Online (Sandbox Code Playgroud) 只是尝试学习Invoke/BeginInvoke,我遇到了那个问题.
// Update UI
public void UpdateForm(string value) {
txtLog.AppendText(value + "\r\n");
}
// Thread function
private void readSocket() {
string row = "";
while (socket.Connected) {
row = socket.readLine();
if (IsControlValid(this))
BeginInvoke((MethodInvoker)delegate { UpdateForm(String.Copy(row)); });
}
}
Run Code Online (Sandbox Code Playgroud)
使用Invoke方法我的UI更新与正确的文本,而不是如果我使用BegineInvoke我看到错误的文本,即一些文本反复很多时间.我知道那个电话
BeginInvoke((MethodInvoker)delegate { UpdateForm(row); });
Run Code Online (Sandbox Code Playgroud)
也许"行"可以像共享变量一样行为而不是
BeginInvoke((MethodInvoker)delegate { UpdateForm(String.Copy(row)); });
Run Code Online (Sandbox Code Playgroud)
我认为每个BeginInvoke调用都会创建一个"新"委托,因此使用String.Copy必须创建另一个字符串实例,但我看到总是错误的值(重复,ecc).
哪里我错了?