相关疑难解决方法(0)

有人在使用ThreadPool时可以解释这种奇怪的行为吗?

代码

using System;
using System.Threading;

public delegate void LoadingProgressCallback(double PercentComplete,string ItemName);
public delegate void LoadCompleteCallback(int ItemID, string ItemName);

public static class Program
{
    public static void Main(string[] args)
    {
        LoadTest loadTest = new LoadTest();
        loadTest.LoadItems(args);
    }
}

public class LoadTest
{       
    ManualResetEvent resetEvent;
    int numThreads = 0;

    public LoadTest()
    {}

    public void LoadItems(string[] Items)
    {
        numThreads = 0;
        resetEvent = new ManualResetEvent(false);

        foreach(string item in Items)
        {
            Console.WriteLine("Adding {0} to ThreadPool",item);
            ThreadPool.QueueUserWorkItem
            (
                delegate
                {
                    Load(item, this.progCall, this.compCall);
                }
            ); …
Run Code Online (Sandbox Code Playgroud)

c# multithreading threadpool

4
推荐指数
1
解决办法
179
查看次数

繁忙等待线程

基本上,我需要忙等到网页上出现一些html.我已经创建了以下代码来忙着等我:

public void ExecuteBusyWaitThreads()
    {

        foreach (Canidate canidate in allCanidates)
        {
            Thread newThread = new Thread(delegate()
            {
                BusyWait(canidate);
            });

            newThread.Start();
        }
    }

    public bool BusyWait(Canidate canidate)
    {
        //hit that url, and wait for the claim all button to appear
        string page = null;
        while (found == false)
        {
            HttpWebRequest request = Canidate.GetHTTPRequest(canidate.URL);
            //make sure we add the authentication cookes to the request
            request = Canidate.AddCookiesToRequest(request, canidate.GetCookies());
            page = new Canidate().GetPage(request);
            if (page.ToLower().Contains("claim all"))
            {
                found = true;
                NotifyAllThreads();
            }
        } …
Run Code Online (Sandbox Code Playgroud)

.net c# multithreading thread-safety

3
推荐指数
1
解决办法
1609
查看次数

线程:不正确的变量传递C#

用户.我遇到了一个我无法找到答案的问题.我是Threading的新手(在C#中),遇到了这个问题.我有这个带有效果的图像编辑器,但由于它运行得太慢,我试图将其拆分为线程.问题是他总是使用效果列表中的最后一项运行"CreatePreview"命令.因此,如果我激活了效果:"黑/白","Sature"和"GreenFilter",它将尝试使用greenfilter创建3个预览.

任何人都可以帮我解决这个问题吗?

private void CreatePreviews(string fileName, List<IEffect> effects)
{
    List<Task> tasks = new List<Task>();
    foreach (var effect in effects)
    {
        //previews.Add(effect, CreatePreview(fileName, effect));
        Task task = new Task(delegate()
        {
            string result = CreatePreview(fileName, effect);
            Dispatcher.BeginInvoke(new Action(
            delegate()
            {
                ShowPreview(result, effect.DisplayName);
            }));

        });
        task.Start();
    }
}
Run Code Online (Sandbox Code Playgroud)

c# variables multithreading

2
推荐指数
1
解决办法
459
查看次数

标签 统计

c# ×3

multithreading ×3

.net ×1

thread-safety ×1

threadpool ×1

variables ×1