當前位置:首頁 » 編程語言 » c語言可以同時運行兩個子程序嗎
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言可以同時運行兩個子程序嗎

發布時間: 2023-05-28 22:40:42

1. c語言中如何讓兩個程序同時運行

C語言中語句的執行是線性的,就是說執行完一條在執行下一條,所以。。。

2. C語言怎麼實現同時運行兩個子程序

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace _2
{
public partial class Form1 : Form
{
public Form1()
{
Control. = false;//忽略錯誤線程
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Thread t1 = new Thread(new ThreadStart(run1));
t1.Start();
Thread t2 = new Thread(new ThreadStart(run2));
t2.Start();
}
public void run1()
{
while (1 == 1)
{
int i1 = label1.Location.X;
Random rd = new Random();
int i = rd.Next(19);
i1 = i1 + i;
Point p = new Point(i1, label1.Location.Y);
Thread.Sleep(22);
if (label1.Left >= this.Size.Width - label1.Size.Width)
{
Thread.CurrentThread.Abort();
}
}
//while (1 == 1)
//{
// Random rd = new Random();
// int i = rd.Next(19);
// label1.Left += i;
// Thread.Sleep(22);
// if (label1.Left >= this.Size.Width - label1.Size.Width)
// {
// Thread.CurrentThread.Abort();
// }
//}
}
public void run2()
{
int i1 = label1.Location.X;
int i2 = label2.Location.X;
//while (1 == 1)
//{
// Random rd = new Random();
// int i = rd.Next(19);
// label2.Left += i;
// Thread.Sleep(22);
// if (label2.Left >= (this.Size.Width - label2.Size.Width))
// {
// Thread.CurrentThread.Abort();
// }
//}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}

}
}

3. C語言如何同時運行兩個子函數

同時執行,使用線程了
C語言本身沒有提供線程的功能,只能調用平台的線程來實現
如果在 WINDOWS 下面,可以參考一下 CreateThread 方法

4. 怎樣用c語言實現多任務同時運行,急.......

用個API函數。
包函頭文件 window.h
CreateThread(NULL,0,FunPro,NULL,0,NULL);

你再寫個線程函數 FunPro

int WINAPI FunPro()
{
return 0;
}

大概是這樣,我也沒寫過,你自己再去網路下。

5. c語言中如何同時運行多個源文件

第一步:

將源文件1(1.c)修改為如下形式:

#include "print.h"
#include "2.c"
int main(void)
{
printHello();
return 0;
}

其中的2.c就是源文件2的文件名

第二步,將三個文件保存到同一目錄中

第三步,打開TC2,執行FILE-CHANGE DIR,將工作目錄換到三個文件所在的目錄。

第四步,在TC2中打開1.c文件,編譯運行。

建議不要再使用TC2這個相對原始的IDE了,上面介紹的這個方法也並不是標准方法,建議使用TC2006/VC/BCB等現代的IDE環境,如果實在是舍棄不下DOS字元界面,那就試試GCC吧!