private void btn_login_Click(object sender, EventArgs e) { if (mqHelper == null) { mqHelper = initSdk(); } }
private void btn_sendManyMulThread_Click(object sender, EventArgs e) { int threadCount = 0; int msgCount = 0; if (int.TryParse(txt_sendCount.Text.Trim(), out msgCount) == false) { MessageBox.Show("发送条数必须是数字"); } else if (msgCount <= 0) { MessageBox.Show("发送条数必须大于0"); } else if (int.TryParse(txt_threadCount.Text.Trim(), out threadCount) == false) { MessageBox.Show("并发数必须是数字"); } else if (threadCount <= 0) { MessageBox.Show("并发数必须大于0"); } else { mqList.Clear(); for (int i = 0; i < threadCount; i++) { MQ_SDK sdk = initSdk(); if (sdk == null) { MessageBox.Show("并发SDK初始化失败"); return; } else { mqList.Add(sdk); } } int msgCountPerThread = msgCount / threadCount; SetrichTextBox(string.Format("并行化准备完毕,准备发送。共{0}个线程并发,每线程发送消息{1}条", threadCount, msgCountPerThread)); sw = new Stopwatch(); sw.Start(); SetrichTextBox("并行发送开始at " + DateTime.Now.ToString("HH:mm:ss fff")); Parallel.ForEach(mqList, mq => { var r = work(mq, msgCountPerThread); }); } }
private Task send(MQ_SDK m_mqSend, string msg, int msgCountPerThread) { return(Task.Run(() => { string channel = txt_sendChannel.Text.Trim(); string queue = txt_sendQueue.Text.Trim(); for (int i = 0; i < msgCountPerThread; i++) { string sendResult = m_mqSend.sendMSG(msg, channel, queue); if (ckb_showResult.Checked) { SetrichTextBox(string.Format("ID为{0}的线程发送第{1}条消息结果(13表示成功):{2}", Thread.CurrentThread.ManagedThreadId, i, sendResult)); } } sw.Stop(); SetrichTextBox(string.Format("****** ID为{2}的线程发送结束at {0},耗时{1} ******", sw.ElapsedMilliseconds, DateTime.Now.ToString("HH:mm:ss fff"), Thread.CurrentThread.ManagedThreadId)); })); }
private MQ_SDK initSdk() { MQ_SDK sdk = new MQ_SDK(); List <ConnectorConfig> list = new List <ConnectorConfig>(); list.Add(new ConnectorConfig(txt_sendChannel.Text.Trim(), txt_sendQueue.Text.Trim())); try { sdk.initial(list); string result = sdk.login(txt_user.Text.Trim(), txt_pwd.Text.Trim(), txt_sendQueue.Text.Trim()); SetrichTextBox("登录返回结果(8表示成功):" + result); return(sdk); } catch (Exception ex) { SetrichTextBox(ex.Message); return(null); } }
private async Task work(MQ_SDK sdk, int msgCountPerThread) { await send(sdk, rtb_message.Text.Trim(), msgCountPerThread); }