本文整理汇总了C#中Func.BeginInvoke方法的典型用法代码示例。如果您正苦于以下问题:C# Func.BeginInvoke方法的具体用法?C# Func.BeginInvoke怎么用?C# Func.BeginInvoke使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Func
的用法示例。
在下文中一共展示了Func.BeginInvoke方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ScanDouble
static void ScanDouble()
{
// Delegates anlegen
var dg1 = new Func<long, long, IEnumerable<long>>(mko.Algo.NumberTheory.PrimeFactors.scan);
var dg2 = new Func<long, long, IEnumerable<long>>(mko.Algo.NumberTheory.PrimeFactors.scan);
var dg3 = new Func<long, long, IEnumerable<long>>(mko.Algo.NumberTheory.PrimeFactors.scan);
var dg4 = new Func<long, long, IEnumerable<long>>(mko.Algo.NumberTheory.PrimeFactors.scan);
// Methoden asynchron mittels Delegates starten
var ares1= dg1.BeginInvoke(1, 2500000, null, null);
var ares2= dg2.BeginInvoke(2500001, 5000000, null, null);
var ares3 = dg3.BeginInvoke(5000001, 75000000, null, null);
var ares4 = dg4.BeginInvoke(7500001, 10000000, null, null);
while (!ares1.IsCompleted && !ares2.IsCompleted && !ares3.IsCompleted && !ares4.IsCompleted)
{
Debug.Write(".");
System.Threading.Thread.Sleep(200);
}
//ares1.AsyncWaitHandle.WaitOne();
var res1 = dg1.EndInvoke(ares1);
var res2 = dg2.EndInvoke(ares2);
}
示例2: button1_Click
private void button1_Click(object sender, EventArgs e)
{
// the inputs
int x = 2;
// the "thread" or work to be "BeginInvoked"
var dlg = new Func<int, int>(
param =>
{
// put in a sleep just for fun...
System.Threading.Thread.Sleep(500);
// note that, while we COULD have referenced "x" here, we are NOT...
// pretend this lives somewhere else... or is even a web request or something.
return param * 2;
});
// "BeginInvoke" will spawn a new ThreadPool thread to do the work for us
var iar = dlg.BeginInvoke(
// this corresponds to the "param" in the delegate above
x,
// who gets called when the work is completed
Callback,
// "AsyncState" - used to pass additional information around
// By convention, the convention was to usually use an object array
// I'd recommend using a Tuple now for more "strong typed ness"
// - first parameter here is the actual delegate (lambda)
// - second parameter here is the input (why? you'll see in a little bit...)
// Alternatively, you can use a custom object here... depends on the consumer as far as which you'd want to use
new Tuple<Func<int, int>, int>(dlg, x));
}
示例3: beginSimulatedDatabaseRequest
// See also http://stackoverflow.com/questions/1047662/what-is-asynccallback for this ugly callback model.
static IAsyncResult beginSimulatedDatabaseRequest(AsyncCallback callback)
{
// simulate long running I/O here, e.g. a DB request
var func = new Func<int>(() => {
System.Threading.Thread.Sleep(3000);
return ++requestX; // not threadsafe, whatever
});
return func.BeginInvoke(callback, func); // which thread will this execute on? A threadpool?
}
示例4: Main
static void Main(string[] args)
{
Func<int, int, int> additionDel = new Func<int, int, int>(Addition);
int ret = additionDel.Invoke(1, 2);
Console.WriteLine("From Sync invoke " + ret);
object state = new Object();
IAsyncResult _asyncresult = additionDel.BeginInvoke(1,2,null, null);
//calling thread blocks until endInvokecompletes
ret= additionDel.EndInvoke(_asyncresult);
Console.WriteLine("From Aynsc EndInvoke " + ret);
_asyncresult = additionDel.BeginInvoke(1, 2, new AsyncCallback(PrintCallBack), null);
// ret = additionDel.EndInvoke(_asyncresult);
// Console.WriteLine("From Aynsc EndInvoke On asyncCallback" + ret);
Thread.Sleep(5000);
}
示例5: Run
public override void Run()
{
if (Enable && !connecting && !GlobalVariables.IsConnected() &&
Kernel.GetInstance().ActionControl.CanPerformAction(ActionControlType.LOGIN))
{
var func = new Func<String, String, String, bool>(Kernel.GetInstance().Client.Login.Login);
func.BeginInvoke(Account, Password, CharacterName, new AsyncCallback(LoginCallback), func);
connecting = true;
}
}
示例6: apm
/// AsynchronouseProngraingModel
void apm()
{
before_work();
var method = new Func<double>(() => { busy_work(); return 0; });
method.BeginInvoke(ar =>
{
var result = method.EndInvoke(ar);// get return value
this.BeginInvoke((Action)(() => { after_work(); }));
}, null);
}
示例7: DatabaseTask
IEnumerator<IAsyncCall> DatabaseTask()
{
button1.Enabled = false;
AsyncCall<int> call = new AsyncCall<int>();
Func slow = new Func(DarnSlowDatabaseCall);
yield return call.WaitOn(cb => slow.BeginInvoke(cb, null)) & slow.EndInvoke;
textBox1.Text = "db task returned " + call.Result + "\r\n";
button1.Enabled = true;
}
示例8: TimeoutTest
public void TimeoutTest()
{
// Arrange.
var func = new Func<ProcessExecutionResult>(() => ProcessRunner.RunProcess("cmd.exe", PauseArguments, Timeout));
// Act.
var result = func.BeginInvoke((a) => { }, null);
result.AsyncWaitHandle.WaitOne(Timeout + AdditionalTimeout);
// Assert.
if (result.IsCompleted)
{
func.EndInvoke(result);
}
}
示例9: MainWindow
public MainWindow()
{
InitializeComponent();
worker.ConnectionStatus += status => this.GuiAsync(() => ConnectionStatus(status));
worker.Processed += (a, b) => this.GuiAsync(() => Worker_Processed(a, b));
Processed += (i) => this.GuiAsync(() => MainWindow_Processed(i));
Func<int> func = new Func<int>(GetAvailableWorkThreads);
IAsyncResult asyncResult;
asyncResult = func.BeginInvoke(null, null);
}
示例10: TimeoutKillTest
public void TimeoutKillTest()
{
// Arrange.
var func = new Func<ProcessExecutionResult>(() => ProcessRunner.RunProcess("cmd.exe", PauseArguments, Timeout));
// Act.
var result = func.BeginInvoke((a) => { }, null);
result.AsyncWaitHandle.WaitOne();
// Assert.
if (Process.GetCurrentProcess().GetChildProcesses().Any())
{
Assert.Fail();
}
Assert.Pass();
}
示例11: ybpara
/// <summary>异步传参数
/// </summary>
/// <param name="age"></param>
public static void ybpara(int age)
{
//异步执行
Func<people, string> FuncAsy = new Func<people, string>((pp) =>
{
return pp.Name;
}
);
FuncAsy.BeginInvoke(new people("小李" + age, age), resual =>
{
//异步执行,从回调函数中获取返回结果
Console.WriteLine(">>>>:{0}", FuncAsy.EndInvoke(resual));
Console.WriteLine(">>>>:{0} end", age);
}, null);
}
示例12: yb
/// <summary>异步
/// </summary>
/// <param name="age"></param>
public static void yb(int age)
{
//异步执行
Func<string> FuncAsy = new Func<string>(() =>
{
people tPeo = new people("异步小李", age);
return tPeo.ToString();
}
);
FuncAsy.BeginInvoke(resual =>
{
//异步执行,从回调函数中获取返回结果
Console.WriteLine(">>>>:{0}", FuncAsy.EndInvoke(resual));
Console.WriteLine(">>>>:{0} end", age);
}, null);
}
示例13: LoadAsync
public static void LoadAsync(string filename, Action<IStorableContent, Exception> loadingCompletedCallback) {
if (instance == null) throw new InvalidOperationException("ContentManager is not initialized.");
var func = new Func<string, IStorableContent>(instance.LoadContent);
func.BeginInvoke(filename, delegate(IAsyncResult result) {
Exception error = null;
IStorableContent content = null;
try {
content = func.EndInvoke(result);
content.Filename = filename;
}
catch (Exception ex) {
error = ex;
}
loadingCompletedCallback(content, error);
}, null);
}
示例14: Main
static void Main(string[] args)
{
Func<string, string> cmdPrint = null;
cmdPrint = new Func<string, string>(
x=> { Thread.Sleep(10000); return "Hola " + x;}
);
var cb = new AsyncCallback(delegate(IAsyncResult result) {
var del = result.AsyncState as Func<string, string>;
var men = del.EndInvoke(result);
Console.WriteLine(men);
});
cmdPrint.BeginInvoke("Juan", cb, cmdPrint);
Otra();
Console.ReadLine();
}
示例15: BeginExecuteQuery
/**
* Begin asynchronous execution of a query.
* @param query The query to execute. Can be one of the following:
* <ul>
* <li><b>randomize</b> Randomizes a pseudonym. parameters[0] must be the pseudonym to randomize.
* <li><b>getEP</b> Returns an encrypted pseudonym. parameters[0] must be the User ID, parameters[0] the SP ID.
* <li><b>getEP</b> Returns a polymorphic pseudonym. parameters[0] must be the User ID.
* </ul>
* @param parameters The parameters for the query.
* @param callback The delegate to call when execution is finished.
* @param state Not used.
* @return IAsyncResult for the asynchronous execution.
*/
public IAsyncResult BeginExecuteQuery(string query, string[] parameters, AsyncCallback callback, object state)
{
PolyPseudWorker worker = new PolyPseudWorker(parameters, config["y_k"], config["connectionString"], config["pseudonymProviderUrl"]);
switch(query)
{
case "randomize":
func = new Func<string[][]>(worker.Randomize);
return func.BeginInvoke(callback, state);
case "getEP":
func = new Func<string[][]>(worker.GetEP);
return func.BeginInvoke(callback, state);
case "getPP":
func = new Func<string[][]>(worker.GetPP);
return func.BeginInvoke(callback, state);
default:
throw new ArgumentException(String.Format("The query '{0}' is not recognized.", query));
}
}