本文整理汇总了C#中ThreadStart.Invoke方法的典型用法代码示例。如果您正苦于以下问题:C# ThreadStart.Invoke方法的具体用法?C# ThreadStart.Invoke怎么用?C# ThreadStart.Invoke使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ThreadStart
的用法示例。
在下文中一共展示了ThreadStart.Invoke方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RunInSTA
/// <summary>
/// Runs a specific method in Single Threaded apartment
/// </summary>
/// <param name="userDelegate">A delegate to run</param>
public void RunInSTA(ThreadStart userDelegate)
{
if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)
Run(userDelegate, ApartmentState.STA);
else
userDelegate.Invoke();
}
示例2: Run
private void Run(ThreadStart userDelegate, ApartmentState apartmentState)
{
lastException = null;
Thread thread = new Thread(
delegate()
{
#if !DEBUG
try
{
#endif
userDelegate.Invoke();
#if !DEBUG
}
catch (Exception e)
{
lastException = e;
}
#endif
});
thread.SetApartmentState(apartmentState);
thread.Start();
thread.Join();
if (ExceptionWasThrown())
ThrowExceptionPreservingStack(lastException);
}
示例3: Run
private void Run(ThreadStart userDelegate, ApartmentState apartmentState)
{
lastException = null;
var thread = new Thread(
delegate()
{
try
{
userDelegate.Invoke();
}
catch (Exception e)
{
lastException = e;
}
});
thread.SetApartmentState(apartmentState);
thread.Start();
thread.Join();
if (ExceptionWasThrown())
{
ThrowExceptionPreservingStack(lastException);
}
}
示例4: RunInApartment
/// <summary>
/// Runs in the specified apartment
/// </summary>
private void RunInApartment(ThreadStart userDelegate, ApartmentState apartmentState)
{
Exception thrownException = null;
Thread thread = new Thread(
delegate()
{
try
{
userDelegate.Invoke();
}
catch (Exception e)
{
thrownException = e;
}
});
thread.SetApartmentState(apartmentState);
thread.Start();
thread.Join();
if (thrownException != null)
{
ThrowExceptionPreservingStack(thrownException);
}
}
示例5: Run
/// <summary>
/// Runs a specific method in Single Threaded apartment
/// </summary>
/// <param name="userDelegate">A delegate to run</param>
public void Run(ThreadStart userDelegate, ApartmentState apartment)
{
if (Thread.CurrentThread.GetApartmentState() != apartment)
{
RunInApartment(userDelegate, apartment);
}
else
{
userDelegate.Invoke();
}
}
示例6: MultiThreadedWorker
void MultiThreadedWorker(ThreadStart userDelegate)
{
try
{
userDelegate.Invoke();
}
catch (Exception e)
{
lastException = e;
}
}
示例7: ViewDidLoad
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
this.Title = "Background Execution";
this.btnStartLongRunningTask.TouchUpInside += (s, e) => {
ThreadStart ts = new ThreadStart ( () => { this.DoSomething(); });
ts.Invoke();
};
}
示例8: ShowAndWait
/// <summary>
/// Show the "please wait" form, execute the code from the delegate and wait until execution finishes.
/// The supplied delegate will be wrapped with a try/catch so this method can return any exception that was thrown.
/// </summary>
/// <param name="title">The title of the form (and Thread)</param>
/// <param name="text">The text in the form</param>
/// <param name="waitDelegate">delegate { with your code }</param>
public void ShowAndWait(string title, string text, ThreadStart waitDelegate) {
this.title = title;
Text = title;
label_pleasewait.Text = text;
cancelButton.Text = Language.GetString("CANCEL");
// Make sure the form is shown.
Show();
// Variable to store the exception, if one is generated, from inside the thread.
Exception threadException = null;
try {
// Wrap the passed delegate in a try/catch which makes it possible to save the exception
waitFor = new Thread(new ThreadStart(
delegate {
try {
waitDelegate.Invoke();
} catch (Exception ex) {
LOG.Error("invoke error:", ex);
threadException = ex;
}
})
);
waitFor.Name = title;
waitFor.IsBackground = true;
waitFor.SetApartmentState(ApartmentState.STA);
waitFor.Start();
// Wait until finished
while (!waitFor.Join(TimeSpan.FromMilliseconds(100))) {
Application.DoEvents();
}
LOG.DebugFormat("Finished {0}", title);
} catch (Exception ex) {
LOG.Error(ex);
throw;
} finally {
Close();
}
// Check if an exception occured, if so throw it
if (threadException != null) {
throw threadException;
}
}
示例9: RunCodeAsSTA
public static void RunCodeAsSTA(AutoResetEvent are, ThreadStart originalDelegate)
{
Thread thread = new Thread(delegate()
{
try
{
originalDelegate.Invoke();
are.Set();
Dispatcher.CurrentDispatcher.InvokeShutdown();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
are.Set();
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
示例10: RunTask
public static Thread RunTask(ThreadStart task)
{
if (_semaphore == null)
{
if (MaxThreads == 0)
MaxThreads = 10;
_semaphore = new Semaphore(MaxThreads, MaxThreads);
}
_semaphore.WaitOne();
var t = new Thread(delegate()
{
try
{
task.Invoke();
}
finally
{
_semaphore.Release();
//Remove thread from _thread list
lock (_threads)
{
if (_threads.Contains(Thread.CurrentThread))
_threads.Remove(Thread.CurrentThread);
}
}
});
t.IsBackground = true;
lock (_threads)
_threads.Add(t);
t.Start();
return t;
}
示例11: scanDirectory
protected static void scanDirectory(string path, bool newThread = true)
{
logger.Info("Scanning directory {0}", path);
var t = new ThreadStart(delegate
{
var hashingService = HashingService.GetHashingService();
if (!Directory.Exists(path))
return;
foreach (var file in Directory.GetFiles(path))
{
hashingService.GetHashAsync(file, Priority.Low);
}
foreach (var dir in Directory.GetDirectories(path))
{
scanDirectory(dir, false);
}
});
if (newThread)
{
new Thread(t).Start();
}
else
{
t.Invoke();
}
}
示例12: WithGrabAt
public ThreadStart WithGrabAt(SimObject obj, ThreadStart closure)
{
var Client = GetGridClient();
return () =>
{
Primitive targetPrim = obj.Prim;
uint objectLocalID = targetPrim.LocalID;
AgentManager ClientSelf = Client.Self;
try
{
ClientSelf.Grab(objectLocalID);
closure.Invoke();
}
finally
{
ClientSelf.DeGrab(objectLocalID);
}
};
}
示例13: WithAnim
public ThreadStart WithAnim(SimAsset anim, ThreadStart closure)
{
var Client = GetGridClient();
AssetThread assetThread = new AssetThread(Client.Self, anim);
return () =>
{
try
{
assetThread.Start();
closure.Invoke();
}
finally
{
assetThread.Stop();
}
};
}
示例14: ExecuteCmdletBase
public void ExecuteCmdletBase(PSCmdlet callingCmdlet)
{
CallingCmdlet = callingCmdlet;
Completed = new ManualResetEvent(false);
ProgressEvent = new AutoResetEvent(false);
LogMessageEvent = new AutoResetEvent(false);
Events = new WaitHandle[] { Completed, ProgressEvent, LogMessageEvent };
//The init of the MapManager and the setting up of the binding takes time,
//starting it async gives instant progress feedback to the user in the cmdlet.
ThreadStart initMapManagerThread = new ThreadStart(InitMapManager);
initMapManagerThread.Invoke();
int index = -1;
do
{
index = WaitHandle.WaitAny(Events);
if (index == 1)
{
lock (pr_lock)
{
WriteProgress(ProgressRecord);
}
}
else if (index == 2)
{
lock(msg_lock)
{
WriteWarningMessage(Message);
}
}
}
while (index != 0); //0 is the Completed event
}
示例15: WithSitOn
public ThreadStart WithSitOn(SimObject obj, ThreadStart closure)
{
bool CanUseSit = WorldSystem.CanUseSit;
return () =>
{
bool SattedUpon = false;
if (CanUseSit)
{
SattedUpon = SitOn(obj);
}
try
{
closure.Invoke();
}
finally
{
if (CanUseSit)
{
if (SattedUpon) StandUp();
}
}
};
}