小编Ini*_*eer的帖子

Parallel.For():在循环外更新变量

我只是在研究新的.NET 4.0功能.有了这个,我正在尝试使用Parallel.For和正常for(x;x;x)循环进行简单的计算.

但是,我有50%的时间得到不同的结果.

long sum = 0;

Parallel.For(1, 10000, y =>
    {
        sum += y;
    }
);

Console.WriteLine(sum.ToString());

sum = 0;

for (int y = 1; y < 10000; y++)
{
   sum += y;
}
Console.WriteLine(sum.ToString());
Run Code Online (Sandbox Code Playgroud)

我的猜测是线程正在尝试同时更新"sum".
有明显的方法吗?

.net c# parallel-processing .net-4.0

35
推荐指数
3
解决办法
2万
查看次数

程序员如何实践代码重用

我是一个糟糕的程序员,因为我正在复制和粘贴.一个例子是,每当我连接到数据库并检索记录集时,我将复制先前的代码并进行编辑,复制设置datagridview和编辑的代码.我知道短语代码重用,但我实际上并没有使用它.我如何利用代码重用,这样我就不必复制和粘贴数据库代码和datagridview代码.

code-reuse

13
推荐指数
2
解决办法
2659
查看次数

C#:明确声明"不安全"/编译器选项的好处

我理解指针,并且很少需要在C#代码中使用它们.我的问题是:在一段代码中明确说明"不安全"的原因是什么.另外,为什么必须更改编译器选项以允许"不安全"代码?

结论: CLR(或语言规范)中的内容使得我们不能随时使用指针(很像C和C++)而不必键入"unsafe"并更改编译器选项?

澄清一下:我知道"不安全"和"安全"的代码是什么.这只是一个问题,为什么我们必须做所有额外的工作(好吧,不是那么多)只是为了能够使用这些功能.

c# pointers unsafe compiler-options

8
推荐指数
2
解决办法
1906
查看次数

C Lib设计 - 结构和内存管理[最佳实践]

所以,我是一个尝试学习C的C#人.作为第一个(个人)项目,我试图编写一个基本的坐标几何库.

问题:在幕后的堆上分配内存而不是让以图书馆为目标的程序员这样做是不是最好的C编程实践呢?

例如,我的'point'结构和相关方法:

point.h

/* A basic point type. */
typedef struct point
{
    float x;
    float y;
    float z;
    char *note;
}point;

/* Initializes a basic point type. [Free with free_point method] */
point *create_point(float pos_x, float pos_y, float pos_z, char *_note);
/* Frees a point type. */
void free_point(point *_point);
/* Finds the midpoint between two points. */
point *midpoint(point *pt1, point *pt2);
Run Code Online (Sandbox Code Playgroud)

point.c

#include "point.h"

/* Initializes a basic point type. [Free with free_point method] */ …
Run Code Online (Sandbox Code Playgroud)

c memory struct

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

你怎么在vb.net中找到5个中最多的?

这是在3中找到最大值的代码,但我想找到最大值的代码5:

Dim a, b, c As Integer

a = InputBox("enter 1st no.") 
b = InputBox("enter 2nd no.") 
c = InputBox("enter 3rd no.")

If a > b Then 
    If a > c Then 
        MsgBox("A is Greater") 
    Else 
        MsgBox("C is greater") 
    End If 
Else 
    If b > c Then 
        MsgBox("B is Greater") 
    Else 
        MsgBox("C is Greater")
    End If 
End If 
Run Code Online (Sandbox Code Playgroud)

vb.net max find

4
推荐指数
2
解决办法
4万
查看次数

如果支持C#和VB.NET,你会使用C++/CLI吗?

我总是有一个C++/CLI的东西.也许是因为没有多少开发人员使用它......或者仅仅因为它不同.

假设Microsoft完全支持C++/CLI,因为它们执行VB.NET和C#(即LINQ,WPF等).你会用吗?

如果没有,为什么?

c++-cli

3
推荐指数
2
解决办法
1164
查看次数

CodeDom:编译部分类

我正在尝试在文本文件中编译代码以更改WinForms应用程序主窗体上的TextBox中的值.IE浏览器.使用方法向调用表单添加另一个partial类.表单有一个按钮(button1)和一个TextBox(textBox1).

文本文件中的代码是:

this.textBox1.Text ="Hello World !!";

和代码:

namespace WinFormCodeCompile
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Load code from file
            StreamReader sReader = new StreamReader(@"Code.txt");
            string input = sReader.ReadToEnd();
            sReader.Close();

            // Code literal
            string code =
                @"using System;
                  using System.Windows.Forms;

                  namespace WinFormCodeCompile
                  {
                      public partial class Form1 : Form
                      {

                           public void UpdateText()
                           {" + input + @"
                           }
                       }
                   }";

            // Compile code
            CSharpCodeProvider cProv …
Run Code Online (Sandbox Code Playgroud)

c# reflection codedom

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

没有结果!!使用fork()使用dup2运行带有2个管道的execlp()

我正在编写一个C程序,它将运行Linux命令,如:
cat /etc/passwd | grep list | cut -c 1-5
我没有任何结果
*这里父母等待第一个孩子(child_pid)完成;
第一个孩子等第二个孩子(pfils)!!
任何的想法?
代码:

main()
{
    pid_t child_pid;
    int fd[2];
    int pfd[2];
    pipe(pfd);
    child_pid = fork ();

    if (child_pid != 0) 
    {
        wait(child_pid);
        close(pfd[1]);
        dup2(pfd[0],0);
        close(pfd[0]);
        execlp("cut","cut","-c","1-5",NULL);
    }
    else
    {
        pid_t pfils = fork();
        pipe(fd);

        if(pfils == 0)
        {
            close(fd[0]);
            dup2(fd[1],1);
            close(fd[1]);
            execlp("cat", "cat","/etc/passwd",NULL);
        }
        else
        {
            wait(pfils);
            close(fd[1]);
            dup2(fd[0],0);
            close(fd[0]);
            close(pfd[0]);
            dup2(pfd[1],1);
            close(pfd[1]);
            execlp("grep","grep","list",NULL);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c linux fork pipe exec

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

ListView中的Details属性未显示

我将列表视图设置为详细信息以尝试创建列,但不会显示列.什么阻止列显示?

Imports System.IO
Imports System.Windows.Forms.VisualStyles.VisualStyleElement.Menu


Public Class Jonathan_Roda_P4
    Dim TitlesData(21)
    Dim BOData(21)

    'Clear Button Function
    Private Sub ResetClearToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ResetClearToolStripMenuItem.Click
        'Clear Title LV
        DispResView.Clear()
        'Clear Box Office List
        BOList.Items.Clear()
        'Resets User Message to default
        UsrMsg.Text = "Data Not Loaded"
    End Sub

    'Help/Instructions Properties

    Private Sub InstructionsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles InstructionsToolStripMenuItem.Click
        MessageBox.Show("1. Click the 'Load Files' Button" & vbNewLine _
                        & vbNewLine _
                        & "2. Select two respective files." & vbNewLine _
                        & …
Run Code Online (Sandbox Code Playgroud)

vb.net listview details

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

打印奇数1-15的产品

我需要编写一个程序,打印出1到15之间所有奇数整数的乘积.无论我尝试什么,它都行不通,只打印产品.我搞砸了if声明吗?这是我的代码:

//Sophia Ali
#include <iostream>

using namespace std;



int main()
{
    int i;
    int product;

    for ( i = 1; i <=15; i+=2)
    {
        if(i%2!=0)
            product = product * i;
        else
            continue;

    }
    cout << "\n product: " << product;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++

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

fork()未声明的错误

我需要能够为一个小项目使用fork().问题是示例代码不起作用:

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main()
{ 
    pid_t pID = fork();
    if (pID == 0) // child
    {
        printf("CHILD\n");
        // Code only executed by child process
    }
    else if (pID < 0) // failed to fork
    {
        printf("FAIL\n");
    }
    else // parent
    {
        printf("PARENT\n"); // Code only executed by parent process
    }
    // Code executed by both parent and child.

    system("PAUSE");
    return 0;   
}
Run Code Online (Sandbox Code Playgroud)

编译器说:"20 D:\ Untitled1.cpp`forb'unclaclared(首先使用这个函数)"

但我已经在互联网上看到它应该位于#include <unistd.h>.

有任何想法吗?谢谢!

c c++ fork

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

如何加速输出字符串的缓冲?

此代码计时输出~380Kb字符串的两种方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace ConsoleApplication1
{
    class Program
    {
        static string outbuff = "";
        static void Main(string[] args)
        {
            {
                Stopwatch exectime = new Stopwatch();
                System.IO.StreamWriter file;
                exectime.Reset(); exectime.Start();
                file = new System.IO.StreamWriter("output.html");
                for (int i = 0; i < 18000; i++)
                {
                    outbuff += "444444444, 5555555555\n";
                }
                string fin = "\nString method took " + exectime.Elapsed.TotalSeconds + "s";
                file.WriteLine(outbuff);
                Console.WriteLine(fin);
                file.WriteLine(fin);
                file.Close();
            }
            {
                Stopwatch exectime = new Stopwatch(); …
Run Code Online (Sandbox Code Playgroud)

c# windows-7 visual-studio-2012

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

将值传递给main(int,char**)

我有一个程序显示ascii值和字母,但现在我修改程序只显示我正在寻找的西班牙字符.该程序被调用,是的,你得到它,"ascii".用户要做的是键入C:>asciiC:>ascii all显示所有ascii字符.要仅显示用户必须键入的西班牙语字符C:>ascii es.

为此我写了这个简单的程序

int main(int argc, char** argv) {
    if(argv[0] == "es"){
        abc::ascii_es();        
    }
    if(argv[0] == "all"){
        abc::ascii_all();
    }
    else
        abc::ascii_es();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但程序总是跳转到ascii_eselse条件关键字中的方法.我究竟做错了什么?

c++ gcc visual-c++

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