本文整理汇总了C#中MethodInvoker.Invoke方法的典型用法代码示例。如果您正苦于以下问题:C# MethodInvoker.Invoke方法的具体用法?C# MethodInvoker.Invoke怎么用?C# MethodInvoker.Invoke使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MethodInvoker
的用法示例。
在下文中一共展示了MethodInvoker.Invoke方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: StartTry
public static Thread StartTry(MethodInvoker code, ErrorHandler on_error = null, MethodInvoker on_finally = null, bool background = true)
{
Thread t = new Thread(
() => {
try
{
code.Invoke();
}
catch (ThreadAbortException e)
{
Thread.ResetAbort();
}
catch (Exception e)
{
if (on_error != null)
on_error.Invoke(e);
else
Message.Error(e);
}
finally
{
on_finally?.Invoke();
}
}
);
t.IsBackground = background;
t.Start();
return t;
}
示例2: ThreadSafeDelegate
// Thread-safe operations from event handlers
// I love StackOverflow: http://stackoverflow.com/q/782274
public void ThreadSafeDelegate(MethodInvoker method)
{
if (InvokeRequired)
BeginInvoke(method);
else
method.Invoke();
}
示例3: ActualizaProgressBar
private void ActualizaProgressBar()
{
while (true)
{
MethodInvoker m = new MethodInvoker(() =>
{
int valor = progressBar.Value;
if (valor == 100)
{
valor = 0;
}
else
{
++valor;
}
progressBar.Value = valor;
Thread.Sleep(milisegundos);
});
if (progressBar.InvokeRequired)
{
progressBar.Invoke(m);
}
else
{
m.Invoke();
}
}
}
示例4: UpdateState
private void UpdateState()
{
MethodInvoker invoker = new MethodInvoker(delegate()
{
if ((File.GetAttributes(m_originalSolutionFullPath) & FileAttributes.ReadOnly) != 0)
{
m_labelState.ForeColor = Color.Red;
m_labelState.Text = "ReadOnly";
m_labelStateDescription.Visible = true;
m_buttonYes.Enabled = false;
}
else
{
m_labelState.ForeColor = Color.Black;
m_labelState.Text = "Writable";
m_labelStateDescription.Visible = false;
m_buttonYes.Enabled = true;
}
});
if (m_labelState.InvokeRequired)
{
this.Invoke(invoker);
}
else
{
invoker.Invoke();
}
}
示例5: BeginInvoke
public static void BeginInvoke(this Control c, MethodInvoker code)
{
//c.BeginInvoke(code);
if (c.InvokeRequired)
c.BeginInvoke(code);
else
code.Invoke();
}
示例6: Test_MethodInvoker_Basic
public void Test_MethodInvoker_Basic()
{
var viewModel5 = new TestViewModel5();
var canMethodInvoker = new MethodInvoker(new BindContext(viewModel5, "TestViewModel"), "CanAddAge", null);
var methodInvoker = new MethodInvoker(new BindContext(viewModel5, "TestViewModel"), "AddAge", null);
bool value = canMethodInvoker.CanInvoke();
Assert.IsFalse(value);
methodInvoker.Invoke();
viewModel5.TestViewModel = new TestViewModel();
value = canMethodInvoker.CanInvoke();
Assert.IsTrue(value);
methodInvoker.Invoke();
Assert.AreEqual(1, viewModel5.TestViewModel.Age);
var canMethodInvoker2 = new MethodInvoker(new BindContext(viewModel5, "TestViewModel"), "AddAge", null);
MissingMemberException exception = null;
try
{
canMethodInvoker2.CanInvoke();
}
catch (MissingMemberException e)
{
exception = e;
}
Assert.IsNotNull(exception);
var vm = new TestViewModel();
var canMethodInvoker3 = new MethodInvoker(new BindContext(viewModel5, "TestViewModel"), "CanSetAge",
new[] { new BindContext(vm, "Age") });
var methodInvoker2 = new MethodInvoker(new BindContext(viewModel5, "TestViewModel"), "SetAge",
new[] { new BindContext(vm, "Age") });
Assert.IsTrue(canMethodInvoker3.CanInvoke());
vm.Age = 6;
Assert.AreNotEqual(6, viewModel5.TestViewModel.Age);
methodInvoker2.Invoke();
Assert.AreEqual(6, viewModel5.TestViewModel.Age);
}
示例7: InvokeInControlThread
public static void InvokeInControlThread(Control c, MethodInvoker code)
{
if (c.InvokeRequired)
{
c.Invoke(code);
return;
}
code.Invoke();
}
示例8: UIThread
public static void UIThread(this System.Windows.Forms.Control form, MethodInvoker code)
{
if (form.InvokeRequired)
{
form.Invoke(code);
return;
}
code.Invoke();
}
示例9: UIThread
/// <summary>
/// Ensure code is run on the UI thread.
/// </summary>
internal static void UIThread(Control form, MethodInvoker code)
{
if (form.InvokeRequired)
{
form.Invoke(code);
return;
}
code.Invoke();
}
示例10: invoke_in_hosting_thread
protected void invoke_in_hosting_thread(MethodInvoker code)
{
if (this.InvokeRequired)
{
this.Invoke(code);
return;
}
code.Invoke();
}
示例11: InvokeMainWindow
/// <summary>
/// Invokes a method on the main window thread
/// </summary>
/// <param name="aInvoker">the method to invoke</param>
/// <param name="aAsync">set to true to invoke asynchronously</param>
private void InvokeMainWindow(MethodInvoker aInvoker, bool aAsync)
{
if (this.mPluginHost.MainWindow.InvokeRequired) {
if (aAsync) {
mPluginHost.MainWindow.BeginInvoke(aInvoker);
} else {
mPluginHost.MainWindow.Invoke(aInvoker);
}
} else {
aInvoker.Invoke();
}
}
示例12: guardedOp
/// <summary>
/// Executes a delegate and show exception window on exception
/// </summary>
private static void guardedOp(MethodInvoker mi)
{
try
{
mi.Invoke();
}
catch (Exception ex)
{
log.Error(ex);
ExceptionDialog.showException(ex, "Dirigent Exception", "");
}
}
示例13: FormAutoAnswer_Load
private void FormAutoAnswer_Load(object sender, EventArgs e)
{
refresh = () =>
{
lsv.Items.Clear();
foreach (var item in aa)
{
lsv.Items.Add(new ListViewItem(new string[]{
item.Description,
item.Prefix,
item.Identify
}));
}
};
refresh.Invoke();
}
示例14: InvokeTest
public void InvokeTest()
{
MethodInfo[] myMethodInfo;
Type myType = typeof(MyClass);
// Get the type and fields of FieldInfoClass.
myMethodInfo = myType.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance
| BindingFlags.Public);
MethodInfo methodInfo = myMethodInfo[0];
IMethodInvoker target = new MethodInvoker(methodInfo);
object instance = new MyClass("S001", "司法署");
object expected = "S001";
object actual;
actual = target.Invoke(instance, null);
Assert.AreEqual(expected, actual);
}
示例15: RenderThreadInvoke
/// <summary>
/// Special Invoke for framework callbacks from the render process.
/// Maintains the thread within the context of the calling remote thread.
/// Use this instead of invoke when the following conditions are meat:
/// 1) The current thread is executing in the scope of a framework
/// callback event from the render process (ex. CfrTask.Execute).
/// 2) You need to Invoke on the webbrowser control and
/// 3) The invoked code needs to call into the render process.
/// </summary>
public void RenderThreadInvoke(MethodInvoker method)
{
if (!CfxRemoteCallContext.IsInContext)
{
throw new HtmlUIException("Can't use RenderThreadInvoke without being in the scope of a render process callback.");
}
if (!InvokeRequired)
{
method.Invoke();
return;
}
var context = CfxRemoteCallContext.CurrentContext;
// Use BeginInvoke and Wait instead of Invoke.
// Invoke marshals exceptions back to the calling thread.
// We want exceptions to be thrown in place.
var waitLock = new object();
lock (waitLock)
{
BeginInvoke((MethodInvoker)(() => {
context.Enter();
try
{
method.Invoke();
}
finally
{
context.Exit();
lock (waitLock)
{
Monitor.PulseAll(waitLock);
}
}
}));
Monitor.Wait(waitLock);
}
}