小编Rob*_*Rob的帖子

这个阻塞线程如何调用"释放"自身?

我正在运行一个基本的单线程应用程序.

通常,在调用时System.Windows.Forms.MessageBox.Show(),可以预期此调用会有效阻止进一步执行,直到此方法返回为止.

但是,在使用的时候System.Windows.Forms.Timer,似乎**线程以某种方式释放本身和TimerTick事件被烧成这个相同的线程.

到底是怎么回事?我觉得这可能与穿线公寓有关,但我想澄清一下.

作为控制台应用程序以其最简单的形式重新创建如下:

class Program
{
    static void Main(string[] args)
    {
        new Program();
        while (true)
        {
            System.Windows.Forms.Application.DoEvents();
        }
    }

    private System.Windows.Forms.Timer timer;

    public Program()
    {
        timer = new System.Windows.Forms.Timer() { Interval = 2000 };
        timer.Tick += timer_Tick;
        timer.Start();
    }

   private void timer_Tick(object sender, EventArgs e)
    {
        Console.WriteLine(string.Format("Thread {0} has entered", Thread.CurrentThread.ManagedThreadId)); 
        var result = MessageBox.Show("Test");
        Console.WriteLine(string.Format("Thread {0} has left", Thread.CurrentThread.ManagedThreadId)); 
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

线程10已进入
线程10已进入
线程10已进入
线程10已进入
线程10已进入

c# multithreading timer winforms

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

为什么我的for循环告诉我我将int转换为bool?

这是我的代码......

for (int Position = 0; CardsInDeck.Length; Position++) 
{
    if (RandomlySelectedCard == CardsInDeck [Position]) 
    {
        Position = 0;
    } else {
        CardsInDeck [Position] = RandomlySelectedCard;
    }
}
Run Code Online (Sandbox Code Playgroud)

Unity告诉我它不能将int转换为bool,但我已经检查了所有内容并且看起来很好.顺便说一句,我正在制作纸牌游戏.

c# loops for-loop unity-game-engine

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

如何在Angular 4中单击时获取按钮的值

我有 4 个按钮,每个按钮都有唯一的值。我在控制台日志中使用当前代码未定义。

HTML

<button type="submit" class="parcel-btn weight-option" value="25" (click)="onItemSelector()">
<button type="submit" class="parcel-btn weight-option" value="50" (click)="onItemSelector()">
<button type="submit" class="parcel-btn weight-option" value="250" (click)="onItemSelector()">
<button type="submit" class="parcel-btn weight-option" value="100" (click)="onItemSelector()">
Run Code Online (Sandbox Code Playgroud)

打字稿

onItemSelector() {
  const weightSelector = this.elm.nativeElement.querySelectorAll('.weight-option').value;
  console.log(weightSelector);
}
Run Code Online (Sandbox Code Playgroud)

javascript typescript angular

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

目录中所有文件中的行数

我有一个包含许多源代码文件的文件夹.

我想在目录中的所有文件中找到源代码行的总数.

有没有简单的方法来做到这一点?

这些是cobol文件,我没有开放它们的开发工具,所以我不能只运行代码指标.

windows cobol

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

跨线程操作无效:控制'webForm'从其创建的线程以外的线程访问

我知道这个问题在这里被问了太多次,但我似乎没有修复它所以我发布了一个新问题.

我有两种形式Form1和Form2.Form2 通过将其最顶层属性设置为true,在form1上显示为叠加层.我有一个类为form1处理事务,如果找到所需的值,它会在STA线程中打开form2作为form1上的叠加层.

但是,如果在接下来的几秒钟内找不到该值,则类处理是一个连续的过程,然后我需要关闭已经打开的form2,由于上述异常,我目前无法做到这一点.

这是我的代码:

private void runBrowserThread(bool markerFound)
{
    try
    {
        var th = new Thread(() =>
        {
            if (markerFound == true)
            {
                if (Application.OpenForms.OfType<webForm>().Count() == 0)
                {
                    webForm frm = new webForm();
                    frm.Show();
                }

                Application.Run();
            }
            else
            {
                if (Application.OpenForms.OfType<webForm>().Count() == 1)
                {
                    Form fc = Application.OpenForms["webForm"];

                    if (fc != null)
                        fc.Close(); //**This line causes cross thread exception**
                }
            }
        });
        th.SetApartmentState(ApartmentState.STA);
        th.Start(); 
    } catch (Exception) 
    {
        //...
    }
}
Run Code Online (Sandbox Code Playgroud)

任何建议都会非常感激.

c# multithreading winforms

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

无法在Visual Studio中调试WPF项目

我有使用C#的VS 2015社区.我创建了一个WPF应用程序.

当我按F5或播放Visual Studio开始正常调试应用程序.我甚至看到诊断显示内存使用情况和CPU(但它锁定,例如在20毫秒并保持在那里).问题是,有时应用程序将在调试时打开并运行,大多数情况下它不会.重要的是要注意,visual studio本身并没有锁定,只是诊断工具窗口.

如果我在没有调试的情况下运行,应用程序正常打开并且没有问题,但如果我想调试,应用程序甚至不会打开.

对不起,我没有错误显示,因为我没有错误或警告.它只是在调试期间不会打开.

是否有我错过的补丁/更新或之前有任何人遇到此问题?

c# wpf

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

使用带有List <>的foreach循环

我有这样的数组

public List<Application> all_roads = new List<Application>();
Run Code Online (Sandbox Code Playgroud)

和一个带有字段的对象:

public string point_of_distination;
public uint length_of_route;
public double price;
public string our_drv_name;
public string our_drv_surname;
public string our_bus_model;
public double gen_ticket_price;
public short cur_year;
public byte cur_day;
public byte cur_month;
public double tour_consumption;
Run Code Online (Sandbox Code Playgroud)

我想计算tour_consumption所有对象的总和.我foreach该怎么写这个循环?请帮我.

c#

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

C#如何衡量经过的时间?

我想记录执行自定义方法所需的时间,但我以前从未使用过计时器类或方法,我知道我做错了.

这就是我所拥有的.

System.Timers.Timer Time;
int Mili = 0 ;

data = Generate();

Time.Enabled = true;
BSort= BubbleSort(data);

Time.Enabled = false;
Run Code Online (Sandbox Code Playgroud)

c# elapsedtime

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

在C#中从base64获取图像大小

如何从C#中获取图像大小base64

有可能做到吗?

c# base64 image

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

确保参数是一个数组

我正试图找到一种方法来检查函数参数是否是一个数组.如果不是,请将其转换为数组并对其执行功能,否则只需对其执行一项功能.

例:

interface employee {
    first: string,
    last: string
}

function updateEmployees (emp: employee | employee[]) {
    let employees = [];
    if (emp instanceof Array) employees = [emp];
    else employees = emp;
    employees.forEach(function(e){
        return 'something'
    })
}
Run Code Online (Sandbox Code Playgroud)

这似乎对我有用,但却发出警告 Type 'employee' is not assignable to type 'any[]'. Property 'length' is missing in type 'employee'.

javascript arrays typescript

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