标签: task

C#线程静态变量

我有一个实现单例模式的静态DataLibrary类.

        public static FacilityRepository FacilRepo
        {
            get
            {
                if (_facilRepo == null)
                {
                    _facilRepo = new FacilityRepository(Authenticated.UserId);
                    if (Authenticated.FacKey.Length > 0)
                    {
                        foreach (var fac in _facilRepo)
                            fac.IsSelected = (fac.FacilityKey == Authenticated.FacKey);                        
                    }
                }
                return _facilRepo;
            }
        }

private static FacilityRepository _facilRepo;
Run Code Online (Sandbox Code Playgroud)

当我使用Task.Factory.StartNew从不同的线程访问它时,FacilityReposity会多次重新创建,我该如何避免这种情况.

c# static multithreading task

0
推荐指数
1
解决办法
5330
查看次数

从AlertDialog(Android)启动长时间运行的任务

我有一个AlertDialog,它有一个按钮,如果选中,将启动文件上传到远程服务器,如下所示:

builder.setMessage(text)                
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
uploadFile();
}})
Run Code Online (Sandbox Code Playgroud)

这很好用,唯一的问题是uploadFile()可能需要很长时间(30秒到2分钟).我想用一个进度对话框(甚至是一个不确定的对话框)替换AlertDialog,但是我看不到如何从另一个对话框启动一个对话框?

任何人都可以提供一些建议,我可以做到这一点.

谢谢,Jarabek

android task android-alertdialog

0
推荐指数
1
解决办法
1582
查看次数

为什么双位数选择不能在python中返回正确的响应?

我正在学习如何使用python.在了解我不理解的定义时发现了一个问题.我给出一个简单的菜单,选择0-4.如果用户选择4以上,则应该收到一条消息"这不是一个有效的选择..."

但是,如果您输入的值大于或等于10,则除菜单外不会返回任何内容...没有消息.

在此先感谢任何想法.

这是我的代码:

# Multitasker
# Allows User to Pick an Item that is Defined.

def exit():
    print("See You Later!")
def task1():
    print("This is Task 1!")
def task2():
    print("This is Task 2!")
def task3():
    print("This is Task 3!")
def task4():
    print("This is Task 4!")

choice = None
while choice != "0":
    print(
        """
        Multitask Selector

        0 - Quit
        1 - Task 1
        2 - Task 2
        3 - Task 3
        4 - Task 4
        """
        )

    choice = input("Pick …
Run Code Online (Sandbox Code Playgroud)

python task

0
推荐指数
1
解决办法
92
查看次数

如何让Callable等到执行?

我有一个Callable,我调用了它

FutureTask<Integer> task = new FutureTask<Integer>(new MyCallable(name, type));
pool = Executors.newSingleThreadExecutor();
pool.submit(task);
Run Code Online (Sandbox Code Playgroud)

我想知道执行是继续执行pool.submit(task)还是等待可调用来完成执行?

总之我只是想知道有没有像thread.join()Callable 这样的方法?

java multithreading callable task

0
推荐指数
1
解决办法
8352
查看次数

当我使用任务秒表时,将显示经过的时间,但是当我使用线程时,它将不会显示.这是为什么?

我很困惑,为什么当我使用任务秒表的时间会显示但是当我使用线程时它不会.我的代码出了什么问题?我错过了什么吗?

static void Main(string[] args)
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();

        //if I used this sw.Elapsed will display
        //Task t1 = Task.Factory.StartNew(runTask1);
        //Task t2 = Task.Factory.StartNew(runTask2);
        //Task.WaitAll(t1, t2);

        //if I used this sw.Elapsed will not display
        //Thread t1 = new Thread(runTask1);
        //Thread t2 = new Thread(runTask2);
        //t1.Start();
        //t2.Start();

        sw.Stop();

        Console.WriteLine(sw.Elapsed);

        Console.ReadLine();
    }

    public static void runTask1()
    {
        for (int x = 1; x <= 5000; x++)
        {
            Console.WriteLine("Run task tester 1");
        }
    }
    public static void runTask2()
    {
        for …
Run Code Online (Sandbox Code Playgroud)

c# multithreading task task-parallel-library

0
推荐指数
1
解决办法
523
查看次数

将任务分组到任务[].我们为什么需要它?

有人可以解释是否有任何区别

 Task[] taskArray = new Task[]
 {
    Task.Factory.StartNew(() => GetData1()),
    Task.Factory.StartNew(() => GetData2())
 };
Run Code Online (Sandbox Code Playgroud)

只是

Task.Factory.StartNew(() => GetData1());
Task.Factory.StartNew(() => GetData2());
Run Code Online (Sandbox Code Playgroud)

谢谢!

.net c# task

0
推荐指数
1
解决办法
70
查看次数

wp8的任务队列?

在wp8中对复杂任务进行排队的正确方法是什么?

任务包括以下内容:

  1. 显示ProgressIndicator通过更新模型变量
  2. 获取或存储数据到wcf服务(UploadStringAsync)
  3. 使用结果更新可能的数据绑定模型UploadStringCompleted.
  4. 隐藏ProgressIndicator更新模型变量

目前我一直在使用一个拥有命令对象队列的类,运行一个在添加项目时启动的线程(如果它尚未运行).

但是,我有等待代码停止运行的任务或子任务的问题.

以前我曾经使用异步等待,但是行为的几个级别变得越来越难以预测.

我想要的是能够创建和排队命令对象的主线程.命令对象应该一次运行一个,而不是在前一个完全完成之前启动一个新对象.如果需要,命令对象应该能够使用调度程序访问主线程.

c# task dispatcher windows-phone-7 windows-phone-8

0
推荐指数
1
解决办法
910
查看次数

从按钮单击(UI线程)运行多个任务 - 如何知道它们是否全部成功或某些失败?

我有WPF应用程序,在按钮单击处理程序中我想要启动多个任务(其中每个任务可以执行长时间运行).

我想向用户报告所有任务是否成功,或者是否成功,单个任务的错误是什么.

虽然区分至少一个例外或者所有例外,但我看不出一个好方法.

using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows;

namespace WPFTaskApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {

            Task t1 = new Task(DoSomething);
            Task t2 = new Task(DoSomethingElse);

            t1.Start();
            t2.Start();

            // So now both tasks are running.
            // I want to display one of two messages to the user:
            // If both tasks completed ok, as …
Run Code Online (Sandbox Code Playgroud)

c# multithreading task task-parallel-library

0
推荐指数
1
解决办法
968
查看次数

如何等待任务的完成及其继续?

我学习以书本为基础的线程。

我想等待我的任务及其继续进行。但是我在“ BLEEEEEEEEEEEEEEEP ” 之前看到“按任意键退出... ”消息(请查看我的代码注释)。为什么会发生,如何解决?

我知道我可以使用两个任务并将其Task.Wait()用于每个任务,但是如果我需要对延续对象执行相同的操作怎么办?

在此处输入图片说明

using System;
using System.Threading;
using System.Threading.Tasks;

namespace Bushman.Sandbox.Threads {
    class Program {
        static void Main(string[] args) {
            Console.Title = "Threads";

            try {
                // The main work
                Action act1 = () => {
                    for (int i = 0; i < 10; i++) {

                        int id = Thread.CurrentThread
                        .ManagedThreadId;

                        Console.WriteLine(
                            "bleep. Thread Id: {0}",
                            id.ToString());
                    }
                };

                // This operation is to be done when the main 
                // work will …
Run Code Online (Sandbox Code Playgroud)

.net c# multithreading task

0
推荐指数
1
解决办法
684
查看次数

在C#中的async-await块中使用"out"时,不清楚编译器错误

我有一个我正在调用的方法,我想确保它在继续之前完成.我也有兴趣知道它是否成功完成,所以我正在做以下事情:

async void Foo() {
  bool success;

  // stuff

  await Task.Run(
    () => Bar(out success)
  );

  if (!success) { // this is the line causing the compiler-error
    // handle
  }

  // other stuff
}

void Bar(out bool success);
Run Code Online (Sandbox Code Playgroud)

但是我收到了错误

CS0165使用未分配的局部变量'success'

这不是一个大问题,因为我可以变得可爱并初始化success=false并传递它,ref而不是out看起来像希望的那样.但是,我很好奇async-await(或Task.Run)的复杂性似乎会导致一个不保证的情况out success会被正确分配.

任何启示都表示赞赏!

编辑:

要添加更多上下文,以下块编译并执行正常.

void Caller() {
  bool success;
  Callee(out success);
  if (success) {
    // do something
  }
}

void Callee();
Run Code Online (Sandbox Code Playgroud)

这是因为out参数在传递之前不需要初始化.有关更多信息,请访问out:https …

c# out task async-await

0
推荐指数
1
解决办法
76
查看次数